首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

关于父类跟子类

2012-12-18 
关于父类和子类class Father{ void print(){System.out.println (父类的哦)} }class Son extends Fath

关于父类和子类

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();//子类的对象调用子类的方法;  } } 

热点排行