簡體   English   中英

多個派生抽象類?

[英]Multiple derived abstract classes?

我必須創建一個包含課程類別的課程管理系統:

Cooking, sewing and writing courses

cookingwriting各有 2 門課程(意大利語、海鮮、創意寫作和商務寫作)。 這將創建派生摘要:

abstract course -> abstract cooking -> concrete seafood

抽象的烹飪和寫作有共同的領域和一些共同的方法,但是它們也有抽象的方法,在基礎 class 中是抽象的。

這可以在 C# 中完成嗎? If I make the derived abstract class methods abstract Visual Studio says they hide the base class abstract and then the concrete class methods have errors saying the base class must be abstract (it is but must not register). 我一直在尋找答案。 我知道單個 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
    }
}

if I make the derived abstract class methods abstract Visual Studio says they hide the base class abstract and then the concrete class methods have errors saying the base class must be abstract (it is but must not register)

如果你有基礎 class 'A' ,它有一個抽象方法 'b()' 那么你不必在 B:A 中再次聲明 'b()' 作為抽象,讓 C:B 覆蓋它,只是不要'不要使用它。 即使您覆蓋 class B 中的 'b()' 方法,您也可以在 class '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
}

還要澄清:聲明為抽象的方法必須被覆蓋(就像在接口中一樣,並且只能由聲明抽象方法的 class 的直接子代)。 聲明為 virtual 的方法可以被覆蓋,但不是必須的。

我認為這種問題最好使用接口而不是抽象類來解決:例如:

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

    }
}

如果我理解正確,我認為您想在要覆蓋的抽象方法上使用“虛擬”關鍵字?

如果您正在談論諸如“某些方法隱藏繼承的成員,如果打算隱藏則添加新關鍵字”之類的錯誤,那么基方法上的虛擬和繼承方法上的覆蓋將執行以下操作:

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


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM