38812 TimeAccount List 12-1 Account List 12-1 // Chap12/TimeAccount.java class TimeAccount { private String name; private String no; private long balance; private long timebalance; // // // // //--- ---// TimeAccount(String n, String num, long z, long timebalance) { name = n; // no = num; // balance = z; // this.timebalance = timebalance; // //--- ---// String getname() { return name; //--- ---// String getno() { return no;
389 //--- ---// long getbalance() { return balance; //--- ---// long gettimebalance() { return timebalance; //--- k ---// void deposit(long k) { balance += k; //--- k ---// void withdraw(long k) { balance -= k; //--- ---// void cancel(long k) { balance += timebalance; timebalance = 0; 12-1 Account Account a b 1, -1, 0 TimeAccount // static int compbalance(account a, Account b) { if (a.getbalance() > b.getbalance()) return 1; else if (a.getbalance() < b.getbalance()) return -1; return 0; // a // b // ab TimeAccount 4 4 4 Account TimeAccount
39012 class Base { int a; int b; int geta() { /**/ void seta(int a) { /**/ int getb() { /**/ void setb(int b) { /**/ class Derived extends Base { int c; int getc() { /**/ void setc(int c) { /**/ derive inheritance Base Derived Fig.12-1 extends Derived Derived Base Fig.12-1
391 Base Derived Derived Base Base Derived Base a, b a, b Derived c Base Fig.12-2 12-1 Fig.12-2
39212 List 12-2 // // class Point2D { int x; // int y; // Point2D(int x, int y) { this.x = x; this.y = y; void setx(int x) { this.x = x; // void sety(int y) { this.y = y; // int getx() { return x; // int gety() { return y; // Chap12/PointTester.java List 12-2 // class Point3D extends Point2D { int z; // Point3D(int x, int y, int z) { super(x, y); this.z = z; void setz(int z) { this.z = z; int getz() { return z; // // public class PointTester { public static void main(string[] args) { Point2D a = new Point2D(10, 15); Point3D b = new Point3D(20, 30, 40); a = (10, 15) b = (20, 30, 40) System.out.printf("a = (%d, %d)\n", a.getx(), a.gety()); System.out.printf("b = (%d, %d, %d)\n", b.getx(), b.gety(), b.getz());
393 Point2D Point3D Fig.12-1 Base Derived Point2D x, y x, y x y Point3D Point2D z 12-1 super(x, y); super(...) super(x, y) x, y x y ゆだ Point3D z super(...) super(...) super(...) this(...) super this main a b getx gety b Point2D getx gety Point3D
39412 Point3D super(...) // Point3D(int x, int y, int z) { this.x = x; this.y = y; this.z = z; super(...) super() // Point3D(int x, int y, int z) { super(); this.x = x; this.y = y; this.z = z; Point2D super(...) super(); X から X() { X() { super(); // X() { super();
395 List 12-3 List 12-3 Chap12/DefaultConstructor.java // // class A { private int a; x.geta() = 50 A() { a = 50; int geta() { return a; // class B extends A { // public class DefaultConstructor { public static void main(string[] args) { B x = new B(); System.out.println("x.getA() = " + x.geta()); A int a a 50 geta B A B() { super(); // 12-1 B super() A B B super() A(int x) { a = x; // B A a 50
39612 // class Base { protected int x; // void print() { System.out.println("Base.x = " + x); // class Derived extends Base { int x; // super Derived(int x1, int x2) { super.x = x1; this.x = x2; // void print() { super.print(); System.out.println("Derived.x = " + x); public class SuperTester { super. List 12-4 List 12-4 // Chap12/SuperTester.java Base() { this.x = 0; Base(int x) { this.x = x; public static void main(string[] args) { Base a = new Base(10); System.out.println("-- a --"); a.print(); Derived b = new Derived(20, 30); System.out.println("-- b --"); b.print(); -- a -- Base.x = 10 -- b -- Base.x = 20 Derived.x = 30 x protected x print Base Derived Derived Base x private protected
12-1Fig.12-3 397 x Base x Fig.12-3 Base x1 x2 x super.x Base x this.x Derived x super. this. x this.x print Base super.print() Base print Derived print Base x Derived x override
39812 Fig.12-4 Fig.12-4 A B B C D B A 4 C D B 4 A 4 4 4 4 4 class A { //... class C extends B { //... class B extends A { //... class D extends B { //... super class sub class direct super class direct sub class Table 12-1 Table 12-1
399 super super super() Fig.12-5 4 4 4 class Q extends A, B { //... Fig.12-5 class Derived extends Base { /**/ extends Base Base Base
40012 class A { //... class B extends A { //... class C extends B { //... class D extends B { //... Fig.12-6 Fig.12-6 Fig.12-4 Object A extends Object Object java.lang Column 12-6 extends Object extends Account Car Day Object Object Ojbect A Object B Object Account Car Day Object Object
401 List 12-5 TimeAccount List 12-5 // class TimeAccount extends Account { private long timebalance; // account2/timeaccount.java // TimeAccount(String name, String no, long balance, long timebalance) { super(name, no, balance); // Account this.timebalance = timebalance; // // long gettimebalance() { return timebalance; // void cancel(long k) { deposit(timebalance); timebalance = 0; 12-1 Account incremental programming List 12-2
40212 TimeAccount Account Account Account TimeAccount Account 4 Account TimeAccount List 12-6 List 12-6 // account2/timeaccounttester1.java x200 class TimeAccountTester1 { y200 y500 public static void main(string[] args) { Account adachi = new Account("", "123456", 1000); TimeAccount nakata = new TimeAccount("", "654321", 200, 500); Account x; x = adachi; x = nakata; System.out.println("x" + x.getbalance()); TimeAccount y; // // y = adachi; // y = nakata; // System.out.println("y" + y.getbalance()); System.out.println("y" + y.gettimebalance()); // // // main adachi Account nakata TimeAccount x Account y TimeAccount x y adachi nakata Fig.12-7 Account x adachi TimeAccount y nakata
403 Fig.12-7 12-1 nakata TimeAccount Account x TimeAccount Account Account TimeAccount gettimebalance cancel TimeAccount Account Account x Account TimeAccount TimeAccount y 4 4 4 4 4 Account adachi y adachi y gettimebalance y.gettimebalance() y adachi Account
40412 List 12-7 compbalance 4 4 4 4 4 4 4 4 4 a b 1, -1, 0 List 12-7 // account2/timeaccounttester2.java class TimeAccountTester2 { // static int compbalance(account a, Account b) { if (a.getbalance() > b.getbalance()) // a return 1; else if (a.getbalance() < b.getbalance()) // b return -1; return 0; // ab a b Account main Account TimeAccount Account Account TimeAccount public static void main(string[] args) { Account adachi = new Account("", "123456", 1000); TimeAccount nakata = new TimeAccount("", "654321", 200, 500); switch (compbalance(adachi, nakata)) { case 0 : System.out.println(""); break; case 1 : System.out.println(""); break; case -1 : System.out.println(""); break; a b TimeAccount main TimeAccount Account Car
405 Object Object List 12C-1 List 12C-1 Chap12/ToString.java // tostring class X { public String tostring() { return "Class X"; class Y extends X { public String tostring() { return "Class Y"; public class ToString { //--- tostring ---// 12-1static void print(object obj) { System.out.println(obj); public static void main(string[] args) { X x = new X(); Y y = new Y(); int[] c = new int[5]; print(x); print(y); print(c); Class X Class Y [I@ca0b6 X Object Y X print obj Object obj obj tostring main X x Y y int c print x X tostring "Class X" y Y tostring "Class Y" Object Object ObjecttoString final int length List 6-15 tostring
40612 name mastername Pet getname name ++++ **** ++++ **** Fig.12-8 getmastername mastername introduce Fig.12-8 List 12-8 Pet RobotPet Pet RobotPet super(...) Pet introduce Pet work 0, 1, 2 RobotPet Pet getname, getmastername introduce work
407 List 12-8 // class Pet { private String name; private String mastername; // // pet/pet.java // public Pet(String name, String mastername) { this.name = name; // this.mastername = mastername; // // public String getname() { return name; // public String getmastername() { return mastername; // public void introduce() { System.out.println("" + name + ""); System.out.println("" + mastername + ""); class RobotPet extends Pet { // public RobotPet(String name, String mastername) { super(name, mastername); // // public void introduce() { System.out.println("" + getname() + ""); System.out.println("" + getmastername() + ""); 12-2 // public void work(int sw) { switch (sw) { case 0: System.out.println(""); break; case 1: System.out.println(""); break; case 2: System.out.println(""); break; introduce override RobotPet introduce Pet introduce
40812 List 12-9 List 12-9 // class PetTester1 { public static void main(string[] args) { Pet kurt = new Pet("Kurt", ""); kurt.introduce(); System.out.println(); RobotPet r2d2 = new RobotPet("R2D2", ""); r2d2.introduce(); System.out.println(); Pet p = r2d2; p.introduce(); Pet kurt Pet introduce RobotPet r2d2 RobotPet introduce pet/pettester1.java Kurt R2D2 R2D2 Pet p RobotPet 4 4 4 p.introduce() p Pet Pet introduce R2D2 RobotPet RobotPet introduce R2D2
409 if (sw == 1) p = kurt; else p = r2d2; p.introduce(); // ppet // probotpet // ppetrobotpet sw int p Pet kurt 4 4 4 4 RobotPet r2d2 p kurt r2d2 Pet introduce 12-2 sw 1 Pet introduce RobotPet introduce 4 4 4 4 4 4 4 4 4 4
41012 polymorphism Fig.12-9 4 4 4 4 4 4 static bindingearly binding 4 4 4 dynamic bindinglate binding 4 4 Kurt R2D2 Fig.12-9
411 List 12-10 List 12-10 // class PetTester2 { // p static void intro(pet p) { p.introduce(); public static void main(string[] args) { Pet[] a = { new Pet("Kurt", ""), new RobotPet("R2D2", ""), new Pet("", ""), ; for (Pet p : a) { intro(p); System.out.println(); pet/pettester2.java Kurt R2D2 // p 12-2 intro p Pet p introduce p Pet Pet RobotPet main Pet RobotPet intro Pet a[0] a[2] Pet introduce RobotPet a[1] RobotPet introduce
41212 Pet RobotPet Pet p = new RobotPet("R2D2", ""); RobotPet Pet widening reference conversionup castfig.12-10 Pet p = (Pet)new RobotPet("R2D2", ""); RobotPet r1 = new Pet("Kurt", ""); // RobotPet r2 = (RobotPet)new Pet("Kurt", ""); // narrowing reference conversion down cast 4 4 4 4 4 4 4 4 4 4 4 r2 r2.work(0); // Fig.12-10
413 List 12-11 List 12-11 // instanceof class PetInstanceOf { public static void main(string[] args) { Pet[] a = { new Pet("Kurt", ""), new RobotPet("R2D2", ""), new Pet("", ""), ; pet/petinstanceof.java a[0] Kurt a[1] a[2] for (int i = 0; i < a.length; i++) { System.out.println("a[" + i + "] "); if (a[i] instanceof RobotPet) // a[i] ((RobotPet)a[i]).work(0); // else // a[i].introduce(); // 12-2 instanceof Table 12-2 instnaceof a[i] RobotPet a[i] RobotPet Table 12-2 x instanceof t x t true false if a[i] Pet RobotPet a[i] instanceof Pet true if (a[i] instanceof Pet) // PetPettrue a[i].introduce(); // a[i]petrobotpet else ((RobotPet)a[i]).work(0); //
41412 RobotPet 4 4 4 4 4 4 4 4 4 introduce introduction 4 class RobotPet { // public void introduction() { // System.out.println("" + getname() + ""); System.out.println("" + getmastername() + ""); // RobotPet introduction introduce introduction List 12-9 p.introduce() RobotPet Pet annotation @Override @Override class RobotPet { // @Override public void introduction() { // System.out.println("" + getname() + ""); System.out.println("" + getmastername() + ""); //
415 Pet introduction introduction class RobotPet { // @Override public void introduce() { // System.out.println("" + getname() + ""); System.out.println("" + getmastername() + ""); // 12-2@Override 4 æ` n téi n e e a, b compbalance static int compbalance(timeaccount a, TimeAccount b) a 1 0b -1 a b Account @Override @Deplicate @Deplicate
41612 member private private private
417 final final final String final 4 4 4 4 4 String class DeluxeString extends String { // //... final 12-3 final final final void f() { /**/ final final final final final final
41812 Fig.12-11 public m1 private m4 public class A { public void m1() { // public protected void m2() { // protected void m3() { // private void m4() { // private Fig.12-11 Table 12-3 Table 12-3 A m1 public public m2 public protected public public protected
419 A m4 tostring public Object tostring public Column 12-6toString public String tostring() public 12-3class A { static void f() { /* */ class B extends A { void f() { /* */ // public final Table 12C-1 Table 12C-1 publicprotectedprivateabstractstaticfinal strictfp publicprotectedprivatestaticfinaltransient volatile publicprotectedprivateabstractstaticfinal synchronizednativestrictfp publicprotectedprivateabstractstaticstrictfp
42012 Object List 12C-2 List 12C-2 // Object package java.lang; public class Object { static { registernatives(); public final native Class<?> getclass(); public native int hashcode(); public boolean equals(object obj) { return (this == obj); protected native Object clone() throws CloneNotSupportedException; API/Object.java public String tostring() { return getclass().getname() + "@" + Integer.toHexString(hashCode()); public final native void notify(); public final native void notifyall(); public final native void wait(long timeout) throws InterruptedException; public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative"); if (nanos < 0 nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); if (nanos >= 500000 (nanos!= 0 && timeout == 0)) { timeout++; wait(timeout); public final void wait() throws InterruptedException { wait(0); protected void finalize() throws Throwable { java.lang Object java.lang native getclass native
hashcode 421 int hashcode Day hashcode public int hashcode() { return (year * 372) + (month * 31) + date; year, month, date 372 12 31 equals equals true false a.equals(b) true a b a.hashcode() b.hashcode() false a b equals hashcode Day equals equalto hashcode Object equals public boolean equals(object obj) { if (this == obj) // return true; if (obj instanceof Day) { // objday Day d = (Day)obj; return (year == d.year && month == d.month && date == d.date)? true : false; return false; hashcode equalto equals day5 12-3 tostring Object tostring "@"
42212 Object Object super(...) super() super() super. B A B A
//--- ---// public class SpecialMember extends Member { private String privilege; // public SpecialMember(String name, int no, int age, String privilege) { super(name, no, age); this.privilege = privilege; @Override public void print() { super.print(); System.out.println("" + privilege); 42312 //--- ---// public class Member { private String name; private int no; private int age; // // // public Member(String name, int no, int age) { this.name = name; this.no = no; this.age = age; public String getname() { return name; public void print() { System.out.println("No." + no + "" + name + "" + age + ""); //--- ---// public class MemberTester { public static void main(string[] args) { Member[] m = { new Member("", 101, 27), new SpecialMember("", 102, 31, ""), new SpecialMember("", 103, 52, ""), ; for (Member k : m) { k.print(); System.out.println(); No.10127 No.10231 No.10352