日期:2014-05-20 浏览次数:20773 次
public class Test { public static void main(String[] args) { Dog dog = new Dog(); dog.call(); MiniDog mini = new MiniDog(); mini.call(); Dog d = (Dog) mini; d.call(); } } public class Dog { private String getName(){ return "Dog"; } public String bark(){ return "wang-wang"; } public void call(){ System.out.println("I'm " + getName() + " " + bark()); } } public class MiniDog extends Dog { private String getName(){ return "Mini"; } public String bark(){ return "WOO"; } }
public class MiniDog extends Dog { @Override private String getName(){ return "Mini"; } @Override public String bark(){ return "WOO"; } }
------解决方案--------------------
私有方法不被继承
------解决方案--------------------