ACCP7.0S2深入.net和c#第6章上机练习

--上机1

public class person
    {
      public string name{get;set;}
      public int age { get; set; }
       public person(){}
       public person(string name,int age) {
           this.name = name;
           this.age = age;
       }
    }

public class student:person
    {
       public int no { get; set; }
       public student() { }
       public student(string name, int age, int no){
           base.name = name;
           base.age = age;
           this.no = no;
       }
    }

public class truck:vehicle
    {
       public truck(string type,string pro):base(type,pro) {
           base.type = type;
           base.produce = pro;
       }
       public void truckrun() {
           Console.WriteLine("型号为{0},产地为{1}的卡车在行驶",base.type,base.produce);
       }
    }

public class vehicle
    {
       public string type;
       public string produce;
       public vehicle(string type,string pro) {
           this.type = type;
           this.produce = pro;
       }
       public void vehiclerun() {
           Console.WriteLine("汽车在行驶");
       }
    }

class Program
    {
        static void Main(string[] args)
        {
            truck t = new truck("奔驰","中国");
            t.vehiclerun();
            t.truckrun();
        }
    }


--上机2、3

public class employee
    {
       public int age;
       public Gender gender;
       public string id;
       public string name;
       public List worklist;
       public employee() { }
       public employee(int age,Gender gen,string id,string name,List work) {
       this.age=age;
           this.gender=gen;
           this.id = id;
           this.name = name;
           this.worklist = work;
       }
       public virtual string dowork() {
           return "";
       }
    }

public enum Gender
    {
        male,female
    }

public class Job
    {
       public string description;
       public string name;
       public Job(string desc,string name) {
           this.description = desc;
           this.name = name;
       }
    }

public class PM:employee
    {
       public int yearofexperience { get; set; }
       public PM() { }
       public PM(string id, string name, int age, Gender gen, int yoe, List work)
           : base(age, gen, id, name, work) 
       {
           base.age = age;
           base.gender = gen;
           base.id = id;
           base.name = name;
           base.worklist = work;
           this.yearofexperience = yoe;
       }
       public override string dowork() {
           return this.name+":管理员工完成工作内容";
       }
    }

public class SE:employee
    {
       public int populartity { get; set; }
       public SE() { }
       public SE(string id,string name,int age,Gender gen,int pop,List work):base(age,gen,id,name,work) 
       {
           base.age = age;
           base.gender = gen;
           base.id = id;
           base.name = name;
           base.worklist = work;
           this.populartity = pop;
       }
       public override string dowork() {
           StringBuilder sb = new StringBuilder();
           sb.Append(this.name+":\n");
           for (int i = 0; i < this.worklist.Count; i++) {
               sb.Append((i+1)+","+worklist[i].name+":"+worklist[i].description+"\n");
           }
           return sb.ToString();
       }
    }

class Program
    {
        static void Main(string[] args)
        {
            List l1 = new List();
            l1.Add(new Job("编码", "购物车模块"));
            l1.Add(new Job("测试", "给购物车模块做单元测试"));
            SE ai = new SE("112","爱变成",25,Gender.male,100,l1);
            List l2 = new List();
            l2.Add(new Job("设计", "数据库建模"));
            l2.Add(new Job("编写文档", "详细设计说明书"));
            SE joe = new SE("113","joe",30,Gender.female,200,l2);
            PM pm = new PM("890","盖茨",50,Gender.female,30,null);
            List emp = new List();
            emp.Add(ai);
            emp.Add(joe);
            emp.Add(pm);
            foreach(employee e in emp){
                    Console.WriteLine(e.dowork(),"汇报");
            }
            Console.ReadLine();
        }
    }


--上机4

public class operation
    {
       public double numberA { get; set; }
       public double numberB { get; set; }
       public virtual double getresult() {
           return 0;
       }
    }

public class operationadd:operation
    {
       public override double getresult()
       {
           return numberA+numberB;
       }
    }

public class operationdiv:operation
    {
       public override double getresult()
       {
           if (numberB == 0) {
               throw new Exception("除数不能为0");
           }
           return numberA/numberB;
       }
    }

public class operationmul:operation
    {
       public override double getresult()
       {
           return numberA*numberB;
       }
    }

public class operationsub:operation
    {
       public override double getresult()
       {
           return numberA-numberB;
       }
    }

namespace no6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            operation o = new operation();
            switch(this.comboBox1.Text.ToString()){
                case "+":
                    o = new operationadd();
                    break;
                case "-":
                    o = new operationsub();
                    break;
                case "*":
                    o = new operationmul();
                    break;
                case "/":
                    o = new operationdiv();
                    break;
            }
            o.numberA = double.Parse(this.textBox1.Text);
            o.numberB = double.Parse(this.textBox2.Text);
            this.label2.Text = o.getresult().ToString();
        }
    }
}


你可能感兴趣的:(ACCP7.0S2深入.net和c#第6章上机练习)