JAVA 普通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(); }
C# 普通person类及调用代码:
class Person { public String xm; public int nl; public void setme(String x, int y) { xm = x; nl = y; } public void showme() { Console.WriteLine("我是" + xm + ",今年" + nl + "岁"); } } class Program { static void Main(string[] args) { Person a, b; a = new Person(); b = new Person(); a.setme("张三", 20); b.setme("李四", 18); a.showme(); b.showme(); Console.ReadKey(); } }
对应内存示意图(不严谨,仅为说明问题,下同)
JAVA 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(); }
对应内存示意图:
运行结果:
C# person类的子类person1及调用代码:
class Person1 : Person { new public void showme()//没有new会报一个警告错误,不影响运行结果。以后会讲到。 { Console.WriteLine("我是中国人" + xm + ",今年" + nl + "岁"); } } class Program { static void Main(string[] args) { 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(); Console.ReadKey(); } }
对应内存示意图:
运行结果: