首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个派生抽象类?

多个派生抽象类?
EN

Stack Overflow用户
提问于 2011-06-30 14:57:26
回答 4查看 26.4K关注 0票数 7

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

代码语言:javascript
复制
Cooking, sewing and writing courses

cookingwriting各有2门课程(意大利语、海鲜、创意写作和商业写作)。这将创建派生的摘要:

代码语言:javascript
复制
abstract course -> abstract cooking -> concrete seafood

抽象的烹饪和写作有共同的字段和一些共同的方法,但它们也有抽象的方法,这些方法在基类中是抽象的。

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

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

代码语言:javascript
复制
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
    }
}
EN

回答 4

Stack Overflow用户

发布于 2011-06-30 15:07:32

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

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

下面是一些代码:

代码语言:javascript
复制
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的方法可以被重写,但不一定要重写。

票数 7
EN

Stack Overflow用户

发布于 2011-06-30 15:12:38

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

代码语言:javascript
复制
//
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()

    }
}
票数 4
EN

Stack Overflow用户

发布于 2017-02-01 13:03:40

代码语言:javascript
复制
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)
    {

    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6531041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档