类的认识

构造函数:

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

  

转载于:https://www.cnblogs.com/y58401/p/7670302.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值