码迷,mamicode.com
首页 > Windows程序 > 详细

C#构造方法(构造函数)

时间:2019-11-22 16:37:01      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:lin   class   手机   span   创建   ring   返回值   bsp   int   

构造方法特点:

一 ,与类同名

public class Product
    {
        public int ID { get; set; }
        public String NAME { get; set; }
        public Decimal Price { get; set; }
        public ProductType type { get; set; }
        public DateTime Birthday { get; set; }

        public Product()  //无参
        {
            ID = 1100;
            NAME = "手机";
            Price = 8888;
            type = ProductType.手机;
            Birthday = new DateTime(2019, 11, 1);
        }
    }

二,没有带返回值

三 ,无参构造函数

        public Product()  //无参
        {
            ID = 1100;
            NAME = "手机";
            Price = 8888;
            type = ProductType.手机;
            Birthday = new DateTime(2019, 11, 1);
        }

四,有参构造函数,this当前对象

        public Product(int id,string Name,int price, ProductType type)
        {
            this.ID = id;
            this.NAME = Name;
            this.Price = price;  //this当前对象
        }

不要声名重复的构造函数,私有的构造方法不能创建对象

调构造函数

            Product s1 = new Product();//无参
            Product s2 = new Product("2000","huawie", 5000,ProductType.服装, new DateTime(2019,2,3)); //有参
            Console.WriteLine(s2.ID+s2.NAME+s2.Price+ProductType.服装);
            Console.ReadLine();
            Console.WriteLine(s1.ID);

 

 

C#构造方法(构造函数)

标签:lin   class   手机   span   创建   ring   返回值   bsp   int   

原文地址:https://www.cnblogs.com/yijieyufu/p/11912356.html

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