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

  • 相关阅读:
    Swift语法基础: 20
    Swift语法基础:19
    Swift语法基础:18
    Swift语法基础:17
    Swift语法基础:16
    Swift语法基础:15
    Swift语法基础:14
    Swift语法基础:13
    Swift语法基础:12
    Swift语法基础:11
  • 原文地址:https://www.cnblogs.com/Excellent/p/1627570.html
Copyright © 2011-2022 走看看