c# 1121 构造方法

构造函数

实例构造函数

csharp 复制代码
  internal class Course
  {
      private int id;
      public int CourseId
      {
          get { return id; }//属性访问器 获取属性
          set { id = value; }//设置属性
      }

      public string CourseName { get; set; }

      public void ShowCourse()
      {
          Console.WriteLine("courseid is:" + CourseId + ",CourseName is:" + CourseName);
      }
      //不写修饰符,默认private 类,方法
      public Course() { }
      public Course(int id,string courseName)
      {
          CourseId=id;
          CourseName=courseName;
      }
  
  
  }
csharp 复制代码
   Course course1 = new Course(22,"sds");
   course1.CourseName = "CS";
course1.ShowCourse();

密封类构造

csharp 复制代码
//public sealed class ChildClass
//{ }
//public class SubClass:ChildClass { }//密封类无法被继承

静态构造函数

csharp 复制代码
    public  class Teacher
    {
        public static int count = 0;
        public Teacher()
        {
            count = 1;
        }
        static Teacher()
        {
            count = 3;
        }
    }
csharp 复制代码
            //静态构造函数
            Console.WriteLine($"count={Teacher.count}");//为什么不输出count=0
            Teacher teacher = new Teacher();
            Console.WriteLine($"count={Teacher.count}");




私有构造

csharp 复制代码
 public class Fav
 {
     private Fav()
     {//单例模式的对象创建使用
     }
     public static Fav instance { get; set; } = new Fav();
 }
   public class RecordInfo
  {
      private static RecordInfo record = null;
      private RecordInfo()
      {

      }
      public static RecordInfo GetObj()
      {
          if(record == null)
          {
              record= new RecordInfo();
          }
          return record;
      }
  }
csharp 复制代码
         //私有构造
 Fav v1 =  Fav.instance;
 RecordInfo record1 = RecordInfo.GetObj();
 RecordInfo record2 = RecordInfo.GetObj();



this

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C_Up
{
    public class Product
    {
        public Product()
        {
            Console.WriteLine("构造方法:Product对象");
        }
        private int Id;
        public string ProductName { get; set; }
        public int[] noArr = new int[5];

        public Product(int id, string name)
        {
            this.Id = id;
            this.ProductName = name;
        }
        //串联构造函数
        public Product(string name) : this(0, name)
        {
            Console.WriteLine("名称:" + name);
            Console.WriteLine("通过名称构造product");
        }
        public void Show()
        {
            Console.WriteLine("num is:" + Id + ";name is:" + ProductName);
        }
        public void SetNos(int[] nos)
        {
            if (nos.Length == 5)
            {
                for (int i = 0; i < nos.Length; i++)
                {
                    {
                        noArr[i] = nos[i];
                    }
                }

            }
        }
    }
}
csharp 复制代码
    Product p1 = new Product(1, "玩具");
    p1.Show();
    Product p2 = new Product("枪");
相关推荐
han_几秒前
JavaScript设计模式(二):策略模式实现与应用
前端·javascript·设计模式
x***r1515 分钟前
Notepad++ 8.6 安装教程:详细步骤+自定义安装路径(附注意事项)
linux·前端·javascript
格林威6 分钟前
工业相机图像采集:Grab Timeout 设置建议——拒绝“假死”与“丢帧”的黄金法则
开发语言·人工智能·数码相机·计算机视觉·c#·机器视觉·工业相机
Hilaku8 分钟前
为什么很多工作 5 年的前端,身价反而卡住了?🤷‍♂️
前端·javascript·面试
小涛不学习9 分钟前
Java高频面试题(带答案版)
java·开发语言
big_rabbit05029 分钟前
JVM堆内存查看命令
java·linux·算法
学习要积极17 分钟前
Springboot图片验证码-EasyCaptcha
java·spring boot·后端
唐青枫24 分钟前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
执行部之龙27 分钟前
JS手写——call bind apply
前端·javascript
李少兄28 分钟前
企业资源计划(ERP)系统全景指南
java·前端·数据库·erp