多个派生抽象类?

发布于 2024-11-17 14:49:53 字数 1277 浏览 0 评论 0原文

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

Cooking, sewing and writing courses

烹饪写作各有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
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤单情人 2024-11-24 14:49:53

如果我创建派生抽象类
方法抽象 Visual Studio 说
他们隐藏了基类抽象并且
那么具体的类方法有
错误说基类必须是
摘要(可以但不能注册)

如果你有基类'A',它有一个抽象方法'b()',那么你不必在B中再次声明'b()'作为抽象:A, 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
}

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

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.

千秋岁 2024-11-24 14:49:53

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

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

    }
}

I think this kind of problems is better to resolve using interfaces and not abstract classes:
Ex:

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

    }
}
左秋 2024-11-24 14:49:53
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 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)
    {

    }
}
浅忆流年 2024-11-24 14:49:53

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

如果您正在谈论类似“某些方法隐藏继承成员,如果打算隐藏,则添加 new 关键字”的错误,那么基本方法上的 virtual 和继承方法上的 override 就可以了:

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


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

If I understand correctly I think you'd want to use the 'virtual' keyword on abstract methods you want to override?

If you are talking about the error that says something like "some method hides inherited member, add the new keyword if hiding was intended", then virtual on the base method and override on the inheriting method will do:

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


public abstract class InheritingClass : BaseClass
{
    public override void SomeMethod()
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文