extends(扩展) 和 override(重写)
extends 关系导致的类型,子类一定是父类,父类一定不是子类。
就赋值而言, 父类 a= 子类 b 是 ok的 ;反过来 子类 x = 父类 y;是不ok的, 也就是说赋值时,类型层级上,右边一定是小于(低于)左边的。
给定 Animal 类,填写 Cat 类的定义,以便在greet()被调用时,“Cat”(而不是“Animal”)被打印到屏幕上。如果猫是 5 岁或以上,“Meow”,5岁以下,则会发出噪音并发出“MEOW”。
1. Animal
public class Animal {
protected String name, noise;
protected int age;public Animal(String name, int age) {
this.name = name;
this.age = age;
this.noise = "Huh?";
}public String makeNoise() {
if (age < 5) {
return noise.toUpperCase();
} else {
return noise;
}
}public void greet() {
System.out.println("Animal " + name + " says: " + makeNoise());
}
}
2.Cat
public class Cat extends Animal {public Cat(String name, int age) {super(name, age);this.noise = "Meow!";}@Overridepublic void greet() {System.out.println("Cat " + name + " says: " + makeNoise());}public static void main(String[] args) {Cat babyCat = new Cat("babyCat",1);Cat bigCat = new Cat("bigCat",5);// MEOW!babyCat.greet();
// Meow!bigCat.greet();}
}
extends 和override 2
对上面的 Animal和Cat ,给出下列程序运行结果:
1 public class TestAnimals {
2 public static void main(String[] args) {
3 Animal a = new Animal("Pluto", 10);
4 Cat c = new Cat("Garfield", 6);
5 Dog d = new Dog("Fido", 4);
6
7 a.greet(); // (A)___Pluto says Huh?___________________
8 c.greet(); // (B)___Garfield says Meow!___________________
9 d.greet(); // (C)___Fido says WOOF!___________________
10 a = c;
11 ((Cat) a).greet(); // (D) ______Garfield says Meow!________________
12 a.greet(); // (E) _______Garfield says Meow!_______________
13 }
14 }
15
16 public class Dog extends Animal {
17 public Dog(String name, int age) {
18 super(name, age);
19 noise = "Woof!";
20 }
21
22 @Override
23 public void greet() {
24 System.out.println("Dog " + name + " says: " + makeNoise());
25 }
26
27 public void playFetch() {
28 System.out.println("Fetch, " + name + "!");
29 }
30 }
如果在上述程序第12行下面加下面语句,会出现什么情况?如果出现了编译错误,为什么?怎么改?
1 a = new Dog("Spot", 10);
2 d = a;
答:
1 a = new Dog("Spot", 10); //没问题, Dog 可以看做是Animal,
2 d = a; //有问题, Animal 不是一个Dog, 上位不是下位。
答:
Dog is a subclass of Animal, so this assignment will
fail at compile time because not all Animals are Dogs. Use casting to address the
problem. 因为不是所有的Animal 都是 Dog ,上位词包含更多不一定是更具体的下位。
也就是右边一定要小于左边。
1 d = (Dog) a;
This represents a promise to the compiler that at runtime, a will be bound to an
object that is compatible with the Dog type.
3 找出下列程序的潜在错误并记录,消除这些错误后,D的运行结果是什么?
1 class A {
2 public int x = 5;
3 public void m1() {System.out.println("Am1-> " + x);}
4 public void m2() {System.out.println("Am2-> " + this.x);}
5 public void update() {x = 99;}
6 }
7 class B extends A {
8 public void m2() {System.out.println("Bm2-> " + x);}
9 public void m2(int y) {System.out.println("Bm2y-> " + y);}
10 public void m3() {System.out.println("Bm3-> " + "called");}
11 }
12 class C extends B {
13 public int y = x + 1;
14 public void m2() {System.out.println("Cm2-> " + super.x);}
15 \\ public void m4() {System.out.println("Cm4-> " + super.super.x); }} can't do super.super
16 public void m5() {System.out.println("Cm5-> " + y);}
17 }
18 class D {
19 public static void main (String[] args) {
20 \\ B a0 = new A(); Dynamic type must be B or subclass of B
21 \\ a0.m1(); cascading: prev line failed, so a0 can't be initialized
22 \\ a0.m2(16); cascading: prev line failed, so a0 can't be initialized
23 A b0 = new B();
24 System.out.println(b0.x); [prints "5"]
25 b0.m1(); [prints "Am1-> 5"]
26 b0.m2(); [prints "Bm2-> 5"]
27 \\ b0.m2(61); m2 (int y) not defined in static type of b0
28 B b1 = new B();
29 b1.m2(61); [prints "Bm2y-> 61"]
30 b1.m3(); [prints "Bm3-> called"]
31 A c0 = new C();
32 c0.m2(); [prints "cm2-> 5"]
33 \\ C c1 = (A) new C(); Can't assign c1 to an A
34 A a1 = (A) c0;
35 C c2 = (C) a1;
36 c2.m3(); [print Bm3-> called]
37 \\ c2.m4(); C.m4() is invalid
38 c2.m5(); [print Cm5-> 6]
39 ((C) c0).m3(); [print Bm3-> called]
40 \\ (C) c0.m3(); NOT RUNTIME ERROR This would case the result of what the method returns and
it returns void therefore compile-time error
41 b0.update();
42 b0.m1(); [print Am1-> 99]
43 }
44 }
第四题不会做。