C#知识整理-类(Class)

关键字:
struct:结构体
class:类
interface:接口
abstract:定义抽象类或抽象方法使用
sealed:密封类,不可继承的类
void:表示无返回值
 
抽象类(abstract class)
抽象类不能被实例化。抽象类的用途是提供一个可供多个派生类共享的通用基类定义。 例如,类库可以定义一个抽象类,将其用作多个类库函数的参数,并要求使用该库的程序员通过创建派生类来提供自己的类实现。
抽象类中的成员
  • 抽象类中通过abstract关键字可以定义抽象方法, 抽象方法不包含方法体的实现。继承自抽象类的派生类必须通过override关键字实现抽象方法。
  • 抽象类中也可以定义包含方法体的方法。可以通过virtual的方法,virtual方法可以在派生类中的被重写。不适用virtual的方法不能被重写。
  • 在派生类中可以通过base.function()调用基类中的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public abstract class BaseEntity
    {
        public virtual string Id { get; set; }
        public virtual string CreatedBy { get; set; }
        public virtual DateTime CreatedDate { get; set; }
        public virtual string UpdatedBy { get; set; }
        public virtual DateTime UpdatedDate { get; set; }
 
        public virtual void TestMethod() {
            Console.WriteLine("Test Mechod");
        }
        public abstract void TestMethod2();
        public void TestMethod3() {
            Console.WriteLine("Test Method3");
        }
    }
  
    public class Person:BaseEntity
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Birthday { get; set; }
        public override void TestMethod()
        {
            Console.WriteLine("Test Method -- Person");
        }
        public override void TestMethod2()
        {
            Console.WriteLine("Test Method2 -- Person");
            base.TestMethod3();
            base.TestMethod();
        }
    }
  
    public class Teacher :Person {
        public int Level { get; set; }
        public string Course { get; set; }
 
        //public override void TestMethod()
        //{
        //    Console.WriteLine("Test Method -- Teacher");
        //}
        public override void TestMethod()
        {
            Console.WriteLine("Test Method -- Teacher");
        }
    }
 Teacher newTeacher = new Teacher()
{
    Id = Guid.NewGuid().ToString(),
    Name = "Teacher1",
};
newTeacher.TestMethod();
newTeacher.TestMethod2();
 
// output:
Test Method -- Teacher
Test Method2 -- Person
Test Method3
Test Mechod
静态类(static class)
静态类中只能包含静态成员,无法实例化,不能包含实例构造函数,无法被继承
相对的静态类中可以定义静态构造函数,它会在创建第一个实例或引用任何静态成员之前自动调用。 静态构造函数最多调用一次。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    public static class UtilitySerivce
    {
        private static string curDate = string.Empty;
        static UtilitySerivce() {
            curDate = DateTime.Now.ToString();
        }
 
        public static string GetDate()
        {
            return curDate;
        }
 
        public static string GetRandomNumber() {
            string result = new Random().Next().ToString();
            return result;
        }
 
    }
  
 Console.WriteLine(UtilitySerivce.GetRandomNumber());
Thread.Sleep(2000);
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine(UtilitySerivce.GetDate());
/*
 * output:
    1383072803
    2024/12/24 14:16:54
    2024/12/24 14:16:52
 */
可以看到UtilityService在第一次调用GetRandomNumber是被初始化,curDate就是在那时被赋值
静态成员
非静态类也可以包含静态方法、字段、属性、事件。即使不存在类的任何实例也可以调用静态成员。
静态成员只有一个副本存在。
静态方法可以进行重载,但不能进行替代或重写,因为它们属于类但不属于类的任何实例,一个简单的例子表明静态成员不属于任何实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   public class Person:BaseEntity
   {
       // 静态成员作为一个计数器
       public static int TotalPersons = 0;
       public string Name { get; set; }
       public int Age { get; set; }
       public string Birthday { get; set; }
       //在构造函数对计数器进行赋值
       public Person() {
           TotalPersons++;
       }
       public override void TestMethod()
       {
           Console.WriteLine("Test Method -- Person");
       }
       public override void TestMethod2()
       {
           Console.WriteLine("Test Method2 -- Person");
           base.TestMethod3();
           base.TestMethod();
       }
   }
