编辑代码

using System;
public class HelloWorld
{

    public static void Main()

    {
        private int myField;    //字段

        public int MyProperty   //属性
        {
            get{return myField;}
            set{myField = value;}
        }


        public int Add(int a=1,int b=1)
        {
            Add = a+b;
            return Add;//方法
        }

        //委托运用
        Transformer t = Square;
        Console.WriteLine($"The result of transformer is {t(3)}");
        t = Cube;
        Console.WriteLine($"The result of transformer is {t(3)}");
        t += Square;
        t(3);

    }


    public class Asset//继承父类
    {
        public string Name;

    }

    public class Stock : Asset   // 继承 from Asset
    {
        public long SharesOwned;

    }


    public class House : Asset   // 继承 from Asset
    {
        public decimal Mortgage;

    }


//多态

    public static void display (Asset asset)
    {
        System.Console.WriteLine (asset.Name);

    }

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

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

    public class House : Asset2   // 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;
    }

    //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]; }
    }

    // 定义委托
    delegate int Transformer (int x);

    static int Square (int x) { 
        int result = x * x;
        Console.WriteLine($"The result of Square is {result}");
        return result; 
    }
    static int Cube (int x) { 
        int result = x * x * x;
        Console.WriteLine($"The result of Cube is {result}");
        return result; 
    }

    
}




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 static void duixiang{
    //对象引用的转换
            Stock1 msft1 = new Stock1();
            Asset asset = msft1;                          // Upcast,隐式向上转换为基类

            Stock msft = new Stock();
            Asset a = msft;                          // Upcast,显示向下转换为子类
            Stock s = (Stock)a;                      // Downcast
            Console.WriteLine (s.SharesOwned);       // <No error>
            Console.WriteLine (s == a);              // True
            Console.WriteLine (s == msft);           // True


}

public class PriceChangedEventArgs : EventArgs
{
    public readonly decimal LastPrice;
    public readonly decimal NewPrice;
    public PriceChangedEventArgs (decimal lastPrice, decimal newPrice)
    {
        LastPrice = lastPrice; NewPrice = newPrice;
    }
}


}