1、如下Java代碼:
public class Parent{
public void function(){
System.out.println(“Father.function”);
}
}
public class Child extends Parent{
public void function(){
System.out.println(“Child.function”);
}
}
執行下列程序,結果是什麼?(結果為:Child.function)Parent p = new Child();
p.function();說明:Java中的函數均是虛函數。
2、如下C 代碼:
class Parent{
public: virtual void function(){
cout<<“Father.function”;
}
}
class Child : public Parent{
public: void function(){
cout<<“Child.function”;
}
}
執行下列程序,結果是什麼?(1)當沒有virtual關鍵字時,結果為:Father.function(2)當有virtual關鍵字時,結果為:Child.function(同Java中情況)Parent p = new Child();
p.function();