zoukankan      html  css  js  c++  java
  • C#你真的懂了吗 啥叫引用2

    看看下面的这段代码,如果你得到的结果和实际情况一致,那就是明白引用类型了

        class Program
        {
            static void Main(string[] args)
            {
                Car theCar = new Car();
                TestClass(theCar);
                Console.WriteLine(theCar.Name);
                TestClass2(theCar);
                Console.WriteLine(theCar.Name);
                TestClass3(theCar);
                Console.WriteLine(theCar.Name);
                Console.ReadLine();
    
            }
    
            public static void TestClass(Car mycar)
            {
                mycar.Name = "aaa";
                mycar.Number = 100;
                mycar.ProductArea = "china";
            }
    
            public static void TestClass2(Car mycar)
            {
                Car thiscar = mycar;
                thiscar.Name = "bbb";
                thiscar.Number = 200;
                thiscar.ProductArea = "Japan";
            }
    
            public static void TestClass3(Car mycar)
            {
                Car thiscar = new Car(mycar);
                thiscar.Name = "ccc";
                thiscar.Number = 300;
                thiscar.ProductArea = "Korea";
            }
        }

        class Car
        {
            public string Name; 
            public string ProductArea;
            public int Number;
    
            public Car(Car thecar)
            {
                this.Name = thecar.Name;
                this.Number = thecar.Number;
                this.ProductArea = thecar.ProductArea;
            }
            public Car()
            {
                this.Name = null;
                this.Number = 0;
                this.ProductArea = null;
            }
        }

    得到的结果是:

    aaa
    bbb
    bbb

  • 相关阅读:
    JVM类加载的过程
    接口文档设计
    代码规范及CodeReview要点
    Linux权限
    Linux文件
    UltraEdit编辑器基础技巧
    Android环境配置
    JDK 环境配置
    xml没有提示解决办法<eclipse>
    mysql事务实例
  • 原文地址:https://www.cnblogs.com/Excellent/p/1627570.html
Copyright © 2011-2022 走看看