类的认识

构造函数:

1,构造函数是和类同名的函数方法

2,构造函数可以多个,也就是可以重载

using System;
class Point
{
public double x, y;
public Point()
{
this.x = 0;
this.y = 0;
}
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
…
}
class Test
{
static void Main()
{
Point a = new Point();
Point b = new Point(3, 4); // 用构造函数初始化对象
…
}
}

  

2,类的构造函数直接传参

class Program
    {
        static void Main(string[] args)
        {
            int 传入构造函数 = 2;
            Test test = new Test(传入构造函数);
            Console.WriteLine("Test类构造函数传出的值是:  " + test.构造函数输出);//实例域的调用方法
            Console.Read();
        }
    }
    class Test
    {
        public int 构造函数输出;//这是一个实例域。
        public Test(int 传入构造函数)
        {
            this.构造函数输出 = 传入构造函数;
        }
    }

  3,类的构造函数重载

4,类的构造函数泛型

 static void Main(string[] args)
        {
            int 传入构造函数 = 2;
            Test<int> test = new Test<int>(传入构造函数);
            Console.WriteLine("Test类构造函数传出的值是:  " + test.构造函数输出);//实例域的调用方法
            string 传入构造函数2 = "hello world";
            Test<string> test1 = new Test<string>(传入构造函数2);
            Console.WriteLine("Test类构造函数传出的值是:   " + test1.构造函数输出);
            Console.Read();
        }
    }
    class Test<多态的类型>
    {
        public 多态的类型 构造函数输出;//这是一个实例域。
        public Test(多态的类型 传入构造函数)
        {
            this.构造函数输出 = 传入构造函数;
        }
    }

  

posted @ 2017-10-15 11:36  我的WP标签备份  阅读(153)  评论(0编辑  收藏  举报