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);
        }
    }






  • 相关阅读:
    [转]HTTP请求模型和头信息参考
    括号匹配算法
    SQL LEFT JOIN 关键字 (转)
    Vim学习资料汇总
    与图相关的算法
    关于字符串的一些算法
    Huffman编码
    迅雷开放了下载引擎
    ubuntu下《UNIX环境高级编程》(apue.h)编译出错的处理方法
    学习unix信号
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/889577.html
Copyright © 2011-2022 走看看