Chapter 6 Reusing Classes
off the shelf:现货供应、非定制 from scratch: from the very beginning
“has a” relationship Example: public class Car { Engine engine = new Engine(); Wheel[] wheel = new Wheel[4]; Door left = new Door(); Door right = new Door(); … } “has a” relationship
Types of Attributes Door Car Window window char color int price Engine engine Wheel wheel Door left Door right Window
*参见程序运行
*参见程序运行
Reference:
“is a” relationship Please: Person、 Employee、Customer、 Manager、 CSR 的继承树 “is a” relationship
Test Composition
Composition & Inheritance Composition- ”has a” Inheritance -”is a”
覆盖所得的方法的可访问级别,不能够比它所覆盖的方法访问级别更低。
private void doSomething() 方法将出现错误信息:---------- javac ---------- UseBoth.java:5: doSomething() in Child cannot override doSomething() in Parent; attempting to assign weaker access privileges; was public private void doSomething() {} ^ 1 error Normal Termination Output completed (0 sec consumed).
Polymorphism & Overriding
class OrderTaker{ private String name; private int basePay; private int ordersTaken; public void setName(String newValue){ name = newValue; } public void setBasePay(int newValue) { basePay = newValue; public void incrementOrdersTaken() { ordersTaken++; public String getName() { return name; public int getBasePay(){ return basePay; public int getOrdersTaken(){ return ordersTaken; public double calculatePay() { //Generic order takers get a weekly portion of their salary return getBasePay() / 52;
public double calculatePay(){ class CSR extends OrderTaker { public double calculatePay(){ //CSRs get their weekly pay plus 10 cents per order they take return getBasePay() / 52 + getOrdersTaken() * .1; } class OEC extends OrderTaker { //OECs get their weekly pay plus 1 cent per order they take return getBasePay() / 52 + getOrdersTaken() * .01;
private int peopleHarassed; public void incrementPeopleHarassed(){ class Telemarketer extends OrderTaker{ private int peopleHarassed; public void incrementPeopleHarassed(){ peopleHarassed++; } public int getpeopleHarassed() { return peopleHarassed; public double calculatePay(){ /*Telemarketers get 10 cents for every order they take and 1cent per person they harass (call)*/ return getBasePay() / 52 + peopleHarassed * .01 + getOrdersTaken() * .1;
public class TestPolymorphism{ //class Clerks public static void main(String args[]) { OrderTaker first, second, third; first = new OEC(); second = new CSR(); third = new Telemarketer(); first.setBasePay(52000); second.setBasePay(52000); third.setBasePay(52000); first.incrementOrdersTaken(); second.incrementOrdersTaken(); third.incrementOrdersTaken(); ((Telemarketer) third).incrementPeopleHarassed(); System.out.println("First made " + first.calculatePay()); System.out.println("Second made " + second.calculatePay()); System.out.println("Third made " + third.calculatePay()); }
Result: ---------- java ---------- First made 1000.02 Second made 1000.1 Third made 1000.11 Normal Termination Output completed (0 sec consumed).
Casts up implicitly :向上转型隐式进行的,会引起方法丢失。子类自动具有父类类型,始终允许向概括类、抽象类转型。 Downward casts :向下转型必须是子类,并由编译器检查。 RTTI(运行期类型识别):运行期类型检查:对象的类型在运行时检查,若类型不匹配,将报错误信息。
//: c07:RTTI. java :From 'Thinking in Java, 3rd ed //: c07:RTTI.java :From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // Downcasting & Run-Time Type Identification (RTTI) : {ThrowsException} class Useful { public void f() {} public void g() {} } class MoreUseful extends Useful { public void u() {} public void v() {} public void w() {} public class RTTI { public static void main(String[] args) { Useful[ ] x = { new Useful(), new MoreUseful() }; x[0].f(); x[1].g(); // x[1].u(); Compile time: method not found in Useful: ((MoreUseful)x[1]).u(); // Downcast/RTTI ((MoreUseful)x[0]).u(); // Exception thrown
Overriding & Overloading Overriding Methods--- same method Overloading Methods---many methods
构造器不存在覆盖的问题,因父子类不同。
Constructor and Initialization Example