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("枪");
相关推荐
SimonKing14 分钟前
震惊:怎么回事,opencode一轮对话耗尽免费额度
java·后端·程序员
装不满的克莱因瓶18 分钟前
【SpringAI Alibaba】ReactAgent响应SSE实时流式传输案例实战
java·人工智能·ai·sse·springai
杨凯凡21 分钟前
Spring Boot 文件上传怎么做才安全?流式落盘、路径校验与数据库一致性实战
java·spring boot·文件上传·后端安全·数据库一致性
这个昵称不容易1 小时前
旅游管理系统的设计与实现(SpringBoot + MySQL + Vue 前后端分离)
java·springboot·毕设
Rain的Java大神之路1 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
1-1=01 小时前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
天天被压力2 小时前
【A股分钟级高频数据完全手册 #11】券商实时行情数据接口:实时交易(券商源)接入与字段
java·人工智能·python
天天被压力2 小时前
【A股分钟级高频数据完全手册 #12】股票五档盘口数据接口:买卖五档实时盘口解析
java·人工智能·python
Wang's Blog11 小时前
Java 项目实战: 交友项目 - FastDFS:入门案例
java·开发语言·交友
apcipot_rain12 小时前
C++转Java日记(03)
java·开发语言·c++