//实例化两个对象,对静态对象进行两次赋值
           Person p1 = new Person();
           Person p2 = new Person();
           Console.WriteLine($"Person Count:{Person.TotalPersons}");
  // output:
      Person Count:2 
类(class)
之前有写到,结构是值类型,类是引用类型,在这里定义对象的时候对于小型、不可变的数据结构,结构体是一个好的选择;而对于需要继承、可以被继承、或者需要引用类型特性的复杂对象,基本上都是选择类
之前提到的抽象类,静态类,密封类都可以认为时特殊的类,他们做了一些特殊的限制导致他们有些不可被继承或者实例化
类的成员包含属性、方法、构造函数等
在编程中类是时刻需要打交道的对象
可以通过以下实例看到类中的字段、属性、接口实现的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
internal class Consts
{
    //定义常量
    public const string TEACHER = "老师";
    public const string STUDENT = "学生";
}
 
internal interface IPerson
{
    void Show();
}
public class Person:BaseEntity
{
    // 静态成员作为一个计数器
    public static int TotalPersons = 0;
    public string Name { get; set; }
    public int Age { get; set; }
    private DateTime _birthday; //定义一个私有字段,存储生日
    public string Birthday {
        get
        {
            return _birthday.ToString("yyyy-MM-dd");
        }
        set
        {
            _birthday = DateTime.Parse(value);
        }
    }
    //只读属性,没有set访问器
    public string Type
    {
        get
        {
            if (Age > 18)
            {
                return Consts.TEACHER;
            }
            else
            {
                return Consts.STUDENT;
            }
        }
    }
    //在构造函数对计数器进行赋值
    public Person() {
        TotalPersons++;
    }
    //提供重载的构造函数
    public Person(string name, int age, string birthday)
    {
        Name = name;
        Age = age;
        Birthday = birthday;
        TotalPersons++;
    }
 
    public override void TestMethod()
    {
        Console.WriteLine("Test Method -- Person");
    }
    public override void TestMethod2()
    {
        Console.WriteLine("Test Method2 -- Person");
        base.TestMethod3();
        base.TestMethod();
    }
 
    public override string ToString()
    {
        return $"name:{Name},age:{Age},type:{Type},birthday:{Birthday}";
    }
}
public class Student : Person, IPerson
{
    public string Class { get; set; }
    public int Grade { get; set; }
    public void Show()
    {
        Console.WriteLine("This is Student");
    }
}
public class Teacher :Person,IPerson {
    public int Level { get; set; }
    public string Course { get; set; }
 
    //public override void TestMethod()
    //{
    //    Console.WriteLine("Test Method -- Teacher");
    //}
    public override void TestMethod()
    {
        Console.WriteLine("Test Method -- Teacher");
    }
    public void Show() {
        Console.WriteLine("This is Teacher");
    }
}
             //定义一个方法,Iperson为参数,体会一下接口的作用
     static void ShowMessage(IPerson person) {
        person.Show();
    }
        Person p1 = new Person("test1",10,"2014-1-1");
        Person p2 = new Person() { Name = "test2", Age = 30, Birthday = "1994-1-1" };
        Console.WriteLine($"Person Count:{Person.TotalPersons}");
        Console.WriteLine(p1);
        Console.WriteLine(p2);
 
        IPerson t1 = new Teacher();
        IPerson s1 = new Student();
        t1.Show();
        s1.Show();
        ShowMessage(t1);
        ShowMessage(s1);
           /*
         output:
            name:test1,age:10,type:学生,birthday:2014-01-01
            name:test2,age:30,type:老师,birthday:1994-01-01
            This is Teacher
            This is Student
            This is Teacher
            This is Student
         */

  

posted @   Terry841119  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示