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("枪");
相关推荐
我是人✓9 分钟前
SQL主键与外键
java·数据库
圣光SG16 分钟前
Java操作题练习(二)
java·开发语言·python
蜡台21 分钟前
Android WebView 设计指南
android·java·kotlin
不简说22 分钟前
JS 代码技巧 vol.9 — 20 个设计模式在真实项目里的应用
前端·javascript·github
勇往直前plus31 分钟前
Vue3(篇三) Element Plus
前端·javascript·typescript·vue
AI_paid_community37 分钟前
机械研发人员如何使用 Claude?
java·vue.js
MacroZheng39 分钟前
同事:“Claude Code都能自动写代码了,还要什么Spec Coding?” 我反问:“屎山代码你来维护?”
java·人工智能·后端
csdn_aspnet42 分钟前
C# 从凸包中删除点(Deleting points from Convex Hull)
开发语言·c#
Revolution611 小时前
数组本身没有 map,为什么还能直接调用:原型链怎样查找属性
前端·javascript·面试
未秃头的程序猿1 小时前
JDK 26的Value Class,我做了个性能测试,结果出乎意料
java·后端·面试