对C#的类的理解,自己的笔记贴

xjw163 xjw163 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
namespace LeafSoft
{
public class EnumType
{
/// <summary>
/// 命令类型
/// </summary>
public enum CMDType
{
Hex,
ASCII
}

}

public class a_class
{
//成员变量
string member_a = "chushihua_value_a";
string member_b = "chushihua_value_b";

//构造函数
public a_class()
{
member_a = "gouzao_a";
}
//成员函数
public void func()
{
Console.WriteLine("duixiang");
}

//类的对象的属性
public string shuxing1
{
get
{
return member_a;
}
set
{
member_a = value;
}
}


public string shuxing2
{
get
{
return "b";
}
}
}
}

namespace ConsoleApplication2
{
class Program
{

static void Main(string[] args)
{
LeafSoft.EnumType.CMDType lec = LeafSoft.EnumType.CMDType.ASCII;
LeafSoft.a_class 对象 = new LeafSoft.a_class();
对象.shuxing1 = "set_value";
Console.WriteLine();
}
}
}


创建一个类:
成员变量
        string member_a = "chushihua_value_a";
string member_b = "chushihua_value_b";


与类名相同的函数是该类的构造函数
        public a_class()
{
member_a = "gouzao_a";
}


类的对象的属性
        public string shuxing1
{
get
{
return member_a;
}
set
{
member_a = value;
}
}

类创建完成后
实例化一个类:
LeafSoft.a_class 对象 = new LeafSoft.a_class();

设置对象的属性:
对象.shuxing1 = "set_value";


在创建对象“对象”时,对象.shuxing1由构造函数设置成"gouzao_a"
随后手动设置对象.shuxing1为
对象.shuxing1 = "set_value";

使用
对象.func();
对对象的成员函数进行调用
...全文
评论 4
收藏 已收藏
分享
返回顶部
xjw163
C# 111,130 成员 642,540 内容