多态一

前面的文章简单的分析了动态绑定机制, http://blog.itpub.net/29876893/viewspace-1816523/,但是也许有很多疑问,下面介绍下子类如何调用自己的方法或我们阻止多态。
修改的代码如下:

点击(此处)折叠或打开

  1. public class Animal {
  2.     
  3.     public String name;
  4.     
  5.     Animal(String name){
  6.         
  7.         this.name = name;
  8.     }
  9.    
  10.     public void enjoy(){
  11.         
  12.         System.out.println("叫声...");
  13.     }

  14. }

  15. public class Cat extends Animal {
  16.     
  17.     private String eyesColor;
  18.     
  19.     Cat(String n,String c){
  20.             
  21.             super(n);
  22.             eyesColor = c;
  23.         }
  24.     
  25.      public void enjoy(){
  26.         
  27.          System.out.println("猫叫声...");
  28.      }
  29.     
  30.      public void catcolor(){
  31.         
  32.          System.out.println(eyesColor);
  33.      }
  34.     }


  35. public class Dog extends Animal{
  36.   
  37.     private String furColor;
  38.     
  39.     Dog(String n,String c){
  40.         
  41.         super(n);
  42.         furColor = c;
  43.     }
  44.     
  45.     public void enjoy(){
  46.         
  47.         System.out.println("狗叫声。。。");
  48.     }
  49.     
  50.     public void dogfurcolor(){
  51.         
  52.         System.out.println(furColor);
  53.     }
  54. }


  55. public class Test {
  56.    
  57.      public void cataddr(Animal per){
  58.     
  59.       System.out.print(per.name + " ");
  60.     
  61.      if(per instanceof Cat){
  62.         
  63.         Cat cat = (Cat)per;
  64.         
  65.         cat.catcolor();;
  66.         
  67.     }else if(per instanceof Dog){
  68.         
  69.         Dog dog = (Dog)per;
  70.         
  71.          dog.dogfurcolor();
  72.     }
  73.     else{
  74.         
  75.         System.out.println("空值");
  76.     }
  77. }

  78.     public static void main(String[] args){
  79.         
  80.         Test w = new Test();
  81.         Cat c = new Cat("catname", "bule");
  82.         Dog d = new Dog("dogname", "black");
  83.         w.cataddr(c);
  84.         w.cataddr(d);
  85.      
  86.     }
  87. }






上面在Cat,Dog中添加了新的方法,如果用Animal的引用调用这些方法,编译器会提错,那如果真想调用基类的自定义的方法,则需要强制转换,把Animal 引用强制转换成Cat,Dog类型,为了防止出现异常,我们用instanceof来检查,返回布尔类型,具体用法:


boolean result = object instanceof class
Result:布尔类型。
Object:必选项。任意对象表达式。
class:必选项。任意已定义的对象类。
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。


我们通过instanceof的检查后,基类可以调用自定义的方法


如果把父类中的enjoy()定义为static,private,final,那么就不会出现多态性,方法被定义指定为private隐式的定义为final,类的设计者不希望该方法被继承修改。被指明为static时,那么这个方法就是类的通性,不会随对象的改变而发生变化,那么也失去了多态。关于这方面,读者可以参考<>第八章和<>第五章,有很详细的介绍。
下面给出一个小例子:


点击(此处)折叠或打开

  1. class t {
  2.    
  3.   private void f(){
  4.     
  5.      System.out.println("diyjie");
  6.   }
  7.   public static void main(String[] args){
  8.     
  9.      t w = new t1();
  10.      w.f();
  11.  }
  12. }

  13. public class t1 extends t{
  14.     
  15.     public void f(){
  16.         
  17.         System.out.println("os");
  18.     }
  19.     
  20. }

我们想出现的结果是os,但是结果是lios,因为父类中的f()方法在子类中不可见,不会出现多态机制,如果清楚多态机制的话,我想原因一目了然。

修改上述代码:

点击(此处)折叠或打开

  1. class t {
  2.    
  3.   public int i = 0;
  4.   
  5.   public void f(){
  6.     
  7.      System.out.println("lios");
  8.   }
  9. }

  10. public class t1 extends t{
  11.     
  12.     public int i =1;
  13.     
  14.     public void geti(){
  15.         
  16.         System.out.print(super.i);
  17.     }
  18.     public void f(){
  19.         
  20.         System.out.println("os");
  21.     }
  22.     
  23.     public static void main(String[] args){
  24.         
  25.         t w = new t1();
  26.         System.out.print(w.i + " ");
  27.         w.f();
  28.         
  29.         t1 w1 = new t1();
  30.         System.out.print(w1.i +" "+"super.i=");
  31.         w1.geti();
  32.         
  33.     }
  34.     
  35. }
输出结果:
0 os
1 super.i=0
可能对于为什么打印 w . i为0,因为在在new子类对象时,堆内分配两个不同地址的i,一个是从父类继承的i,一个是基类中的i,此时父类的引用只指向自己的对象中的成员变量,所以结果如上。其他的都很简单。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1816807/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29876893/viewspace-1816807/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值