2017-10-04 3 views
-3

実行時にオブジェクトがどのように作成されるのか?動的バインディングはどこで行われますか?あなたはこれのためのリアルタイムの例を与えることができますか? オブジェクト型に基づいて実行時にメソッドの実装をメソッド呼び出しにバインドすることがわかっています。実際にそれがどうなるか見たいと思っていますか? 画像は素晴らしいでしょう!動的メソッドのディスパッチ/実行時の多型

はどうもありがとう:)

答えて

0

をこの例あなたは動的バインディングを理解するのに役立ちます。

class Parent{ 
    int value = 10; 

    public void setValue(int value) { 
     this.value = value; 
    } 

    public int getValue() { 
     return value; 
    } 
} 

class Child extends Parent{ 
    int value =20; 

    public void setValue(int value) { 
     this.value = value; 
    } 

    public int getValue() { 
     return value; 
    } 
} 
public class RunTimePolymorphism { 
    public static void main(String[] args) { 
     Child child = new Child(); 
     child.setValue(100); 
     System.out.println(child.getValue()); 

     Child child1 = new Child(); 
     System.out.println(child1.getValue()); 

     Parent parent = new Child(); 
     parent.setValue(200); 
     System.out.println(parent.getValue()); 

     Parent parent1 = new Child(); 
     System.out.println(parent1.getValue()); 


    } 
} 

希望するので、ランタイム多態性の理解に役立ちます。

関連する問題