多个派生抽象类?

我必须创建一个课程管理系统,其中包括课程类别:

Cooking, sewing and writing courses 

cookingwriting每个都有2道菜(意大利,海鲜,创意写作和商业写作)。 这会创建派生的抽象:

 abstract course -> abstract cooking -> concrete seafood 

抽象的烹饪和写作有共同的领域和一些常用的方法,但是它们也有抽象的方法在基类中是抽象的。

这可以在C#中完成吗? 如果我使派生的抽象类方法抽象Visual Studio说他们隐藏基类抽象然后具体的类方法有错误说基类必须是抽象的(它必须但不能注册)。 我找了答案。 我知道单inheritance在C#中使用,但inheritance带有链。 什么是最好的答案?

这是一个代码片段 – 我希望它澄清了问题:

 public abstract class Course { public abstract void AddStudent(StudentName sn, int f); public abstract decimal CalculateIncome(); } public abstract class WritingCourse : Course { public void AddStudent(StudentName sn, int f) { //Add student } public abstract decimal CalculateIncome(); // can only be claculated in concrete } public class BusinessWritCourse : WritingCourse { public void AddStudent(StudentName sn, int f): base(sn, false){} public decimal CalculateIncome() { return //do stuff } } public class SewingCourse : Course { public override void AddStudent(StudentName sn, int f) { //do stuff } public override decimal CalculateIncome() { return //do stuff } } 

如果我使派生的抽象类方法抽象Visual Studio说他们隐藏基类抽象然后具体类方法有错误说基类必须是抽象的(它必须但不能注册)

如果你的基类“A”有一个抽象方法’b()’,那么你不必再将’b()’再次声明为B:A中的抽象,让C:B覆盖它,只需要’使用它。 即使你重写了B类中的’b()’方法,你也可以在类’c()’中重写它(甚至使用base。();来执行B的实现。

一些代码:

 public abstract class A{ public abstract void b(); } public class B : A{ public override void b() { //do stuff }; //overrides b from a public virtual void c() { //do stuff }; //creates an implemented method c in B that can be overriden by childs. public void d() { //do stuff}; } public class C : B{ public override void b() { //do stuff}; //overrides b from A, works! public override void c() {//do stuff}; //overrides c from B, works! public override void d() {//do stuff}; //doesn't work since d from B isn't abstract or virtual (hides it) public new void d() {//do stuff}; //works, hides d, but when you use inheritance this method will not be called, instead B's d() method will be called, only if you see C as the specific class C this will work } 

还要澄清:声明为abstract的方法必须被覆盖(就像在接口中一样,并且只能由声明抽象方法的类的直接子节点)。 声明为虚拟的方法可以被覆盖,但不一定是。

我认为使用接口而不是抽象类来解决这类问题更好:例如:

 // interface IInterface1 { void SameMethod(); } interface IInterface2 { void SameMethod(); } class TestClass : IInterface1, IInterface2 { void IInterface1.SameMethod() { // do one thing for Interface 1 } void IInterface2.SameMethod() { // do something elsefor Interface 2 } } class Program { static void Main(string[] args) { TestClass test = new TestClass(); IInterface1 i1 = test; i1.SameMethod(); // will call IInterface1.SameMethod() IInterface2 i2 = test; i2.SameMethod(); // will call IInterface2.SameMethod() } } 
 public class StudentName { } public abstract class Course { public abstract void AddStudent(StudentName sn, int f); public abstract decimal CalculateIncome(); } public abstract class WritingCourse : Course { override public void AddStudent(StudentName sn, int f) { //Add student } override public abstract decimal CalculateIncome(); // can only be claculated in concrete } public class BusinessWritCourse : WritingCourse { override public void AddStudent(StudentName sn, int f) { base.AddStudent(sn, 0); } override public decimal CalculateIncome() { return (decimal)3.4;//do stuff } } public class SewingCourse : Course { public override void AddStudent(StudentName sn, int f) { //do stuff } public override decimal CalculateIncome() { return (decimal)10; //do stuff } } class Program { static void Main(string[] args) { } } 

如果我理解正确,我认为您想要在要覆盖的抽象方法上使用’virtual’关键字?

如果你所说的错误是“某些方法隐藏了inheritance的成员,如果要隐藏就添加新的关键字”,那么基础方法上的虚拟和inheritance方法的覆盖将会:

 public abstract class BaseClass { public virtual void SomeMethod() { } } public abstract class InheritingClass : BaseClass { public override void SomeMethod() { } }