java8-4 多态的练习以及题目

1、
/*
多态练习:猫狗案例
*/

 1 class Animal {
 2 public void eat(){
 3 System.out.println("吃饭");
 4 }
 5 }
 6 
 7 class Dog extends Animal {
 8 public void eat() {
 9 System.out.println("狗吃肉");
10 }
11 
12 public void lookDoor() {
13 System.out.println("狗看门");
14 }
15 }
16 
17 class Cat extends Animal {
18 public void eat() {
19 System.out.println("猫吃鱼");
20 }
21 
22 public void playGame() {
23 System.out.println("猫捉迷藏");
24 }
25 }
26 
27 class DuoTaiTest {
28 public static void main(String[] args) {
29 //定义为狗
30 Animal a = new Dog();
31 a.eat();
32 System.out.println("--------------");
33 //还原成狗
34 Dog d = (Dog)a;
35 d.eat();
36 d.lookDoor();
37 System.out.println("--------------");
38 //变成猫
39 a = new Cat();
40 a.eat();
41 System.out.println("--------------");
42 //还原成猫
43 Cat c = (Cat)a;
44 c.eat();
45 c.playGame();
46 System.out.println("--------------");
47 
48 //演示错误的内容
49 //Dog dd = new Animal();
50 //Dog ddd = new Cat();
51 //ClassCastException
52 //Dog dd = (Dog)a;
53 }
54 }

 

2、不同地方饮食文化不同的案例

 1 class Person {
 2 public void eat() {
 3 System.out.println("吃饭");
 4 }
 5 }
 6 
 7 class SouthPerson extends Person {
 8 public void eat() {
 9 System.out.println("炒菜,吃米饭");
10 }
11 
12 public void jingShang() {
13 System.out.println("经商");
14 }
15 }
16 
17 class NorthPerson extends Person {
18 public void eat() {
19 System.out.println("炖菜,吃馒头");
20 }
21 
22 public void yanJiu() {
23 System.out.println("研究");
24 }
25 }
26 
27 class DuoTaiTest2 {
28 public static void main(String[] args) {
29 //测试
30 //南方人
31 Person p = new SouthPerson();
32 p.eat();
33 System.out.println("-------------");
34 SouthPerson sp = (SouthPerson)p;
35 sp.eat();
36 sp.jingShang();
37 System.out.println("-------------");
38 
39 //北方人
40 p = new NorthPerson();
41 p.eat();
42 System.out.println("-------------");
43 NorthPerson np = (NorthPerson)p;
44 np.eat();
45 np.yanJiu();
46 }
47 }

 

题目:

1、看程序写结果:先判断有没有问题,如果没有,写出结果

 1 class Fu {
 2 public void show() {
 3 System.out.println("fu show");
 4 }
 5 }
 6 
 7 class Zi extends Fu {
 8 public void show() {
 9 System.out.println("zi show");
10 }
11 
12 public void method() {
13 System.out.println("zi method");
14 }
15 }
16 
17 class DuoTaiTest3 {
18 public static void main(String[] args) {
19 Fu f = new Zi();
20 f.method();
21 f.show();
22 }
23 } 

答案是:  出错,f.method()这里出错,父类没有这个方法

2、看程序写结果:先判断有没有问题,如果没有,写出结果

 1 class A {
 2 public void show() {
 3 show2();
 4 }
 5 public void show2() {
 6 System.out.println("我");
 7 }
 8 }
 9 class B extends A {
10 public void show2() {
11 System.out.println("爱");
12 }
13 }
14 class C extends B {
15 public void show() {
16 super.show();
17 }
18 public void show2() {
19 System.out.println("你");
20 }
21 }
22 public class DuoTaiTest4 {
23 public static void main(String[] args) {
24 A a = new B();
25 a.show();
26 
27 B b = new C();
28 b.show();
29 }
30 }

 

//答案是 爱你 。
public void show() {
show2();
}   默认在B类的show2前面

多态的成员访问特点:
方法:编译看左边,运行看右边。

继承的时候:
子类中有和父类中一样的方法,叫重写。
子类中没有父亲中出现过的方法,方法就被继承过来了。


posted @ 2016-09-10 09:50  卡拉瓦  阅读(7982)  评论(1编辑  收藏  举报