zoukankan      html  css  js  c++  java
  • C# 语法练习(11): 类[三] 构造函数、析构函数、base、this


    构造函数与析构函数:
    using System;
    
    class MyClass
    {
        private int FNum;
        public int Num { get { return FNum; } }
    
        /* 构造函数没有返回值, 无参的构造函数是默认的 */
        public MyClass()
        {
            this.FNum = 2009;
        }
    
        /* 可以有多个参数不同的构造函数 */
        public MyClass(int x)
        {
            this.FNum = x;
        }
    
        public MyClass(params int[] arr)
        {
            foreach (int i in arr) this.FNum += i;
        }
    
        /* 析构函数无参、无返回值、无访问修饰, 最多只能有一个 */
        ~MyClass()
        {
            //析构函数是自动调用的
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass obj1, obj2, obj3;
    
            obj1 = new MyClass();
            Console.WriteLine(obj1.Num); //2009
    
            obj2 = new MyClass(100);
            Console.WriteLine(obj2.Num); //100
    
            obj3 = new MyClass(1, 2, 3);
            Console.WriteLine(obj3.Num); //6
    
            Console.ReadKey();
        }
    }
    

    如果没有构造与析构函数, new 时将使用默认(或继承); 给一个私有的构造函数能阻止类被实例化:
    using System;
    
    class MyClass
    {
        private MyClass() { }
        public static void Msg1() { Console.WriteLine("Msg1"); }
        public static void Msg2() { Console.WriteLine("Msg2"); }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass.Msg1(); //Msg1
            MyClass.Msg2(); //Msg2
    
            Console.ReadKey();
        }
    }
    

    如果一个类有了非默认的构造函数, 就不能再使用默认的构造函数:
    using System;
    
    class MyClass
    {
        private int FNum;
        public int Num { get { return FNum; } }
    
        public MyClass(int x, int y)
        {
            this.FNum = x + y;
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass obj;
    
            obj = new MyClass(1, 2);
            Console.WriteLine(obj.Num); //3
    
            Console.ReadKey();
        }
    }
    

    静态构造函数:

    静态构造函数既无访问修饰符、无参数;
    在 new 或调用任何静态成员之前,将自动调用静态构造函数;
    静态构造函数一般用于初始化静态数据;
    静态构造函数会在第一次 new 或第一次使用静态成员前触发;
    不能直接调用静态构造函数.
    using System;
    
    class MyClass
    {
        public static int Num;
        public static void ShowNum() { Console.WriteLine(Num); }
        public void Msg() { Console.WriteLine("Msg"); }
    
        static MyClass() { Num = 123; }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass.ShowNum();            //123
            MyClass.Num = 2009;
            MyClass.ShowNum();            //2009
    
            MyClass obj1 = new MyClass();
            obj1.Msg();                   //Msg
    
            Console.ReadKey();
        }
    }
    

    自动调用父类的构造方法:
    using System;
    
    class Parent
    {
        public Parent() { Console.WriteLine("Parent"); }
    }
    
    class Child1 : Parent
    {
        public Child1() { Console.WriteLine("Child1"); }
        public Child1(int x) { Console.WriteLine(x); }
    }
    
    class Child2 : Child1
    {
        public Child2() { Console.WriteLine("Child2"); }
        public Child2(int x, int y) { Console.WriteLine(x + y); }
    }
    
    class Program
    {
        static void Main()
        {
            Parent p = new Parent();           // Parent
            Child1 c1 = new Child1();          // Parent / Child1
            Child2 c2 = new Child2();          // Parent / Child1 / Child2
    
            Child1 c11 = new Child1(999);      // Parent / 999
            Child2 c22 = new Child2(111, 222); // Parent / Child1 / 333
            
            Console.ReadKey();
        }
    }
    

    base:
    using System;
    
    class Parent
    {
        public Parent() { Console.WriteLine("Parent"); }
        public Parent(int x, int y) { Console.WriteLine(x + y); }
        public Parent(string s1, string s2) { Console.WriteLine(s1 + s2); }
    }
    
    class Child1 : Parent
    {
        public Child1() { Console.WriteLine("Child1"); }
    }
    
    class Child2 : Parent
    {
        public Child2() : base() { Console.WriteLine("Child2"); }
    }
    
    class Child3 : Parent
    {
        public Child3() : base(111,222) { Console.WriteLine("Child3"); }
    }
    
    class Child4 : Parent
    {
        public Child4() : base("111", "222") { Console.WriteLine("Child4"); }
    }
    
    class Program
    {
        static void Main()
        {
            Child1 c1 = new Child1(); // Parent / Child1
            Child2 c2 = new Child2(); // Parent / Child2
            Child3 c3 = new Child3(); // 333 / Child3
            Child4 c4 = new Child4(); // 111222 / Child4
            
            Console.ReadKey();
        }
    }
    

    this:
    using System;
    
    class MyClass
    {
        private string fs = "ABC-";
        public MyClass() { Console.WriteLine("MyClass"); }
        public MyClass(string str) { Console.WriteLine(this.fs + str); }
        public MyClass(int num) : this() { Console.WriteLine(num); }
        public MyClass(int x, int y) : this("XYZ") { Console.WriteLine(x + y); }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass c1 = new MyClass();          // MyClass
            MyClass c2 = new MyClass("EFG");     // ABC-EFG
            MyClass c3 = new MyClass(123);       // MyClass / 123
            MyClass c4 = new MyClass(111, 222);  // ABC-XYZ / 333
            Console.ReadKey();
        }
    }
    

    构造函数、属性、base:
    using System;
    
    abstract class Parent
    {
        private byte FID;
    
        public Parent(byte n)
        {
            FID = n;
        }
    
        public byte Id
        {
            get { return FID; }
            set { FID = value; }
        }
    }
    
    class Child : Parent
    {
        public Child(byte MyID) : base(MyID) { }
    }
    
    
    class Program
    {
        static void Main()
        {
            Child Rect = new Child(6);
            Console.WriteLine(Rect.Id); //6
    
            Rect.Id = 8;
            Console.WriteLine(Rect.Id); //8
    
            Console.ReadKey();
        }
    }
    

  • 相关阅读:
    [Bash] Shortcut
    [Bash] Rerun Bash Commands with History Expansions (!! & !$)
    [Bash] Create and Copy Multiple Files with Brace Expansions in Bash
    [Bash] Add Executable Files to $PATH with Bash
    [Typescript] Create Type From Any Given Data in TypeScript
    [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
    [Bash] Chain Commands with Pipes and Redirect Output in Bash
    [Bash] Use Conditional Statements in Bash
    [Bash] Understand Exit Statuses in Bash
    监听内容变化 TextWatcher @功能 [MD]
  • 原文地址:https://www.cnblogs.com/del/p/1367117.html
Copyright © 2011-2022 走看看