`

关于父类和子类

    博客分类:
  • Java
 
阅读更多
class Father{ 
void print(){
	
	System.out.println ("父类的哦");
	}; 

}

class Son extends Father{ 
    void print(){
    	System.out.println("子类中!");//重写父类的方法(将会覆盖父类的方法)
    	} 
     void show(){
     	System.out.println("show 中!");
     	} 
} 
class Demo{ 

    public static void main(String args[]){ 
     
    // Father  obj = new Father();
     Father objFat  = new Son(); //父类的对象指向子类
       Son  objSon  =new Son();//子类的对象指向子类
     objFat.print();//子类重写了父类的方法调用的是子类的方法(也是子类方法覆盖了父类的方法) 
     //objFat.show();//编译有错误(父类的对象调用子类的方法) 
     objSon.show();//子类的对象调用子类的方法;
  } 
} 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics