多个派生抽象类? [英] Multiple derived abstract classes?

查看:118
本文介绍了多个派生抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个课程管理系统的课程类别:

 烹饪,缝纫和写作课程
 

烹饪各有2个疗程(意大利,海鲜,文学创作和商业写)。这将创建派生摘要:

 抽象的过程 - >抽象的烹饪 - >具体海鲜
 

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

这个问题能在C#中做了什么?如果我让派生抽象类的方法摘要Visual Studio中说,他们隐藏了基类抽象,然后在具体的类中的方法存在错误说法的基类必须是抽象的(它可是绝对不能注册)。我一直在寻求一个答案。我知道单继承是用在C#中,但继承进行​​环比下跌。什么是最好的答案吗?

下面是一个code段 - 我希望澄清的问题:

 公共抽象类课程
{
    公共抽象无效传递addStudent(StudentName SN,INT F);
    公共抽象十进制CalculateIncome();
}

公共抽象类WritingCourse:课程
{
    公共无效传递addStudent(StudentName SN,INT F)
    {
     //添加学生
    }
    公共抽象十进制CalculateIncome(); //只能在混凝土被claculated
}

公共类BusinessWritCourse:WritingCourse
{
    公共无效传递addStudent(StudentName SN,INT F):
    基地(SN,FALSE){}

    公共十进制CalculateIncome()
    {
       返回//做的东西
    }
}

公共类SewingCourse:课程
{
    公众覆盖无效传递addStudent(StudentName SN,INT F)
    {
        //做的东西
    }

    公众覆盖小数CalculateIncome()
    {
       返回//做的东西
    }
}
 

解决方案
  

如果我做了派生抽象类   方法摘要Visual Studio的说   他们隐藏在基类的抽象   那么具体类的方法有   错误说法的基类必须是   抽象的(这是但绝不能注册)

如果你有基类A其中有一个抽象方法'B()',那么你不必申报'B()再次在抽象B:A,有C:将B覆盖它,只是不使用它。即使你覆盖'B()'B类的方法,你可以再次重写它在课堂上'C()'(甚至使用基();执行B的实施

有些code:

 公共抽象类A {
    公共抽象无效B();
}

大众B级:A {
    公众覆盖无效B(){//做的东西}; //覆盖b从一个
    公共虚拟无效C(){//做的东西}; //创建B中,可以通过孩子的被覆盖的实现方法C。
    公共无效D(){//做的东西};
}

公共类C:B {
    公众覆盖无效B(){//做的东西}; //覆盖b从A,作品!
    公众覆盖无效C(){//做的东西}; //覆盖c从B,有效!
    公众覆盖无效D(){//做的东西}; //不起作用,因为d从B不是抽象的或虚拟的(隐藏它)
    大众新无效D(){//做的东西}; //作品,隐藏研发,但是当你使用继承此方法就不会被调用,而不是B的D()方法将被调用,只有当你看到C为特定的C类,这将工作
}
 

另外要澄清:声明抽象方法必须被重写(就像在一个接口,只有由类声明的抽象方法的直接子)。声明虚方法可以重写,但不必须是。

I have to create a Course Management System with course categories:

Cooking, sewing and writing courses

cooking and writing each have 2 courses (Italian, seafood, creative write and business write). This creates derived abstract:

abstract course -> abstract cooking -> concrete seafood

The abstract cooking and writing have common fields and some common methods, however they also have abstract methods that are abstract in the base class.

Can this be done in 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). I have looked for an answer. I know single inheritance is used in C# but inheritance carries down the chain. What is the best answer?

Here is a code snippet - I hope it clarifies the problem:

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)

If you have base class 'A' which has an abstract method 'b()' then you don't have to declare 'b()' again as abstract in B : A, to have C : B override it, just don't use it. Even if you override the 'b()' method in class B you can again override it in class 'c()' (and even use base.(); to execute B's implementation.

Some code:

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
}

Also to clarify: methods declared abstract must be overriden (just like in an interface, and only by the direct child of the class declaring the abstract method). Methods declared virtual can be overriden, but don't have to be.

这篇关于多个派生抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