记下来只为给自己一个教训:
输出:
Derived tell: null
Derived print: base
Derived tell: derived
Derived print: derived
class Base{
private String name = "base";
public Base() {
tellName();
printName(name);
}
public void tellName(){
System.out.println("Base tell: "+name);
}
public void printName(String name){
System.out.println("Base print: "+name);
}
}
public class Derived extends Base{
private String name = "derived";
public Derived() {
tellName();
printName(name);
}
public void tellName(){
System.out.println("Derived tell: "+name);
}
public void printName(String name){
System.out.println("Derived print: "+name);
}
public static void main(String[] args) {
new Derived();
}
}
输出:
Derived tell: null
Derived print: base
Derived tell: derived
Derived print: derived
本文通过实例分析了Java中类继承与多态的运用,通过具体代码展示了如何实现类之间的继承关系和多态特性,为读者提供了一个实践性的学习案例。
1337

被折叠的 条评论
为什么被折叠?



