zoukankan      html  css  js  c++  java
  • 构造函数总结

    构造函数总结

    1:构造方法要与类名相同
    2:构造函数无返回值
    3:对对象进行初始化 ,且默认为0或null。
    4 :构造函数的声明为public类型,不可以声明protected成员、virtual成员、sealed成员和override成员;
    5:可以重载System.Object的3个虚方法,Equals()、ToString()和GetHashTable()。
    6:也不能声明抽象函数。
    7:没有void修饰。

    class CoOrds//类名
    {
        public int x, y;

        // Default constructor:
        public CoOrds()
        {
            x = 0;//初始化 
            y = 0;
        }

        // A constructor with two arguments:
        public CoOrds(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        // Override the ToString method:
        public override string ToString()
        {
            return (System.String.Format("({0},{1})", x, y));
        }
    }

    class MainClass
    {
        static void Main()
        {
            CoOrds p1 = new CoOrds();
            CoOrds p2 = new CoOrds(5, 3);

            // Display the results using the overriden ToString method:
            System.Console.WriteLine("CoOrds #1 at {0}", p1);
            System.Console.WriteLine("CoOrds #2 at {0}", p2);
        }
    }






  • 相关阅读:
    Codeforces 1149 B
    Tenka1 Programmer Contest 2019 D
    BZOJ 1001 [BeiJing2006]狼抓兔子
    Codeforces 741 D
    HDU 5306 Gorgeous Sequence
    HDU 6521 Party
    Codeforces 912A/B
    Educational Codeforces Round 35 B/C/D
    Codeforces 902D/901B
    Codeforces 902B
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/889577.html
Copyright © 2011-2022 走看看