C# 多个派生抽象类?

C# 多个派生抽象类?,c#,.net,c#-4.0,abstract-class,multiple-inheritance,C#,.net,C# 4.0,Abstract Class,Multiple Inheritance,我必须创建一个包含课程类别的课程管理系统: Cooking, sewing and writing courses 烹饪和写作各有2门课程(意大利语、海鲜、创意写作和商务写作)。这将创建派生摘要: abstract course -> abstract cooking -> concrete seafood 抽象烹饪和写作有共同的领域和一些共同的方法,但他们也有抽象的方法是抽象的基类 这可以用C语言完成吗?如果我使派生的抽象类方法抽象,VisualStudio会说它们隐藏基类抽象

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

Cooking, sewing and writing courses
烹饪
写作
各有2门课程(意大利语、海鲜、创意写作和商务写作)。这将创建派生摘要:

abstract course -> abstract cooking -> concrete seafood
抽象烹饪和写作有共同的领域和一些共同的方法,但他们也有抽象的方法是抽象的基类

这可以用C语言完成吗?如果我使派生的抽象类方法抽象,VisualStudio会说它们隐藏基类抽象,然后具体的类方法会有错误,说基类必须是抽象的(它是抽象的,但不能注册)。我一直在寻找答案。我知道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
    }
}

如果我理解正确的话,我认为您应该在要重写的抽象方法上使用“virtual”关键字

如果您讨论的错误类似于“某个方法隐藏继承的成员,如果打算隐藏,请添加新关键字”,则基本方法上的虚拟和继承方法上的重写将执行以下操作:

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


public abstract class InheritingClass : BaseClass
{
    public override void SomeMethod()
    {
    }
}
如果我创建派生的抽象类 VisualStudio说的方法摘要 它们隐藏了基类和抽象类 然后,具体的类方法 表示基类必须是 摘要(是但不得注册)

如果基类“A”有一个抽象方法“b()”,那么不必在b:A中将“b()”再次声明为抽象,只要不使用它,就可以让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
}

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

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


你能发布你遇到的问题的示例代码吗?只是为了澄清-正如你所看到的,我有一个来自a的具体类A1和一个抽象A2-即使它在A2中是抽象的,我不想实现它-我必须…我不能将抽象方法从A2传递到B…我曾想过使用接口-但是在d期间讨论会一个学生提到了它们,讲师说不要寻找没有实现的方法。。。
//
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)
    {

    }
}