zoukankan      html  css  js  c++  java
  • [think in java] 第8章 多态

    多态

    “封装”通过合并特征和行为来创建新的数据类型。

    “多态”的作用则是消除类型之间的耦合关系。


    方法调用绑定

    定义:将一个方法调用同一个方法主题关联起来被称为绑定。

    若在程序执行前进行绑定(假设有的话,由编译器和链接程序实现),叫前期绑定。在执行时依据对象的类型进行绑定称为后期绑定,或执行时绑定。java中的多态就是通过动态绑定实现的。

    java中除了static方法和final方法(private 方法属于final方法)之外,其它全部方法都是后期绑定。


    缺陷:域与静态方法

    一旦你了解了多态机制,你可能会觉得全部事物都是能够多态的发生的。然而,仅仅有普通的方法调用是能够多态的。假设你訪问某个域field,这个訪问将在编译期进行解析。看以下的样例:

    class Super{
    		public int filed = 0;
    		public int getFiled(){
    			return filed;
    		}
    	}
    	
    	class Sub extends Super{
    		public int filed = 1;
    		public int getFiled(){
    			return filed;
    		}
    		
    		public int getSuperFiled(){
    			return super.filed;
    		}
    	}
    public class FiledAccess {
    	
    	
    	
    		public static void main(String [] agrs){
    			Super sup = new Sub();
    			System.out.println("sup.field = " + sup.filed + ", sup.getField() = "+
    			sup.getFiled());
    			
    			
    			Sub sub = new Sub();
    			System.out.println("sub.field = " + sub.filed + ", sub.getField() = "+
    					sub.getFiled() + ", sub.getSuperField() = " + sub.getSuperFiled());
    			
    	}
    
    }
    sup.field = 0, sup.getField() = 1
    sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0
    

    当Sub转型为Super引用时,不论什么域訪问操作都将由编译器解析,因此不是多态的。在本例中,为Super.field和Sub.field分配了不同的存储空间。这样,Sub实际上包括两个成为field的域:他自己的和它从Super处得到的。然而在引用Sub中的filed时所产生的默认域并不是Super版本号的field域。因此,为了得到Super.filed,必须显示的指明super.field。

    这个问题看起来非常easy令人混淆,可是实际中却并不常发生。由于依据设计原则,我们经常将全部的域设置为private的,因此不能直接訪问他们,仅仅能通过方法来訪问。

    总结:方法多态,域不多态訪问。


    静态方法相同不具有多态性。由于静态方法是与类相关联的,而不是与单个对象相关联。看以下的样例消化一下^.^~:

    class StaticSuper{
    	public static String staticGet(){
    		return "Base staticGet()";
    	}
    	
    	public String dynamicGet(){
    		return "Base dynamicGet()";
    	}
    }
    
    class StaticSub extends StaticSuper{
    	public static String staticGet(){
    		return "Derived staticGet()";
    	}
    	
    	public String dynamicGet(){
    		return "Derived dynamicGet()";
    	}
    }
    public class StaticPolymorphism {
    	public static void main(String [] args){
    		StaticSuper sup = new StaticSub();
    		System.out.println(sup.staticGet());
    		System.out.println(sup.dynamicGet());
    	}
    }
    
    output:
    Base staticGet()
    Derived dynamicGet() 
    

    參考:《thinking in java》

  • 相关阅读:
    js跨域之window.name跨域解决方案
    html中用IE条件判断
    收集的CSS浏览器兼容问题大全(粘贴过来,会用到的)
    移动网站开发系列(一)
    离散数学归谬法与反证法的比较
    端口未正常关闭解决方法
    nodejs 连接 mysql 出现 Client does not support authentication protocol requested by server; consider upgrading MySQL client
    http规范
    退役后的苦逼高三生活续++
    NOIP2018 游记
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4296607.html
Copyright © 2011-2022 走看看