首页 > 编程语言 >C#-类

C#-类

时间:2022-08-27 08:25:14浏览次数:44  
标签:Cellphone set Console get C# class public

1、类的声明

class 类名
{
    //类中的代码
}

2、类的成员

类头:类名
类体:字段、属性、方法、构造函数等

1、字段

就是常量或者变量。
若字段未初始化默认为0。

namespace Demo
{
    class Employee//定义员工类
    {
        public string name;//定义姓名字段
        public int age;//定义年龄字段
    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee e = new Employee();//创建员工类的对象
            e.name = "小王";//为姓名字段赋值
            e.age = 30;//为年龄字段赋值
            //输出员工的姓名及年龄
            Console.WriteLine("姓名:{0}  年龄:{1}", e.name, e.age);
            Console.ReadLine();
        }
    }
}

特殊字段:枚举
不要声明在方法里面

访问修饰符 enum 枚举名{值1,值2...}
枚举名.值1 //调用

2、属性

属性的声明

private 数据类型 变量名;
public 数据类型 属性名
{
    get { return 变量名; }
    set { 变量名 = value; }
}
[权限修饰符] [类型] [属性名]
{
    get {get访问器体}
    set {set访问器体}
}

实例

namespace Demo
{
    class cStockInfo//商品信息类
    {
        private int num = 0;//声明一个私有变量,用来表示数量
        public int Num//库存数量属性
        {
            get
            {
                return num;
            }
            set
            {
                if (value > 0 && value <= 100)//控制数量在0—100之间
                {
                    num = value;
                }
                else
                {
                    Console.WriteLine("商品数量输入有误!");
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入库存商品数量:");
            //创建cStockInfo对象
            cStockInfo stockInfo = new cStockInfo();
            stockInfo.Num = Convert.ToInt32(Console.ReadLine());
            Console.ReadLine();
        }
    }
}

3、构造函数

1、构造函数的定义

public class Book
{
    public Book() //无参数构造方法
    {
    
    }
    public Book(int args) //有参数构造方法
    {
        args = 2 + 3;
    }
}

2、

namespace Demo
{
    class Cellphone
    {
        public Cellphone()
        {
            Console.WriteLine("智能手机的默认语言为英文");
        }

        public Cellphone(String defaultLanguage)
        {
            Console.WriteLine("将智能手机的默认语言设置为" + defaultLanguage);
        }
        static void Main(string[] args)
        {
            Cellphone cellphone1 = new Cellphone();
            Cellphone cellphone2 = new Cellphone("中文");
            Console.ReadLine();
        }
    }
}

4、析构函数

标签:Cellphone,set,Console,get,C#,class,public
From: https://www.cnblogs.com/shazamsjtu/p/16629451.html

相关文章

  • current
    Origin:curant,presentparticipleofcourre'torun',fromLatincurrere]Currentmayreferto:Current(fluid),theflowofaliquidoragasAircurrent,......
  • cur·ric·u·lum
    Ineducation,acurriculum(/kəˈrikjʊləm/;pluralcurricula/kəˈrikjʊlə/orcurriculums)isbroadlydefinedasthetotalityofstudentexperiencesthat......
  • PowerShell教程 - 计算机管理(Computer System Management)
    更新记录转载请注明出处。2022年8月27日发布。2022年8月27日从笔记迁移到博客。计算机管理(ComputerSystemManagement)重启计算机Restart-Computer重命名计算......
  • NC50940 Running Median
    题目原题地址:RunningMedian题目编号:NC50940题目类型:对顶堆时间限制:C/C++5秒,其他语言10秒空间限制:C/C++65536K,其他语言131072K1.题目大意多组数据,每组有标号、......
  • cur·ren·cy
    Origin:currentia'flowing',fromLatincurrere;currentAcurrencyinthemostspecificsenseismoneyinanyformwheninuseorcirculationasamediumofe......
  • DefaultKaptcha无法自动装配,找不到 'DefaultKaptcha' 类型的 Bean
    报错信息可以检查一下包含DefaultKaptcha的Config类上面是否添加了@Configuration注解,示例如图......
  • 【CodeEnd】Github Note Command
    NotegithubNote嵌入式笔记EmbeddedNoteC基础知识数据结构(链表hash表排序算法设计模式等)外设(串口网口i2si2cspisdio等)ARMcortex-m0m3m4A8等芯片架构......
  • asyncio队列 asyncio.Queue()
    importasyncio#如果maxsize小于等于零,则队列尺寸是无限的。#如果是大于0的整数,则当队列达到maxsize时,awaitput()将阻塞至某个元素被get()取出Q=async......
  • C#
    类类的声明class类名{}类的成员类头:类名类体:字段、属性、方法、构造函数等1、字段就是常量或者变量。若字段未初始化默认为0。namespaceDemo{classE......
  • Codeforces Round #813 (Div. 2) A - E2
    A:一组长度为n的排列,问交换多少次,能让前m个数变成[1,m]中的数输出前m个数中有多少个比m大的就可以了//-------------------------代码----------------------------......