Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Reusing Classes.

Similar presentations


Presentation on theme: "Chapter 6 Reusing Classes."— Presentation transcript:

1 Chapter 6 Reusing Classes

2 off the shelf:现货供应、非定制
from scratch: from the very beginning

3

4 “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

5 Types of Attributes Door Car Window window char color int price
Engine engine Wheel wheel Door left Door right Window

6

7

8

9

10

11

12

13

14

15

16 *参见程序运行

17 *参见程序运行

18

19

20 Reference:

21 “is a” relationship Please:
Person、 Employee、Customer、 Manager、 CSR 的继承树 “is a” relationship

22 Test Composition

23

24

25

26

27

28 Composition & Inheritance
Composition- ”has a” Inheritance -”is a”

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44 覆盖所得的方法的可访问级别,不能够比它所覆盖的方法访问级别更低。

45 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).

46 Polymorphism & Overriding

47 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;

48 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;

49 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 * getOrdersTaken() * .1;

50 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()); }

51 Result: java First made Second made Third made Normal Termination Output completed (0 sec consumed).

52

53

54

55

56 Casts up implicitly :向上转型隐式进行的,会引起方法丢失。子类自动具有父类类型,始终允许向概括类、抽象类转型。
Downward casts :向下转型必须是子类,并由编译器检查。 RTTI(运行期类型识别):运行期类型检查:对象的类型在运行时检查,若类型不匹配,将报错误信息。

57

58 //: 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

59

60

61 Overriding & Overloading
Overriding Methods--- same method Overloading Methods---many methods

62

63

64

65 构造器不存在覆盖的问题,因父子类不同。

66

67

68

69

70 Constructor and Initialization Example

71

72

73

74

75

76

77

78

79

80


Download ppt "Chapter 6 Reusing Classes."

Similar presentations


Ads by Google