类一
public class Example1 {
public String getName() {
return "luffyke" ;
}
}
类二
public class Example2 {
public int getAge()
{
return 25 ;
}
} 类三public class MainExample { public class Test1 extends Example1 {
public String getName() {
return super.getName();
}
} public class Test2 extends Example2 {
public int getAge() {
return super.getAge();
}
} public String showName() {
return new Test1().getName();
}
public int showAge() {
return new Test2().getAge();
}//这两行在这里有吗 在我看来好像没什么用啊
public static void main(String args[]) {
MainExample example = new MainExample();
System.out.println("姓名:" + example.showName());//直接调用example.getName()行不行啊
System.out.println("年龄:" + example.showAge());//也是直接调用example.getAge()行不
}
}
public class Example1 {
public String getName() {
return "luffyke" ;
}
}
类二
public class Example2 {
public int getAge()
{
return 25 ;
}
} 类三public class MainExample { public class Test1 extends Example1 {
public String getName() {
return super.getName();
}
} public class Test2 extends Example2 {
public int getAge() {
return super.getAge();
}
} public String showName() {
return new Test1().getName();
}
public int showAge() {
return new Test2().getAge();
}//这两行在这里有吗 在我看来好像没什么用啊
public static void main(String args[]) {
MainExample example = new MainExample();
System.out.println("姓名:" + example.showName());//直接调用example.getName()行不行啊
System.out.println("年龄:" + example.showAge());//也是直接调用example.getAge()行不
}
}
但是要注意,MainExcample并没有得到那两个方法,所以直接example.getName()和example.getAge()是不行的.
这也是为什么要加上showName()和showAge()那两个方法的原因.
首先是MainExample中的showName()和showAge()两个方法调用Test1和Test2中的getName()和getAge()方法
然后Test1和Test2在分别调用父类中的getName()和getAge()
层层递进的关系
这样理解是不是向上转型
这个还说明了内部类的用途..那两句是要的到内部类的getName()和getAge(); MainExcample是没有拿到这两个方法.