2018年11月8日 星期四

各語言繼承的不同

java 與眾不同

public class T_8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        T8_A a;
        T8_B b;
       
        a = new T8_B();       
        out.println(a.getName());
       
        b = (T8_B)a;
        out.println(b.getName());
    }
   
}

class T8_A{
    String name = "A";
   
    public String getName(){
        return name;
    }
}

class T8_B extends T8_A{
    String name = "B";
}
// 答案都是 A
----------------------------------------------------------------------
<?php

class A {

    public $name = "A";

    public function getName() {
        return $this->name;
    }

}

class B extends A {

    public $name = "B";

}

$b = new B();
printf("%s", $b->getName());

// 答案是 B
----------------------------------------------------------------------
function A(){
    this.name = "A";
}

(function(){
    this.getName = function(){
        return this.name;
    };
}).call(A.prototype);
//-------------------------------
function B(){
    A.call(this);
    this.name = "B";
}
B.prototype = Object.create(A.prototype);
//-------------------------------
console.log((new B()).getName());

// 答案是 B
----------------------------------------------------------------------

class A:
    def __init__(self):
        self.name = "A"

    def getName(self):
        return self.name;

class B(A):
    def __init__(self):
        self.name = "B"

b = B()
print("%s"%b.getName())

// 答案是 B
----------------------------------------------------------------------