文章目录
模型建立原则
- 模型建立原则: 从一般到特例 。 如果两个类是某种更一般情况的特定特例,就可以创建继承自同一个类。
- 避免子类出现重复代码——基类中应该使用合适的字段和方法。
- 设计原则:关注点分离——类尽可能减少重叠
- 创建类层次结构——避免重复代码的同时,易于理解代码结构
- 每个子类都扩展其基类
如果子类只是增加一个与基类方法同名的方法,那么它只是隐藏了基类方法而不是覆盖了这个方法。
例如Jewelhief中的方法ReturnContents
使用关键字new 进行基类方法的隐藏,在Jewelhief中的方法ReturnContents的方法上添加关键字new
public void Meth01()
{
Owner owner = new Owner();
Safe safe = new Safe();
Jewelhief jewelhief = new Jewelhief();
jewelhief.OpenSafe(safe,owner);
}
public class Jewels
{
public string Sparkle()
{
return "Sparkle,Sparkle!";
}
}
public class Locksmith
{
private string writtenDownCombination = null;
public void WriteDownComabination(string safecombination)
{
writtenDownCombination = safecombination;
}
public void OpenSafe(Safe safe,Owner ownerr)
{
safe.PickLock(this);
Jewels safeContents = safe.Open(writtenDownCombination);
ReturnContents(safeContents, ownerr);
}
public void ReturnContents(Jewels safeContents,Owner ownerr)
{
ownerr.ReceiveContents(safeContents);
}
}
class Jewelhief : Locksmith
{
private Jewels stolenJewels = null;
public void ReturnContents(Jewels jewels,Owner ownerr)
{
stolenJewels = jewels;
Console.WriteLine("I`M stealing the contents!"+ stolenJewels.Sparkle());
}
}
public class Safe
{
private Jewels contents = new Jewels();
private string safeCombination = "123456";
public Jewels Open(string combination)
{
if (safeCombination == combination)
{
return contents;
}
else
return null;
}
public void PickLock(Locksmith locksmith)
{
locksmith.WriteDownComabination(safeCombination);
}
}
public class Owner
{
private Jewels returnedContents;
public void ReceiveContents(Jewels jewels)
{
returnedContents = jewels;
Console.WriteLine("Thank you for returning jewels!" + jewels.Sparkle()); ;
}
}
子类可以覆盖方法来改变或替换它继承的方法:
基类中的方法需要有关键字 virtual.子类中增加完全相同的方法、返回值、参数必须都相同,并使用override关键字
使用冒号继承一个类
子类从一个基类继承时,基类中所有字段、属性、方法都会自动增加到子类。
只要能只用基类,就可以使用它的某个子类
例如: animal animalone=new dog();
子类可以使用base关键字访问基类
public class Locksmith
{
private string writtenDownCombination = null;
public void WriteDownComabination(string safecombination)
{
writtenDownCombination = safecombination;
}
public void OpenSafe(Safe safe,Owner ownerr)
{
safe.PickLock(this);
Jewels safeContents = safe.Open(writtenDownCombination);
ReturnContents(safeContents, ownerr);
}
public virtual void ReturnContents(Jewels safeContents,Owner ownerr)
{
ownerr.ReceiveContents(safeContents);
}
}
class Jewelhief : Locksmith
{
private Jewels stolenJewels = null;
public override void ReturnContents(Jewels jewels,Owner ownerr)
{
stolenJewels = jewels;
Console.WriteLine("I`M stealing the contents!"+ stolenJewels.Sparkle());
base.ReturnContents(jewels, ownerr);
}
}
基类有构造函数,子类也需要有构造函数
public class BaseClass
{
public BaseClass(string tipstr)
{
MessageBox.Show("zheshijilei " + tipstr);
}
}
public class SubClass : BaseClass
{
public SubClass(string basetipstr, string currenttip) : base(basetipstr)
{
MessageBox.Show("这是子类的提示 参数:" + currenttip);
}
}
蜂巢管理系统构建
需求
1、蜂王分配工种(6种)给能干该工作的工蜂,不同的工作可能需要多个班次才能完成
2、点击班次按钮,提示给蜂王这个班次的工蜂都在做什么,进度是多少
3、蜂王可以查看该工蜂现在的工作与进度
4、最后需要统计共耗蜂蜜,每个密缝消耗为:体重*比例系数 ,工蜂另外增加系数
public void Meth03()
{
cmjobs.ItemsSource = new List<string> { "Nectar collector", "Honey manufacturing", "Egg care", "Baby bee tutoring", "Hive maintenance", "Sting patrol" };
cmjobs.SelectedIndex = 1;
Worker[] workers = new Worker[4];
workers[0] = new Worker(new string[] { "Nectar collector", "Honey manufacturing" }, 175);
workers[1] = new Worker(new string[] { "Egg care", "Baby bee tutoring" }, 114);
workers[2] = new Worker(new string[] { "Hive maintenance", "Sting patrol" }, 149);
workers[3] = new Worker(new string[] { "Nectar collector", "Honey manufacturing", "Egg care", "Baby bee tutoring", "Hive maintenance", "Sting patrol" }, 155);
queen = new Queen(workers, 275);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!queen.AssignWork(cmjobs.Text, Convert.ToInt32(TBnumberOfshifts.Text)))
{
MessageBox.Show($"No worker are available to do the job {cmjobs.Text}");
}
else
{
MessageBox.Show($"This job {cmjobs.Text} will be done in {TBnumberOfshifts.Text}");
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
LScontent.Items.Add(queen.WorkTheNextSift());
}
public class Bee
{
public const double HoneyUnitsConsumedPerMg = 0.25;
public double WeightMg { get; private set; }
public Bee(double weightMg)
{
WeightMg = weightMg;
}
public virtual double HoneyConsumptionRate()
{
return WeightMg * HoneyUnitsConsumedPerMg;
}
}
public class Worker : Bee
{
private string[] JobsIcanDo;
private int shiftsToWork;
private int shiftsWorked;
const double honeyUintsPerShiftWorked = .65;
public Worker(string[] jobsICando, double weightMg) : base(weightMg)
{
this.JobsIcanDo = jobsICando;
}
public int ShiftsLeft
{
get { return shiftsToWork - shiftsWorked; }
}
private string currntJob = "";
public string CurrntJob
{
get { return currntJob; }
}
public bool DothisJob(string job, int numberOfshifts)
{
if (!string.IsNullOrEmpty(currntJob))
return false;
for (int i = 0; i < JobsIcanDo.Length; i++)
{
if (JobsIcanDo[i] == job)
{
currntJob = job;
this.shiftsToWork = numberOfshifts;
shiftsWorked = 0;
return true;
}
}
return false;
}
public bool DidYouFinish()
{
if (string.IsNullOrEmpty(currntJob))
return false;
shiftsWorked++;
if (shiftsWorked > shiftsToWork)
{
shiftsWorked = 0;
shiftsToWork = 0;
currntJob = "";
return true;
}
else
{
return false;
}
}
public override double HoneyConsumptionRate()
{
double consumption= base.HoneyConsumptionRate();
consumption += shiftsWorked * honeyUintsPerShiftWorked;
return consumption;
}
}
class Queen : Bee
{
private Worker[] Workers;
public Queen(Worker[] workers, double weightMg) : base(weightMg)
{
this.Workers = workers;
}
private int shiftNumber = 0;
public bool AssignWork(string job, int numberOfShifts)
{
for (int i = 0; i < Workers.Length; i++)
{
if (Workers[i].DothisJob(job, numberOfShifts))
{
return true;
}
}
return false;
}
public string WorkTheNextSift()
{
double honeyConsumed = HoneyConsumptionRate();
shiftNumber++;
string reportstr = "Report for shift #" + shiftNumber + "\r\n";
for (int i = 0; i < Workers.Length; i++)
{
honeyConsumed += Workers[i].HoneyConsumptionRate();
if (Workers[i].DidYouFinish())
reportstr += "Worker #" + (i + 1) + "finished the job\r\n";
if (string.IsNullOrEmpty(Workers[i].CurrntJob))
reportstr += "Worker #" + (i + 1) + "is not working\r\n";
else
{
if (Workers[i].ShiftsLeft > 0)
{
reportstr += "Worker #" + (i + 1) + "is doing " + Workers[i].CurrntJob + " for " + Workers[i].ShiftsLeft + " more shifts\r\n";
}
else
{
reportstr += "Worker #" + (i + 1) + "will be done with " + Workers[i].CurrntJob + " after this shifts\r\n";
}
}
}
reportstr += "Total honey consumed for the shift : " + honeyConsumed + " units\r\n";
return reportstr;
}
}