多态

多态      函数体现

如方法的重载

多态      对象体现

how

1 classextends 动物{public void 吃(){}}
2 动物 D=new 猫();
3 D.吃();
扩展:据此增强扩展性
1
abstract class Animal 2 { 3 abstract void eat(); 4 } 5 6 class Cat extends Animal 7 { 8 public void eat(){ 9 System.out.println("吃鱼"); 10 } 11 } 12 13 class Dog extends Animal 14 { 15 public void eat(){ 16 System.out.println("吃骨头"); 17 } 18 } 19 20 class DuoTai 21 { 22 public static void main(String[] args) 23 { 24 /* 25 Cat c=new Cat(); 26 c.eat(); 27 Dog d=new Dog(); 28 d.eat(); 29 */ 30 //改进版,以后要是有pig也是不怕的 31 duoTaiEat(new Cat()); 32 duoTaiEat(new Dog()); 33 } 34 public static void duoTaiEat(Animal a){ 35 a.eat(); 36 } 37 }

 

 

存在问题:增加一个Pig,岂不是要多写一句duoTaiEat(new Pig());???      反射!!!

上转型与下转型

上转型:Animal a=new Cat();//a可以调用父类中声明过的方法,不能调用Cat的特有方法

下转型:Cat c=(Cat)a;//原本a为上转型对象了,现在强制下转型,c可以掉用Cat的特有方法

 1 /**
 2 *定义抽象类Animal,具有抽象方法eat()
 3 *Cat和Dog继承Animal且均具有各自特有的方法
 4 */
 5 abstract class Animal
 6 {
 7     abstract void eat();
 8 }
 9 
10 class Cat extends Animal
11 {
12     public void eat(){
13         System.out.println("猫吃鱼");
14     }
15     public void catchMouse(){
16         System.out.println("猫抓老鼠");
17     }
18 }
19 class Dog extends Animal
20 {
21     public void eat(){
22         System.out.println("狗啃骨头");
23     }
24     public void kanJia(){
25         System.out.println("狗看家");
26     }
27 }
28 
29 class ZhuanXing 
30 {
31     public static void function(Animal a){
32         //动物都具有eat(),当传递进来Cat的时候,相当于Animal a=new Cat();上转型
33         a.eat();
34         //instanceof判断一个对象是否是某一个类的实例
35         if(a instanceof Cat){
36             Cat c=(Cat)a;//强制下转型
37             c.catchMouse();
38         }
39         else if(a instanceof Dog){
40             Dog d=(Dog)a;
41             d.kanJia();
42         }
43         
44     }
45     public static void main(String[] args) 
46     {
47         function(new Cat());
48         function(new Dog());
49 
50     }
51 }

 注意:

  1.上面这种做法明显有弊端,当出现的子类越来越多的时候,必须要不停的去判断,这里只是顺便展示一下instanceof的用法

  2.当然,在子类有限且很少的情况下可以这么去做,比如说人分为男人和女人的时候

多态定义工具类应用

多态的注意事项

posted @ 2015-07-30 15:14  洱海  阅读(222)  评论(0编辑  收藏  举报
.First { margin: 10px 0; font-family: 'Microsoft Yahei'; text-align: left; padding: 6px 20px; color: #fff; background: #55895B; font-size: 20px; border-radius: 4px; clear: both; } .Second { margin: 10px 0; font-family: 'Microsoft Yahei'; padding: 6px 20px; background: #93C8A2; color: white; font-size: 18px; border-radius: 4px; clear: both; } .Third { margin: 10px 0; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0; font-size: 16px; color: black; background: #C6EFD2; border-radius: 4px; clear: both; } .note { margin: 10px 0; padding: 15px 20px 15px 60px; background: #FCFAA9 url('http://images.cnblogs.com/cnblogs_com/libaoheng/305804/o_yellow-pin.png') no-repeat 20px 0; font-size: 15px; font-family: 'Microsoft Yahei'; box-shadow: 0 0 8px #aaa; clear: both; } .demo { text-align: left; padding: 6px 20px; overflow: auto; border-radius: 4px; background: orange; color: #fff; font-size: 16px; clear: both; } .cnblogs_Highlighter { border: solid 1px #ccc; clear: both; } .cnblogs_code { background: #EFFFF4; border: solid 0px #939393; font-size: 14px; clear: both; padding: 10px 20px; } .cnblogs_code pre { font-size: 14px; } .cnblogs_code span { font-family: Courier New; font-size: 14px; }