多个派生抽象类

本文关键字:抽象类 派生 | 更新日期: 2023-09-27 18:00:39

我必须创建一个课程类别为的课程管理系统

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

多个派生抽象类

如果我生成派生的抽象类方法摘要Visual Studio说它们隐藏基类抽象和那么具体的类方法表示基类必须为摘要(它是但不能注册)

如果你有一个基类"A",它有一个抽象的方法"b()",那么你不必在b:A中再次声明"b(;以执行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的方法可以被覆盖,但不必被覆盖。

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

//
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"关键字?

如果你谈论的错误是"某个方法隐藏继承的成员,如果打算隐藏,则添加新的关键字",那么在基本方法上的虚拟和在继承方法上的覆盖将起作用:

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

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