不实现继承的抽象类 [英] does not implement inherited abstract class

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

问题描述

行。所以我得到了一个处理书籍和电影的程序。现在我有1个基类,2个继承自那个。



当我尝试运行它时,我得到没有实现继承的抽象成员



baseclass:

OK. So I got a program handling books and movies. Now I have 1 base class, and 2 that inherit from that.

When I try to run it I get "does not implement inherited abstract member"

baseclass:

public abstract class saker
   {
       public string titel;
       public string genre;
       public int pris;
       public abstract string book(string t, string g, int p, int page);
       public abstract string movie(string t, string g, int p, double l);
       public abstract string visainfo();
   }







级电影:




class film:

class  film : saker
  {
          double langd;
          public override string movie(string t, string g, int p, double l)
          {
              this.langd = l;
              this.titel = t;
              this.genre = g;
              this.pris = p;
          }
          public override string visainfo()
          {
              string s;
              s = "Titel: " + titel + " Genre: " + genre + " Pris: " + pris + "kr Langd: " + langd;
              return s;
          }
  }







现在我做错了什么?





啊啊没有它,我只是忘了添加代码片段




Now what am I doing wrong?


ahh no its there, i just forgot to add the code snippet

public class bok : saker
{
    int sidor;
    public override string book(string t, string g, int p, int page)
    {
        this.sidor = page;
        this.titel = t;
        this.genre = g;
        this.pris = p;
    }
    public override string visainfo()
    {
        string s;
        s = "Titel: " + titel + " Genre: " + genre + " Pris: " + pris + "kr Sidor: " + sidor;
        return s;
    }
}





[/编辑]



[/Edit]

推荐答案

您错过了预订,这也需要实施。



基于您更新的问题,您不能在多个类上实现抽象方法,其中没有单个类实现所有这些。这不是它的工作原理。每个派生类都需要实现基类中的所有抽象方法。
You missed book, that needs to be implemented too.

Based on your updated question, you cannot implement the abstract methods over multiple classes where no single class implements all of them. That's not how it works. Every derived class needs to implement all the abstract methods from the base class.


你的saker类有三个抽象成员。你必须在每个后代中实现所有三个。



所以你的Movie类必须实现Book和Movie,你的Book类必须实现Movie和Book。 br />


看起来你需要重新设计你的saker类。
Your saker class has three abstract members. You have to implement all three in each descendant.

So your Movie class must implement Book as well as Movie and your Book class must implement Movie as well as Book.

It looks like you need to redesign your saker class.


这是另一个设计建议:



Here's an alternate design suggestion:

public interface IBook
{
    string book(string t, string g, int p, int page);
}
interface IMovie
{
    string movie(string t, string g, int p, double l);
}
public abstract class saker
{
    public string titel;
    public string genre;
    public int pris;
    public abstract string visainfo();
}
public class Book : saker, IBook
{
    public override string visainfo()
    {
        throw new NotImplementedException();
    }
    public string book(string t, string g, int p, int page)
    {
        throw new NotImplementedException();
    }
}
public class Movie : saker, IMovie
{
    public override string visainfo()
    {
        throw new NotImplementedException();
    }
    public string movie(string t, string g, int p, double l)
    {
        throw new NotImplementedException();
    }
}


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

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