5 p.1 5 JPanel (toy example) 5.1 2 extends : Object java.lang.object extends... extends Object Point.java 1 public class Point { // public int x; public int y; Point x y 5.1.1,
5 p.2 5 5.2 Point int Java p Point Point p; 5.2.1 p = new Point(); Point 5.2.2 instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2; System.out.println("(" + p.x + ", " + p.y + ")"); 5.2.3. Point p1 = new Point(), p2 = new Point(); p1.x = 1; p1.y = 2; p2.x = 9; p2.y = 8; System.out.println("(" + p1.x + ", " + p1.y + ")"); System.out.println("(" + p2.x + ", " + p2.y + ")"); 5.2.1 5.3 C 5.3.1, Point.java 2
5.3. 5 p.3 public class Point { // ( public int x; public int y; // public void move(int dx, int dy) { x += dx; y += dy; public double distance() { return Math.sqrt(x * x + y * y); public void print() { System.out.printf("(%d, %d)", x, y); public void moveandprint(int dx, int dy) { print(); move(dx, dy); print(); // public Point(int x0, int y0) { x = x0; y = y0; move distance, print, moveandprint x, y move print. Point int 2 : 1 Point p = new Point(1, 2); Point x 1 y 2
5 p.4 5. PointTest Point main PointTest.java public class PointTest { public static void main(string args[]) { Point p = new Point(10, 20); p.move(1, -1); p.print(); System.out.println("<br />"); static C C++ PointTest Java Q 5.3.1 PointTest : PointTest.java Point.java PointTest.java javac Point.java 5.4 Point ColorPoint Point Point 5.4.1 5.4.2 Point ColorPoint 5.4.3 ColorPoint Point 5.4.4 extends Point.java ColorPoint.java 1 public class ColorPoint extends Point { public String color;
5.4. 5 p.5 public ColorPoint(int x, int y, String c) { super(x, y); /* 1 */ color = c; @Override public void print() { System.out.printf("<font color= %s >", color); // System.out.printf("(%d, %d)", x, y); /* 2 */ // super.print(); System.out.print("</font>"); // ColorPoint color print() GUI JPanel super(x, y) /* 1 */ Point super : print() HTML HTML ColorPoint print() 2 /* 2 */ Point print() super.print(); super main 1 /* 3 */ ColorPoint x 10 y 20 color green ColorPoint Point x y move /* 4 */ PointTest.java 2 public static void main(string args[]) { ColorPoint cp = new ColorPoint(10, 20, "green"); /* 3 */ cp.move(1, -1); /* 4 */ cp.print();
5 p.6 5 System.out.println("<br />"); Q 5.4.1 PointTest 2 Q 5.4.2 DeepPoint Point int depth x, y, depth print depth 5 DeepPoint (((((11, 19))))) 5 DeepPoint DeepPoint.java public class DeepPoint { // public DeepPoint(int x, int y, int d) { depth = d; public void print() { int i; for (i = 0; i < depth; i++) { System.out.print("("); System.out.printf("%d, %d", x, y); for (i = 0; i < depth; i++) { System.out.print(")"); 5.5 PointTest testpoint Point PointTest.java 3 public static void testpoint(point p) { p.move(10, 10);
5.5. 5 p.7 p.print(); main Point, ColorPoint, DeepPoint 3 testpoint PointTest.java 3 public static void main(string args[]) { Point p = new Point(1, 2); ColorPoint cp = new ColorPoint(3, 4, "green"); DeepPoint dp = new DeepPoint(5, 6, 5); testpoint(p); testpoint(cp); testpoint(dp); testpoint ColorPoint, DeepPoint Point, widening : CastTest.java ColorPoint cp = new ColorPoint(... ); Point p = cp; p.move(1, -1); CastTest.java // // p = new Point(3, 4); ColorPoint cp2 = (ColorPoint)p; // cp2.color = "red"; cp2.print(); p ColorPoint ClassCastException testpoint? testpoint move print ColorPoint?
5 p.8 5 Q 5.5.1 PointTest.java 3 (1). (11, 12)(13, 14)(15, 16) (2). (11, 12)<font color= green >(13, 14)</font>(((((15, 16))))) Java print 5.5.1 (dynamic binding) (static) (dynamic) C++ Java Point, ColorPoint, DeepPoint //... Point* p = new Point(1, 2); ColorPoint* cp = new ColorPoint(3, 4, "green"); DeepPoint* dp = new DeepPoint(5, 6, 5); testpoint(p); testpoint(cp); testpoint(dp); //... Point print (11, 12)(13, 14)(15, 16) C++ Java print 5.5.2 (virtual function) *p, *cp, *dp Java C++ virtual class Point { // : C++ public: int x, y; void move(int dx, int dy); virtual void print(void); ; C++
5.5. 5 p.9 Point moveandprint public void moveandprint(int dx, int dy) { print(); move(dx, dy); print(); moveandprint ColorPoint DeepPoint print moveandprint Point move print Point print moveandprint print (polymorphism) 1 Poly 2 Morph 1 (GUI) Java / JButton, JLabel, JTextField, JTextArea GUI JComponent javax.swing.jcomponent JComponent setvisible, setenabled, setlocation 5.5.2 HideShow.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HideShow extends JPanel implements ActionListener { private JTextField input; private JLabel lbl; 1 2 =
5 p.10 5 private JButton b1, b2; public HideShow() { setpreferredsize(new Dimension(300, 50)); lbl = new JLabel("label"); input = new JTextField("text", 5); b1 = new JButton("Hide"); b2 = new JButton("Show"); b1.addactionlistener(this); b2.addactionlistener(this); setlayout(new FlowLayout()); add(lbl); add(input); add(b1); add(b2); public void actionperformed(actionevent e) { if (e.getsource() == b1) { lbl.setvisible(false); input.setvisible(false); b1.setvisible(false); else if (e.getsource() == b2) { lbl.setvisible(true); input.setvisible(true); b1.setvisible(true);... // main Hide setvisible JComponent JComponent add = setvisible setvisible repaint paintcomponent
5.6. 5 p.11 5.6 : OverloadTest.java public class OverloadTest { double x, y; // public void foo(double dx, double dy) { // foo-1 x += dx; y += dy; public void foo(int dx, int dy) { // foo-2 x *= dx; y *= dy; public void print() { System.out.printf("(%g, %g)", x, y); System.out.println(); /* 1 */ public static void main(string[] args) { OverloadTest o = new OverloadTest(1.1, 2.2); o.foo(3.3, 4.4); // foo-1 o.print(); o.foo(2, 3); // foo-2 o.print(); /* 2 */ : Q 5.6.1 OverloadTest.java......... /* 1 */ OverloadTest.java bar public void bar(point p) { System.out.print("Point class: "); // bar-1
5 p.12 5 p.print(); System.out.println(); public void bar(colorpoint p) { System.out.print("ColorPoint class: "); p.print(); System.out.println(); // bar-2 OverloadTest.java /* 2 */ ColorPoint cp = new ColorPoint(0, 0, "red"); Point p = cp; o.bar(cp); // bar-2 o.bar(p); // bar-1 Q 5.6.2 OverloadTest.java /* 1 */, /* 2 */......... Java. 5.7 ColorPoint color "red", "green" 2 setcolor getcolor ColorPoint "black", "red", "green", "yellow","blue", "magenta", "cyan", "white" 5.7.1 String String == == true equals true java.lang.string public boolean equals(object s)
5.7. 5 p.13 public boolean equalsignorecase(string s) ColorPoint.java 2 public class ColorPoint extends Point { public String[] cs = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"; public String color; @Override public void print() { // System.out.print("<font color= " + getcolor() + " >"); System.out.printf("(%d, %d)", x, y); // super.print(); System.out.print("</font>"); // public void setcolor(string c) { int i; for (i = 0; i < cs.length; i++) { if (c.equals(cs[i])) { color = c; return; // public ColorPoint(int x, int y, String c) { super(x, y); setcolor(c); if (color == null) color = "black"; public String getcolor() { return color; setcolor getcolor color cp.color = "NoSuchColor"; 5.7.2 public 5.7.3
5 p.14 5 color ColorPoint... private String color; //...... color cs PointTest main cp.color = "NoSuchColor"; Q 5.7.1 : public : protected private, public, protected public. 5.7.4 5.7.5 (encapsulation) ColorPoint color "black", "red" cs 2 private private
5.8. 5 p.15 5.7.2 color cs int ColorPoint 3 5.7.3 DeepPoint depth 1 10 void setdepth(int d) depth int getdepth() setdepth 0 11 1 10 depth 0 11 1 10 depth setdepth 5.7.4 SecretPoint Point 2 int a, b 2 print a x+b y = 1 (1, 2) (?,?) SecretPoint a, b print 5.8 < > Pair E1, E2 Pair.java public class Pair<E1, E2> { public E1 fst; public E2 snd; public Pair(E1 f, E2 s) { fst = f; snd = s; Triple.java public class Triple<E1, E2, E3> extends Pair<E1, E2> { public E3 thd; public Triple(E1 f, E2 s, E3 t) { super(f, s); thd = t; 3 color int
5 p.16 5 TripleTest.java public class TripleTest { public static void main(string[] args) { Triple<Integer, String, Double> test = new Triple<>(1, "abc", 1.4); System.out.printf("(%d, %s, %g)%n", test.fst, test.snd, test.thd); 5.8.1 Player Enemy GenericEnemy Boss Enemy Alice Grace GenericEnemy Enemy attack GenericEnemy updatedamage : Player.java 1 public class Player { 2 private int hp; 3 4 public Player(int inithp) { 5 hp = inithp; 6 7 8 public void damage(int damage) { 9 hp -= damage; 10 System.out.println(damage + " HP = " + hp); 11 12 : Enemy.java 1 public class Enemy { 2 public void attack(player p) { 3 : GenericEnemy.java 1 public class GenericEnemy { 2 private String name; 3 public int damage; 4 5 public GenericEnemy(String n, int d) { 6 name = n; 7 damage = d; 8 9
5.8. 5 p.17 10 @Override 11 public void attack(player p) { 12 System.out.print(name + " : "); 13 p.damage(damage); 14 updatedamage(); 15 16 17 public void updatedamage() { 18 : Alice.java 1 2 public class Alice { 3 private int init; 4 5 public Alice(int d) { 6 super("alice", d); 7 init = d; 8 9 10 @Override 11 public void updatedamage() { 12 damage += init; // 13 // name += "!"; // 14 15 : Grace.java 1 public class Grace { 2 private int ratio; 3 4 public Grace(int d, int r) { 5 super("grace", d); 6 ratio = r; 7 8 9 @Override 10 public void updatedamage() { 11 damage *= ratio; // 12 // name += "?"; // 13 14 : Boss.java 1 public class Boss { 2 public int count; 3 4 public Boss() { 5 count = 0;
5 p.18 5 6 7 8 @Override 9 public void attack(player p) { 10 System.out.print("Boss : "); 11 if (++count >= 3) { 12 p.damage(1000); 13 else { 14 p.damage(0); 15 16 17 main Main.java : Main.java 1 import java.util.arraylist; 2 3 public class Main { 4 public static void main(string[] args) { 5 ArrayList<Enemy> enemies = new ArrayList<>(); // ArrayList 6 Player p = new Player(3000); 7 8 Alice a = new Alice(100); 9 enemies.add(a); 10 Grace g = new Grace(100, 2); 11 enemies.add(g); 12 Boss b = new Boss(); 13 enemies.add(b); 14 15 for (int i = 0; i < 3; i++) { 16 for (int j = 0; j < enemies.size(); j++) { 17 Enemy e = enemies.get(j); 18 e.attack(p); 19 20 21 22 Main main
5.8. 5 p.19,,,,