作者dasea (植栽鸡肉饭)
看板ncyu_phyedu
标题[讨论] 继承
时间Sat Jan 30 14:57:01 2010
基本观念
Encapsulation,Message Passing以及Inheritance是构成Object-Oriented的三大要素,如
果某程式语言只具备前面两项特性,一般成为Object-Based。所谓Inheritance(继承),是
指Sub Class(子类别)继承Super Class(父类别)後,就会自动取得父类别特性。如果子类
别继承了一个以上的父类别,则称为Multiple Inheritance(多重继承)。Java为了避开多
重继承的复杂性, class只允许单一继承。
Java使用关键字extends来表达继承观念:
public class Animal {
public String moveMethod() {
return "Unspecified";
}
}
public class Bird extends Animal {
public String moveMethod() {
return "Fly";
}
}
public class Dog extends Animal {
public String moveMethod() {
return "run";
}
}
public class Fish extends Animal {
public String moveMethod() {
return "swim";
}
}
若class宣告时没有指定extends,则Java会自动extends java.lang.Object。
public class A {
}
和下面的写法相同
public class A extends java.lang.Object {
}
UpCasting(向上转型)和DownCasting(向下转型)
所谓casting是指型态转换, UpCasting是将子类别型态的reference转型为父类别型态,
DownCasting则是将父类别型态的reference转型成子类别型态。由於子类别可以视为和父
类别相容,如Fish, Dog, Bird都是一种Animal, 因此UpCasting一定没有问题:
Animal a;
Bird b;
a = b; // upcasting, Bird is a kind of Animal
父类别的reference可以指到子类别的Object,这种观念称为Polymorphism(多型)。
但在downcasting的情况下, 父类别的reference和子类别并不相容, 如Animal不见得是一
个Bird, 因此必须使用(SubClass)的casting语法来做强迫转换。
Animal a = new Bird(); // upcasting
Bird b;
b = (Bird)a; // downcasting, compile correct
if (a instanceof Bird) { // true
}
downcasting除了必须由设计者下达外, JVM在runtime也会检查实际的物件能否和
reference的型态相容
Animal a = new Dog(); // upcasting
Bird b;
b = (Bird) a; // downcasting, compile correct, but runtime error
比较完整的范例如下
public class InheritanceExample {
public static void main(String[] argv) {
Animal a1, a2, a3, a4;
Bird b;
Dog d;
Fish f;
a2 = a1 = new Animal();
b = new Bird();
d = new Dog();
f = new Fish();
System.out.println(a1.moveMethod());
System.out.println(b.moveMethod());
System.out.println(d.moveMethod());
System.out.println(f.moveMethod());
a1 = b; // Correct, we call this upcasting
b = a1; // Compile Error, type not compatible
b = (Bird)a1; // downcasting, Compile Correct
a2 = b; // Correct,we call this upcasting
d = a2; // Compile Error, type not compatible
d = (Dog)a2; // Compile Correct, but runtime error
}
}
Override(覆写)
子类别重新定义它所能看到的父类别中的method(如public, protected, 如果子类别和父
类别在同一个package里, 则没有修饰字的method也可以), 称为override。
public class Animal {
public String moveMethod() {
return "Unspecified";
}
}
public class Bird extends Animal {
// override Animal's moveMethod
public String moveMethod() {
return "Fly";
}
}
要特别强调的是
如果子类别看不到父类别的方法(如父类别的private方法,或子父类别不在同一个package
而子类别定义了父类别内的package method),则就算定义了同样的method,也不是
override
重复定义static method也不算override
子类别不可缩小父类别方法的存取范围
public class C2 {
public void a() {}
}
public class C1 extends C2 {
protected void a() { // Compile Error,不得缩小存取范围
}
}
Virtual Function(虚拟函数)
在讯息传递的章节里,我们有提到过Object接收到讯息後,是在Runtime才决定实际所要呼
叫的Method。由於父类别的reference可以指到子类别物件(Polymorphism),而子类别和父
类别可能都定义了相同的Method(Override),当使用父类别reference传递讯息给子类别物
件时,应该要呼叫父类别的方法还是子类别的方法? 如果
呼叫子类别的方法,则称为Virtual Function
呼叫父类别的方法,则称为Non-Virtual Function
有些程式语言,如C++,以上两种机制都提供,可由设计者自行决定。但是Java语言为了遵循
物件导向的精神,并避免设计者因语言设计复杂而犯错,因此只提供了Virtual Function。
public class InheritanceExample {
public static void main(String[] argv) {
Animal a1;
a1 = new Animal();
System.out.println(a1.moveMethod()); // print out "Unspecified"
a1 = new Bird(); // polymorphism
System.out.println(a1.moveMethod()); // print out "Fly"
}
}
请注意上一小节所提到Override的注意事项
class Animal {
public static String moveMethod() {
return "Unspecified";
}
public static void main(String[] argv) {
Animal a1;
a1 = new Bird();
System.out.println(a1.moveMethod()); // print out "Unspecified"
}
}
class Bird extends Animal {
// we can't override static method
public static String moveMethod() {
return "Fly";
}
}
上面的moveMethod()由於宣告为static,因此是依照reference的type来决定执行的method
。
class Animal {
private String moveMethod() {
return "Unspecified";
}
public static void main(String[] argv) {
Animal a1;
a1 = new Bird();
System.out.println(a1.moveMethod()); // print out "Unspecified"
}
}
class Bird extends Animal {
// this is not override because Bird can't see Animal's moveMethod
public String moveMethod() {
return "Fly";
}
}
由於上面Animal内的moveMethod宣告为private,因此执行时印出"Unspecified"。
采用Virtual Function的优点
Runtime自动寻找最特定的方法(尽量用子类别的方法),可用父类别reference呼叫到子类
别的方法,因此增加新的子类别时,不需要修改程式
缺点
执行起来比较慢
本章观念整理范例
public class Shape2D { // define super class
public double area() { // all Shape2D have their own area
return 0;
}
}
public class Rectangle extends Shape2D {
private double length, width;
public Rectangle(double l, double w) { // define constructor
length = l;
width = w;
}
public double area() { // Override
return length * width;
}
}
public class Circle extends Shape2D {
private double radius;
public Circle(double r) {
radius = r;
}
public double area() { // Override
return 3.141592654 * radius * radius;
}
}
public class Parallelogram extends Shape2D {
private double top, bottom, height;
public Parallelogram(double t, double b, double h) {
top = t;
bottom = b;
height = h;
}
public double area() { // Override
return (top + bottom) * height / 2.0;
}
}
publicclass Main {
public static double sum(Shape2D[] shapes) {
double total = 0;
for (int i = 0; i < shapes.length; i++) {
total += shapes[i].area(); // use Virtual Function to calculate
area of Shape2D
// Without Virtual Function, value of
Shape2D.area() will be 0
}
return total;
}
public static void main(String[] argv) {
Shape2D[] data; // array of reference to Shape2D
data = new Shape2D[5]; // create array object
data[0] = new Rectangle(2.4, 3.8); // Polymorphism
data[1] = new Circle(3.9);
data[2] = new Parallelogram(3.5, 6.7, 10.2);
data[3] = new Rectangle(5.3, 7.2);
data[4] = new Circle(4.6);
System.out.println("Sum of all Shape2D is "+sum(data));
}
}
如果程式语言不支援virtual function的话, 则上面的范例就得写成下面的形式才行
public class Main { // example for non-virtual function implementation
public double sum(Shape2D[] shapes) {
double total = 0;
for (int i = 0; i < shapes.length; i++) {
if (shapes[i] instanceof Rectangle) {
total += ((Rectangle)shapes[i]).area();
} else if (shapes[i] instanceof Circle) {
total += ((Circle)shapes[i]).area();
} else if (shapes[i] instanceof Parallelogram) {
total += ((Parallelogram)shapes[i]).area();
} // modify source code here for new sub classes
}
return total;
}
public static void main(String[] argv) {
Shape2D[] data; // array of reference to Shape2D
data = new Shape2D[5]; // create array object
data[0] = new Rectangle(2.4, 3.8); // Polymorphism
data[1] = new Circle(3.9);
data[2] = new Parallelogram(3.5, 6.7, 10.2);
data[3] = new Rectangle(5.3, 7.2);
data[4] = new Circle(4.6);
System.out.println("Sum of all Shape2D is "+sum(data));
}
}
final修饰字
final除可用来修饰变数外,也可放在class和object method前面:
public final class FinalClass {
public final void finalMethod() {
}
}
放在class前面表示class不可被继承, 放在object method表示不可被Override。
继承关系下的Constructor执行顺序
先将所有变数设为内定值。对数值型态来说,其值为0; 对reference来说,其值为null; 对
boolean来说,其值为false。
呼叫父类别的constructor。如果子类别Constructor里没有指定父类别的Constructor,
则使用父类别没有参数的Constructor。
执行变数宣告的初始化动作。
执行自己的constructor。
如果要指定父类别其他的constructor,则必须在子类别的constructor的第一行使用关键
字super来处理。
class Animal {
int aMask = 0x00FF;
public Animal() {
}
public Animal(int mask) {
aMask = mask;
}
}
public class Bird extends Animal {
int bMask = 0xFF00;
int fullMask;
public Bird() {
// Compiler add super() here
fullMask = bMask | aMask;
}
public Bird(int mask) {
/* 若有super,则必须放在第一行,连变数宣告也不能摆在super前面 */
super(mask);
fullMask = bMask | aMask;
}
public static void main(String[] argv) {
Bird b = new Bird();
System.out.println(b.fullMask);
b = new Bird(0x0011);
System.out.println(b.fullMask);
}
}
当执行new Bird()时,此物件内各个变数的变化如下
步骤 aMask bMask fullMask
default 0 0 0
call Bird() 0 0 0
call Animal() 0 0 0
Animal initialize 0x00FF 0 0
execute Animal() 0x00FF 0 0
Bird initialize 0x00FF 0xFF00 0
execute Bird() 0x00FF 0xFF00 0xFFFF
当执行new Bird(0x0011)时,此物件内各个变数的变化如下
步骤 aMask bMask fullMask
default 0 0 0
call Bird(0x0011) 0 0 0
call Animal(0x0011) 0 0 0
Animal initialize 0x00FF 0 0
execute Animal(0x0011) 0x0011 0 0
Bird initialize 0x0011 0xFF00 0
execute Bird(0x0011) 0x0011 0xFF00 0xFF11
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.58.22.74