【6月日新计划02】C#入门介绍-類,接口和繼承

200 阅读2分钟

【6月日新计划02】C#入门介绍-類,接口和繼承

1.Class

public class Point 
{ 
    public int X { get; } 
    public int Y { get; } 
    public Point(int x, int y) => (X, Y) = (x, y); 
}

readonly 修饰符声明 只读字段。 只能在字段声明期间或在同一个类的构造函数 中向只读字段赋值。

public class Color 
{ 
    public static readonly Color Black = new Color(0, 0, 0); 
    public static readonly Color White = new Color(255, 255, 255); 
    public static readonly Color Red = new Color(255, 0, 0); 
    public static readonly Color Green = new Color(0, 255, 0); 
    public static readonly Color Blue = new Color(0, 0, 255); 
    
    public byte R; 
    public byte G; 
    public byte B; 
    public Color(byte r, byte g, byte b) 
    { 
        R = r; 
        G = g; 
        B = b; 
    } 
}

2.Enum

public enum SomeRootVegetable 
{ 
    HorseRadish, 
    Radish, 
    Turnip 
}
public enum Seasons
{ 
    None = 0, 
    Summer = 1, 
    Autumn = 2, 
    Winter = 4, 
    Spring = 8, All = Summer | Autumn | Winter | Spring 
 } 
 
 var turnip = SomeRootVegetable.Turnip; 
 var spring = Seasons.Spring; 
 var startingOnEquinox = Seasons.Spring | Seasons.Autumn; 
 var theYear = Seasons.All;

3.Interface

多重继承

interface IControl 
{ 
    void Paint();
} 

interface ITextBox : IControl 
{ 
    void SetText(string text);
} 

interface IListBox : IControl 
{ 
    void SetItems(string[] items); 
} 

interface IComboBox : ITextBox, IListBox 
{
    。。。。
}

重写方法

public abstract class Expression 
{ 
    public abstract double Evaluate(Dictionary vars);
} 

public class Constant : Expression 
{ 
    double _value; 
    public Constant(double value) 
    { 
        _value = value; 
    } 
    public override double Evaluate(Dictionary vars) 
    { 
        return _value; 
    } 
} 

public class VariableReference : Expression 
{ 
    string _name; public VariableReference(string name) 
    { 
        _name = name; 
    } 
    public override double Evaluate(Dictionary vars) 
    { 
        object value = vars[_name] ?? throw new Exception($"Unknown variable: {_name}"); 
        return Convert.ToDouble(value); 
    } 
} 

public class Operation : Expression 
{ 
    Expression _left; 
    char _op; 
    Expression _right; 
    
    public Operation(Expression left, char op, Expression right) 
    { 
        _left = left; 
        _op = op; 
        _right = right; 
    } 
    
    public override double Evaluate(Dictionary vars) 
    { 
        double x = _left.Evaluate(vars); 
        double y = _right.Evaluate(vars); 
        switch (_op) 
        { 
            case '+': return x + y; 
            case '-': return x - y; 
            case '*': return x * y; 
            case '/': return x / y; 
            default: throw new Exception("Unknown operator"); 
        } 
    } 
}

Expression e = new Operation( new VariableReference("x"), '+', new Constant(3));

方法重载

class OverloadingExample 
{ 
    static void F() => Console.WriteLine("F()"); 
    static void F(object x) => Console.WriteLine("F(object)"); 
    static void F(int x) => Console.WriteLine("F(int)"); 
    static void F(double x) => Console.WriteLine("F(double)"); 
    static void F(T x) => Console.WriteLine("F(T)"); 
    static void F(double x, double y) => Console.WriteLine("F(double, double)"); 
    
    public static void UsageExample() 
    { 
        F();  // Invokes F() 
        F(1); // Invokes F(int) 
        F(1.0); // Invokes F(double) 
        F("abc"); // Invokes F<string>(string) 
        F((double)1); // Invokes F(double) 
        F((object)1); // Invokes F(object) 
        F<int>(1); // Invokes F<int>(int) 
        F(1, 1); // Invokes F(double, double) } }

4.Modifier

  • public :访问不受限制。
  • private :访问仅限于此类。
  • protected :访问仅限于此类或派生自此类的类。
  • internal :仅可访问当前程序集( .exe 或 .dll )。
  • protected internal :仅可访问此类、从此类中派生的类,或者同一程序集中的类。
  • private protected :仅可访问此类或同一程序集中从此类中派生的类。

5.Other Function

5.1 Constructed Function

get 访问器读取该值。 set 访问器写入该值。

public class MyList<T> 
{ 
    const int DefaultCapacity = 4; 
    T[] _items; 
    int _count; 
    
    public MyList(int capacity = DefaultCapacity) 
    { 
        _items = new T[capacity]; 
    } 
    
    public int Count => _count; 
    public int Capacity 
    { 
        get => _items.Length; 
        set 
        { 
            if (value < _count) value = _count;
            if (value != _items.Length) 
            { 
                T[] newItems = new T[value]; 
                Array.Copy(_items, 0, newItems, 0, _count); 
                _items = newItems; 
            } 
        } 
    } 
    
    public T this[int index] 
    { 
        get => _items[index]; 
        set 
        { 
            _items[index] = value; 
            OnChanged(); 
        } 
    } 
    
    public void Add(T item) 
    { 
        if (_count == Capacity) Capacity = _count * 2; 
        _items[_count] = item; 
        _count++; 
        OnChanged(); 
    } 
    
    protected virtual void OnChanged() => Changed?.Invoke(this, EventArgs.Empty); 
    
    public override bool Equals(object other) => Equals(this, other as MyList); 
    
    static bool Equals(MyList a, MyList b) 
    { 
        if (Object.ReferenceEquals(a, null)) return Object.ReferenceEquals(b, null); 
        if (Object.ReferenceEquals(b, null) || a._count != b._count) 
            return false; 
        for (int i = 0; i < a._count; i++) 
        { 
            if (!object.Equals(a._items[i], b._items[i])) 
            { 
                return false;
            } 
        } 
        return true; 
    } 
    
    public event EventHandler Changed; 
    
    public static bool operator ==(MyList a, MyList b) => Equals(a, b);
    
    public static bool operator !=(MyList a, MyList b) => !Equals(a, b); 
}