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("枪");
相关推荐
Z***25802 小时前
Java爬虫框架
java·开发语言·爬虫
晓华-warm2 小时前
Warm-Flow 1.8.4 票签新增多种通过率策略!
java·中间件·流程图·jar·开源软件·工作流
m***11902 小时前
【SpringBoot】Spring Boot 项目的打包配置
java·spring boot·后端
李慕婉学姐2 小时前
Springboot剪纸数字博物馆系统6wd19a3a(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·spring boot·后端
90后小陈老师3 小时前
用户管理系统 05 实现后端注册功能 | Java新手实战 | 最小架构 | 期末实训 | Java+SpringBoot+Vue3
java·开发语言·spring boot·后端·spring·maven·mybatis
Coding_Doggy3 小时前
链盾shieldchain | 项目管理、DID操作、DID密钥更新消息定时提醒
java·服务器·前端
j***63083 小时前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
GISer_Jing3 小时前
3D Cesium渲染架剖析
javascript·3d·webgl
han_3 小时前
前端性能优化之CSS篇
前端·javascript·性能优化