今日学习内容:

1. 设计编写一个控制台应用程序,练习类的继承。

(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。

(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

覆盖Work 方法。

(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

(4) 在 Student 和 Employer 实例中输出各自不同的信息。

运行代码:

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
using System;
 
using System.Collections.Generic;
 
using System.Linq;
 
using System.Text;
 
using System.Threading.Tasks;
 
  
 
namespace _2_1
 
{
 
    abstract class People   //抽象类:people
 
    {
 
        public String name; //姓名
 
        public String Name { get; set; }//姓名属性
 
        public int age;//年龄
 
        public abstract void work();//work方法
 
    }
 
    class Student : People  //学生类,继承people类
 
    {
 
        public String school; //增加的学校字段
 
        public override void work()
 
        {
 
            Console.WriteLine("student类————");
 
        }
 
    }
 
    class Employer : People //Employer类,继承people类
 
    {
 
        public String workplace;//增加的工作单位字段
 
        public override void work()
 
        {
 
            Console.WriteLine("Employer类————");
 
        }
 
  
 
    }
 
    class Program
 
    {
 
        static void Main(string[] args)
 
        {
 
            Student st = new Student();
 
            Console.WriteLine("请输入学生的姓名:");
 
            st.name = Console.ReadLine();
 
            Console.WriteLine("请输入学生的年龄:");
 
            st.age = int.Parse(Console.ReadLine());
 
            Console.WriteLine("请输入学生的学校:");
 
            st.school = Console.ReadLine();
 
            Console.WriteLine("学生的姓名:{0}", st.name);
 
            Console.WriteLine("学生的年龄:{0}", st.age);
 
            Console.WriteLine("学生的学校:{0}", st.school);
 
            st.work();
 
            Employer em = new Employer();
 
            Console.WriteLine("请输入员工的姓名:");
 
            em.name = Console.ReadLine();
 
            Console.WriteLine("请输入员工的年龄:");
 
            em.age = int.Parse(Console.ReadLine());
 
            Console.WriteLine("请输入员工的工作单位:");
 
            em.workplace = Console.ReadLine();
 
            Console.WriteLine("员工的姓名:{0}", em.name);
 
            Console.WriteLine("员工的年龄:{0}", em.age);
 
            Console.WriteLine("员工的工作单位:{0}", em.workplace);
 
            em.work();
 
        }
 
    }
 
}

  

 

posted on   白日梦想家~  阅读(49)  评论(0编辑  收藏  举报
努力加载评论中...

点击右上角即可分享
微信分享提示