继承

    技术2025-12-28  7

        今天写了一个小小的小程序,虽然在写的过程中出现了一些问题:

    1,看到题目没有思路,心里模模糊糊、大大概概的感觉,像知道又像不知道,在写代码前,不要急着去写程序,要学会分析,,比如我在写一个公司类的时候,就要去想到,公司里共有的东西:员工、工作内容等,这样就可以定义一个共有的类,里面包括很多的共有的属性。接着再考虑特殊的类,特殊的类可以继承共有的类的很多东西,再把自己的特殊的属性方法加上去即可,节约时间。

    2,函数的调用,参数的传递,也可以说是消息的传递,要注意,你想要实现什么,而你为了实现这个又需要哪些东西,方法里传递进来的就是你为了实现某个功能所必须的变量、类。

        花了很多时间,最后写出来了,很有成就感。

        form1.cs:

        private void button1_Click(object sender, EventArgs e)        {            Form2 formm = new Form2();            formm.Show();        }

    form2.cs:

         namespace WindowsFormsApplication1{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }

            private void Form2_Load(object sender, EventArgs e)        {            PM jack = new PM(18, "男", 2003, "王青", "分发任务", 4);            label1.Text = jack.Name;            label2.Text=jack.DoWork();                    }    }}employee。cs:

    namespace WindowsFormsApplication1{    class Employee    {         public int Age { get; set; }         public string Gender { get; set; }         public int ID { get; set; }         public string Name { get; set; }         public string  Worklist { get; set; }         public Employee()         {          }         public Employee(int age, string gender, int id, string name, string worklist)         {             this.Age = age;             this.Gender = gender;             this.ID = id;             this.Name = name;             this.Worklist = worklist;         }        

        }}

    se.cs:

    namespace WindowsFormsApplication1{    class SE:Employee    {          public int Popularity { get; set; }          public void DoWork(string name,string worklist)          {              Console.WriteLine("请输入程序员的姓名和工作内容");                           Console.WriteLine("{0}的工作是{1}",Name,worklist);

                 

              }    }} 

    PM.cs:

    namespace WindowsFormsApplication1{    class PM:Employee    {        public int Yearofexperience { get; set; }        public PM(int age,string gender,int id,string name,string work,int year):base( age, gender, id, name, work)        {            this.Yearofexperience =year ;

            }        public string DoWork()        {            string message = "项目经理的工作是沟通";            return message;

            }    }}

     

     

    最新回复(0)