编辑代码

using System;

namespace ClassLearning{
    //字段与属性
    public class Stock
    {
        private decimal currentPrice;     // The private "backing" field

        public decimal CurrentPrice       // The public property
        {
            get { return currentPrice; }
            set { currentPrice = value; }
        }

        //方法与方法签名
        public decimal calculatePrice(decimal increasement) {
            currentPrice += increasement;
            return currentPrice;
        }

        public decimal calculatePrice(int decreasement) {
            currentPrice -= decreasement;
            return currentPrice;
        }

    }

    
    class Rectangle
    {
        public readonly float Width, Height;
        public Rectangle (float width, float height)
        {
            Width = width;
            Height = height;
        }
        //解构器
        public void Deconstruct (out float width, out float height)
        {
            width = Width;
            height = Height;
        }
    }

    public class Bunny
    {
        public string Name;
        public bool LikesCarrots;
        public bool LikesHumans;

        public Bunny () {}
        public Bunny (string n) { Name = n; }
    }

    //this
    public class Test
    {
        string name;
        public Test (string name) { this.name = name; }
    }


    //静态构造器与静态字段的初始化顺序
    class Foo
    {
        public static Foo Instance = new Foo();
        public static int X = 3;

        static Foo() { Console.WriteLine ($"I'm in static constructor with {X}"); }   
        Foo() { Console.WriteLine ($"I'm in constructor with {X}"); }    // 0

        ~Foo() => Console.WriteLine ("Foo Finalizing");
    }

    //继承
    public class Asset
    {
        public string Name;
        //虚函数
        public virtual decimal Liability => 0;   // Expression-bodied property
    }

    public class Stock1 : Asset   // inherits from Asset
    {
        public long SharesOwned;
    }

    public class House : Asset   // inherits from Asset
    {
        public decimal Mortgage;
        public override decimal Liability => Mortgage;
    }

    //抽象类
    public abstract class AbAsset
    {
        // Note empty implementation
        public abstract decimal NetValue { get; }
    }

    public class RealStock : AbAsset
    {
        public long SharesOwned;
        public decimal CurrentPrice;
        // Override like a virtual method.
        public override decimal NetValue => CurrentPrice * SharesOwned;
    }

    // 密封函数
    // public  class SealedStock : AbAsset
    // {
    //     public long SharesOwned;
    //     public decimal CurrentPrice;
    //     // Override like a virtual method.
    //     public override decimal NetValue => CurrentPrice * SharesOwned;
    // }

    // public  class StockInerit : SealedStock
    // {
    //     public long SharesOwned;
    //     public decimal SealedPrice;
    //     // Override like a virtual method.
    //     public override decimal NetValue => SealedPrice * SharesOwned;
    // }
    
    //构造器和继承
    public class BaseClass
    {
        public int X;
        public BaseClass () { }
        public BaseClass (int x) { 
            Console.WriteLine($"BaseClass {X}");
            this.X = x; 
            
        }
    }
    public class SubClass : BaseClass { }

    public class SubClass1 : BaseClass
    {
        public SubClass1 (int x) : base (x) { 
            Console.WriteLine($"SubClass1 {X}");
        }
    }

    //object类型
    public class Stack
    {
        int position;
        object[] data = new object[10];
        public void Push (object obj)   { data[position++] = obj;  }
        public object Pop()               { return data[--position]; }
    }
    

    public class HelloClass
    {
        // Cube是WriteCube的局部方法
        public static void WriteCubes()
        {
            Console.WriteLine (Cube (3));
            Console.WriteLine (Cube (4));
            Console.WriteLine (Cube (5));

            int Cube (int value) => value * value * value;
        }

        //多态
        public static void display (Asset asset)
        {
          System.Console.WriteLine (asset.Name);
        }

        public static void Main()
        {
            Stock stockA = new Stock();

            stockA.CurrentPrice = 2.0M;

            Console.WriteLine($"The price of stockA is {stockA.CurrentPrice}");

            WriteCubes();

            //解构器
            var rect = new Rectangle (3, 4);
            (float width, float height) = rect;            // Deconstruction
            Console.WriteLine (width + " " + height);      // 3 4

            //对象初始化器
            // Note parameterless constructors can omit empty parentheses
            Bunny b1 = new Bunny { Name="Bo", LikesCarrots=true, LikesHumans=false };
            Console.WriteLine(b1.Name);
            Bunny b2 = new Bunny ("Bob")      { LikesCarrots=true, LikesHumans=false };
            Console.WriteLine(b2.Name);

            Foo test = Foo.Instance;

            Console.WriteLine($"GetType of Foo is {test.GetType()}.");
            Console.WriteLine($"nameof of test is {nameof(test)}.");


            Stock1 msft = new Stock1 { Name="MSFT",
                            SharesOwned=1000 };

            Console.WriteLine (msft.Name);           // MSFT
            Console.WriteLine (msft.SharesOwned);    // 1000
            House mansion = new House { Name="Mansion",
                                Mortgage=250000 };

            Console.WriteLine (mansion.Name);        // Mansion
            Console.WriteLine (mansion.Mortgage);    // 250000

            //多态
            display(msft);
            display(mansion);
            
            //对象引用的转换
            Stock1 msft1 = new Stock1();
            Asset asset = msft1;                          // Upcast
            Stock1 stock = (Stock1)asset;                 // Downcast
            Console.WriteLine (stock.SharesOwned);       // <No error>
            Console.WriteLine (stock == asset);              // True
            Console.WriteLine (stock == msft1);           // True

            //as运算符
            Asset a1 = new Asset();
            Stock1 s1 = a1 as Stock1;        // s is null; no exception thrown
            try{
                Stock1 s2 = (Stock1)a1;
            }
            catch(Exception ex) {
               // Console.WriteLine(ex);
            }

            // is运算符
            if (asset is Stock1 s3){
                Console.WriteLine (s3.SharesOwned);
            }

            House mansion1 = new House { Name="McMansion", Mortgage=250000 };
            Asset a2 = mansion1;
            Console.WriteLine (mansion1.Liability);  // 250000
            Console.WriteLine (a2.Liability);          // 250000

            //AbAsset abA = new AbAsset();
            // AbAsset abA = new RealStock {
            //     SharesOwned = 100,
            //     CurrentPrice = 250000
            // };
            // Console.width(abA.NetValue);


            //构造器与顺序
            // s = new Subclass (123);
            SubClass1 sub = new SubClass1 (123);

            //object
            Stack stack = new Stack();
            stack.Push ("sausage");
            string s = (string) stack.Pop();    // Downcast, so explicit cast is needed
            stack.Push (3);
            int three = (int) stack.Pop();

            Console.WriteLine (s);              // sausage
            Console.WriteLine (three);              // 3
                
        }
    }
}