码迷,mamicode.com
首页 > 其他好文 > 详细

类的认识

时间:2017-10-15 14:55:57      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:hello   str   sha   string   ogr   har   class   poi   pre   

构造函数:

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.构造函数输出 = 传入构造函数;
        }
    }

  

类的认识

标签:hello   str   sha   string   ogr   har   class   poi   pre   

原文地址:http://www.cnblogs.com/y58401/p/7670302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!