1.继承
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_01
- {
- 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;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_01
- {
- //继承
- public class Student:Person
- {
- public string ExamNo { get; set; }
- public Student() { }
- //子类构造函数
- public Student(string name,int age,string examno):base(name,age)
- {
- base.Name = name;
- base.Age = age;
- this.ExamNo = examno;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_01
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Student student = new Student();
- // student.Name = "bb";
- // student.Age = 15;
- // Console.WriteLine("您好我叫{0},今年{1}岁", student.Name, student.Age);
- // Console.ReadLine();
- Student studetn = new Student("郝静芳",20,"00001");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_02上级模拟汽车行驶
- {
- public class Vehicle
- {
- public string Type { get; set; }
- public string Place { get; set; }
- public Vehicle(string type,string place )
- {
- this.Type = type;
- this.Place = place;
- }
- public void VehicleRun()
- {
- Console.WriteLine("汽车在行驶!");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_02上级模拟汽车行驶
- {
- public class Truck:Vehicle
- {
- public Truck(string type,string place):base(type,place)
- {
- }
- public void TruckRun()
- {
- Console.WriteLine("型号为{0},产地为{1}的在行驶!",this.Type,this.Place);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_02上级模拟汽车行驶
- {
- class Program
- {
- static void Main(string[] args)
- {
- Truck truck = new Truck("奔驰","德国");
- truck.VehicleRun();
- truck.TruckRun();
- Console.ReadLine();
- }
- }
- }
3.多态
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_03多态
- {
- public class Person
- {
- public virtual void Say()
- {
- Console.WriteLine("打招呼");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_03多态
- {
- public class Chinese:Person
- {
- public override void Say()
- {
- Console.WriteLine("中国人说:您好!");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_03多态
- {
- public class American:Person
- {
- public override void Say()
- {
- Console.WriteLine("美国人说:Hello!");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_03多态
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Person> list = new List<Person>()
- {
- new Chinese(),
- new American()
- };
- foreach (Person person in list)
- {
- person.Say();
- }
- }
- }
- }
4.上级实现工作汇报
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Day06_04上级实现工作汇报
- {
- public class Job
- {
- public string Description { get; set; }
- public string Name { get; set; }
- public Job(string name,string description)
- {
- this.Name = name;
- this.Description = description;
- }
- }
- }
- namespace Day06_04上级实现工作汇报
- {
- public class PM:Employee
- {
- public int YearOfExperience { get; set; }
- public PM(string id, int age, string name, char gender, int yearofexperience, List<Job> list)
- : base(id, age, name, gender, list)
- {
- this.YearOfExperience = yearofexperience;
- }
- public string DoWork()
- {
- string message = this.Name + ":管理员完成工作内容";
- return message;
- }
- public override void SayHi()
- {
- Console.WriteLine("经理");
- }
- }
- }
- namespace Day06_04上级实现工作汇报
- {
- public class SE : Employee
- {
- public int Popularity { get; set; }
- public SE(string id, int age, string name, char gender,int popularity, List<Job> list):base(id,age,name,gender,list)
- {
- this.Popularity = popularity;
- }
- public 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();
- }
- public override void SayHi()
- {
- Console.WriteLine("员工");
- }
- }
- }
- namespace Day06_04上级实现工作汇报
- {
- public partial class FrmMain : Form
- {
- public FrmMain()
- {
- InitializeComponent();
- }
- //员工集合
- List<Employee> emp = new List<Employee>();
- //员工信息初始化
- public void Init()
- {
- //实例化SE对象
- List<Job> list = new List<Job>();
- list.Add(new Job("编码", "购物车模块"));
- list.Add(new Job("测试", "给购物车模块做单元测试"));
- SE se=new SE("0001",20,"小艾",'男',100,list);
- //实例化PM对象
- PM pm = new PM("0002",25,"小比",'男',50,null);
- emp.Add(se);
- emp.Add(pm);
- }
- private void Report_Click(object sender, EventArgs e)
- {
- foreach (Employee employee in emp)
- {
- if (employee is PM)
- {
- MessageBox.Show(((PM)employee).DoWork(),"汇报",MessageBoxButtons.OK);
- }
- if (employee is SE)
- {
- MessageBox.Show(((SE)employee).DoWork(), "汇报", MessageBoxButtons.OK);
- }
- }
- List<Employee> em = new List<Employee>()
- {
- new PM("0002",25,"小比",'男',50,null),
- new SE("0001",20,"小艾",'男',100,null)
- };
- foreach(Employee emps in em)
- {
- emps.SayHi();
- }
- }
- private void FrmMain_Load(object sender, EventArgs e)
- {
- Init();
- }
- }
- }
5.上级4使用多态实现计算器
- namespace 上级4使用多态实现计算器
- {
- public class Operation
- {
- public double Number1 { get; set; }
- public double Number2 { get; set; }
- public virtual double GetResult()
- {
- double result = 0;
- return result;
- }
- }
- }
- namespace 上级4使用多态实现计算器
- {
- public class OperationAdd:Operation
- {
- public override double GetResult()
- {
- double result = Number1 + Number2;
- return result;
- }
- }
- }
- namespace 上级4使用多态实现计算器
- {
- public class OperationDiv : Operation
- {
- public override double GetResult()
- {
- if (Number2 == 0)
- {
- throw new Exception("除数不能为0!");
- }
- double result = Number1 / Number2;
- return result;
- }
- }
- }
- namespace 上级4使用多态实现计算器
- {
- public class OpreationCheng:Operation
- {
- public override double GetResult()
- {
- double result = Number1 * Number2;
- return result;
- }
- }
- }
- namespace 上级4使用多态实现计算器
- {
- public class OpreationJian:Operation
- {
- public override double GetResult()
- {
- double result = Number1 + Number2;
- return result;
- }
- }
- }
- namespace 上级4使用多态实现计算器
- {
- public partial class FrmMain : Form
- {
- public FrmMain()
- {
- InitializeComponent();
- this.comboBox1.SelectedIndex = 0;
- }
- private void btnCount_Click(object sender, EventArgs e)
- {
- try
- {
- Operation opr = new Operation();
- switch (this.comboBox1.SelectedItem.ToString().Trim())
- {
- case "+":
- opr = new OperationAdd();
- break;
- case "-":
- opr = new OpreationJian();
- break;
- case "*":
- opr = new OpreationCheng();
- break;
- case "/":
- opr = new OperationDiv();
- break;
- }
- opr.Number1 = double.Parse(this.textBox1.Text.Trim());
- opr.Number2 = double.Parse(this.textBox2.Text.Trim());
- this.label2.Text = opr.GetResult().ToString();
- this.label1.Visible = true;
- this.label2.Visible = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
6.猫狗继承关系
- namespace Day06_06猫狗继承关系
- {
- public class Animals
- {
- public string Name
- {
- get;
- set;
- }
- public string Color
- {
- get;
- set;
- }
- public virtual void Bark()
- {
- }
- public Animals(string name, string color)
- {
- this.Color = color;
- this.Name = name;
- }
- namespace Day06_06猫狗继承关系
- {
- public class Dog : Animals
- {
- public Dog(string name, string color)
- : base(name, color)
- {
- this.Name = name;
- this.Color = color;
- }
- public override void Bark()
- {
- string message = string.Format("我是{0}狗{1},汪汪", this.Color, this.Name);
- Console.WriteLine(message);
- }
- }
- }
- namespace Day06_06猫狗继承关系
- {
- public class Cat : Animals
- {
- public Cat(string name, string color)
- : base(name, color)
- {
- this.Name = name;
- this.Color = color;
- }
- public override void Bark()
- {
- Console.WriteLine
- ("我是{0}的{1},喵喵", this.Color, this.Name);
- }
- }
- }
- namespace Day06_06猫狗继承关系
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Animals> a = new List<Animals>()
- {
- new Dog("哈士奇","灰"),
- new Cat("加菲猫","咖啡")
- };
- foreach (Animals animalse in a)
- {
- animalse.Bark();
- }
- Console.ReadLine();
- }
- }
- }
7.简答题银行ATM业务
- namespace SimATM
- {
- public class Card
- {
- public Card() { }
- public Card(string cardNum, string password,string accountName, double balance)
- {
- this.CardNum = cardNum;
- this.Password = password;
- this.AccountName = accountName;
- this.Balance = balance;
- }
- private string cardNum;
- public string CardNum
- {
- get { return cardNum; }
- set { cardNum = value; }
- }
- private string password;
- public string Password
- {
- get { return password; }
- set { password = value; }
- }
- private string accountName;
- public string AccountName
- {
- get { return accountName; }
- set { accountName = value; }
- }
- private double balance;
- public double Balance
- {
- get { return balance; }
- set { balance = value; }
- }
- public virtual bool GetMoney(double money) { return true; }
- }
- }
- namespace SimATM
- {
- public class LocalCard:Card
- {
- public LocalCard() { }
- public LocalCard(string cardNum, string password, string accountName, double balance)
- : base(cardNum,password, accountName, balance)
- { }
- //取款
- public override bool GetMoney(double money)
- {
- if (this.Balance >= money)
- {
- this.Balance = this.Balance - money;
- return true;
- }
- else
- {
- return false;
- }
- }
- //转账
- public bool PostMoney(Card card, double money)
- {
- if (this.Balance >= money)
- {
- this.Balance = this.Balance - money;
- card.Balance += money;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- namespace SimATM
- {
- public class OtherCard:Card
- {
- public OtherCard() { }
- public OtherCard(string cardNum, string password, string accountName, double balance)
- : base(cardNum,password, accountName, balance)
- { }
- //取款
- public override bool GetMoney(double money)
- {
- if (this.Balance >= money)
- {
- this.Balance = this.Balance - money - 2;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
- namespace SimATM
- {
- public partial class MainForm : Form
- {
- Card myCard;
- OtherCard yourCard;
- Dictionary<string, Card> cards = new Dictionary<string, Card>();
- string key = null;
- public MainForm()
- {
- InitializeComponent();
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Dispose();
- }
- private void btnQuit_Click(object sender, EventArgs e)
- {
- Init();
- this.txtCardNum.Text = "";
- this.txtPwd.Text = "";
- }
- private void Init()
- {
- this.lblAccountName.Text = "";
- this.lblBalance.Text = "";
- this.lblCardNum.Text = "";
- this.txtPostNum.Text = "";
- this.txtGetMoney.Text = "";
- this.txtPostMoney.Text = "";
- this.btnGet.Enabled = false;
- this.btnPost.Enabled = false;
- this.btnQuery.Enabled = false;
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- Init();
- myCard = new LocalCard("1234", "1234", "张三", 10000.00);
- yourCard = new OtherCard("4321", "4321", "李四", 2000.00);
- cards.Add(myCard.CardNum,myCard);
- cards.Add(yourCard.CardNum, yourCard);
- }
- private void btnInsert_Click(object sender, EventArgs e)
- {
- Init();
- if (String.IsNullOrEmpty(txtCardNum.Text)
- || String.IsNullOrEmpty(txtPwd.Text))
- {
- MessageBox.Show("用户名密码能为空");
- return;
- }
- key = txtCardNum.Text;
- if(!cards.ContainsKey(key))
- {
- MessageBox.Show("没有这个用户");
- return;
- }
- if (cards[key].Password == txtPwd.Text)
- {
- this.lblAccountName.Text = cards[key].AccountName;
- this.lblCardNum.Text = key;
- this.btnGet.Enabled = true;
- this.btnQuery.Enabled = true;
- if (cards[key] is LocalCard)
- {
- this.btnPost.Enabled = true;
- }
- }
- }
- private void btnQuery_Click(object sender, EventArgs e)
- {
- this.lblBalance.Text = cards[key].Balance.ToString();
- }
- private void btnGet_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(this.txtGetMoney.Text))
- {
- MessageBox.Show("取款额不能为空");
- return;
- }
- if (cards[key].GetMoney(double.Parse(txtGetMoney.Text)))
- {
- MessageBox.Show("取款成功");
- lblBalance.Text = cards[key].Balance.ToString();
- }
- else
- {
- MessageBox.Show("余额不足。");
- }
- }
- private void btnPost_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrEmpty(this.txtPostMoney.Text)
- ||String.IsNullOrEmpty(txtPostNum.Text))
- {
- MessageBox.Show("转账金额和转账帐户不能为空");
- return;
- }
- string targetCardKey = txtPostNum.Text;
- if (!cards.ContainsKey(targetCardKey))
- {
- MessageBox.Show("转账账户不存在,请重新输入!");
- }
- else
- {
- if (((LocalCard)(cards[key])).PostMoney(cards[targetCardKey], double.Parse(txtPostMoney.Text)))
- {
- MessageBox.Show("转帐成功");
- lblBalance.Text = cards[key].Balance.ToString();
- }
- else
- {
- MessageBox.Show("余额不足。");
- }
- }
- }
- }
- }