普通person类及调用代码:
public class Person { public String xm; public int nl; public void setme(String x,int y) { xm=x; nl=y; } public void showme() { System.out.println("我是"+xm+",今年"+nl+"岁"); } } public static void main(String[] args) { // TODO Auto-generated method stub Person a,b; a=new Person(); b=new Person(); a.setme("张三",20); b.setme("李四",18); a.showme(); b.showme(); }
对应内存示意图(不严谨,仅为说明问题,下同)
person类的子类person1及调用代码:
public class Person1 extends Person { public void showme() { System.out.println("我是中国人"+xm+",今年"+nl+"岁"); } } public static void main(String[] args) { // TODO Auto-generated method stub Person1 a; Person b,c; a=new Person1(); b=new Person(); a.setme("张三",20); b.setme("李四",18); c=a; a.showme(); b.showme(); c.showme(); }
对应内存示意图:
运行结果:
原文地址:https://www.cnblogs.com/wanjinliu/p/11001244.html
时间: 2024-10-25 14:37:32