2011-06-30 64 views
7

我必须与课程类别创建一个课程管理系统:多派生抽象类?

Cooking, sewing and writing courses 

cookingwriting各有2个疗程(意大利,海鲜,创意写入和商业写)。这将创建衍生摘要:

abstract course -> abstract cooking -> concrete seafood 

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

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

下面的代码片段 - 我希望澄清的问题:

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 
    } 
} 
+1

你可以用你遇到的问题发布示例代码吗? –

回答

0

如果我理解正确的话,我认为你想使用上要覆盖抽象方法的“虚拟”的关键字?

如果你谈论的是一个类似于“一些方法隐藏继承成员,添加新的关键字,如果隐藏之意”,然后在基方法virtual和override在继承的方法做错误:

public abstract class BaseClass 
{ 
    public virtual void SomeMethod() 
    { 
    } 
} 


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

这里是一个代码片段 - 也许它会澄清 – Julesyw

7

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

如果您有基类'A',它具有抽象方法'b()',那么您不必在B:A中再次声明'b()'为抽象,以便拥有C:B重写它,只是不要使用它。即使你覆盖“B()”中的B类方法,你可以再次重写它在课堂“C()”(甚至使用基地();执行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 
} 

另外要说明的是:声明抽象的方法必须被重写(就像在一个接口中一样,只能由声明抽象方法的类的直接子类)声明为virtual的方法可以被overriden,但不一定是

+0

嘿 - 感谢那... – Julesyw

+0

只是澄清 - 你可以看到我有一个具体的类A1来自A以及一个抽象的A2 - 即使它是抽象的在A2钕我不想实现它 - 我必须......我不能通过抽象的方法,然后从A2到B ... – Julesyw

4

我觉得这样的问题比较好解决使用接口而不是抽象类: 例如:

// 
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() 

    } 
} 
+0

我曾想过使用一个接口 - 但是在讨论板上一个学生提到他们,讲师说不要去寻找不在那里的实现... – Julesyw

1
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) 
    { 

    } 
}