zoukankan      html  css  js  c++  java
  • person类与其子类在使用中的内存情况(含java的改写和c#的屏蔽)

    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();
            }
        }
    

      对应内存示意图:

    运行结果:

  • 相关阅读:
    【2017-06-20】Linux应用开发工程师C/C++面试问题记录之一:Linux多线程程序的同步问题
    April 14 2017 Week 15 Friday
    April 13 2017 Week 15 Thursday
    April 12 2017 Week 15 Wednesday
    【2017-06-17】QtGui基础控件:QSpinBox及QDoubleSpinBox
    April 11 2017 Week 15 Tuesday
    April 10 2017 Week 15 Monday
    April 9 2017 Week 15 Sunday
    April 8 2017 Week 14 Saturday
    April 7 2017 Week 14 Friday
  • 原文地址:https://www.cnblogs.com/wanjinliu/p/11001244.html
Copyright © 2011-2022 走看看