1. 程式人生 > >jvm視角看java繼承和多型

jvm視角看java繼承和多型

複製程式碼
package test.xing;

class Father{
    protected int age;
    public Father(){
        age = 40;
    }
    
    void eat(){
        System.out.println("父親在吃飯");
    }
}
class Child extends Father{
    protected int age;
    public Child(){
        age = 18;
    }
    
    void eat(){
        System.out.println(
"孩子在吃飯"); } void play(){ System.out.println("孩子在打CS"); } } public class TestPolymorphic { public static void main(String[] args) { Father c = new Child(); c.eat(); //c.play(); System.out.println("年齡:"+c.age ); } }
複製程式碼