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

      对应内存示意图:

    运行结果:

  • 相关阅读:
    Django之DB数据库优化
    whatweb运行流程详解,适用于小白
    阿里云部署Django详细过程
    web指纹识别技术
    whatweb运行原理及各文件的作用详解
    Ruby种的特殊变量
    Ruby正则练习面试题
    centos7安装升级Ruby
    Ruby中的<<和>>的作用详解
    git操作指令合集
  • 原文地址:https://www.cnblogs.com/wanjinliu/p/11001244.html
Copyright © 2011-2022 走看看