简体   繁体   English

C#中的继承/类

[英]Inheritance/classes in C#

I am new to C#, and are trying to understand classes and "is a" relationships. 我是C#的新手,正在努力理解课程和“是一种”关系。 I've been trying to get a piece of code to work, but I can't seem to get it right. 我一直试图让一段代码工作,但我似乎无法做到正确。

It looks like this: http://pastebin.com/cQDusptB 它看起来像这样: http//pastebin.com/cQDusptB

I have a base class, Mammal, which one should be able to create instances of, giving the name of the mammal as input. 我有一个基类,哺乳动物,其中一个应该能够创建实例,给出哺乳动物的名称作为输入。 I also have a class, Dog, which is a Mammal. 我还有一个班,狗,这是一个哺乳动物。 One should be able to create an instance of this too, in the same manner as Mammal. 应该能够以与Mammal相同的方式创建此实例。

Can anyone see my flaw(s)? 任何人都可以看到我的缺陷吗? If yes, can you please explain what i misunderstood/forgot? 如果是的话,你能解释我误解/忘记的内容吗?

When posting questions like this it is helpful to also post the error message the compiler gave you. 在发布这样的问题时,发布编译器给出的错误消息也很有帮助。

Can anyone see my flaw(s)? 任何人都可以看到我的缺陷吗?

You have a virtual constructor. 你有一个虚拟的构造函数。

can you please explain what i misunderstood/forgot? 你能解释一下我误解/忘记了什么吗?

Constructors are not inherited in C# and therefore it makes no sense to mark them as either virtual or override . 构造函数不是在C#中继承的 ,因此将它们标记为虚拟覆盖是没有意义的。

The constructor of Dog needs to be named "Dog". Dog的构造函数需要命名为“Dog”。 You can't/don't need to override "Mammal". 你不能/不需要覆盖“哺乳动物”。 Also, Mammal's constructor shouldn't be virtual. 此外,Mammal的构造函数不应该是虚拟的。

public class Mammal 
{

    public Mammal(string name)
    {
        //....
    }
}

public class Dog : Mammal {

    public Dog(string name) : base(name) 
    {
    }
}

You pass arguments to the base class constructor using the "base" keyword (see code above). 使用“base”关键字将参数传递给基类构造函数(请参阅上面的代码)。

Be sure to pick up a good book on C# to get the language basics right. 一定要拿一本关于C#的好书来正确掌握语言基础知识。

You have a few issues here. 你在这里有一些问题。 For one, constructors can not be marked as virtual or overridden. 例如,构造函数不能标记为虚拟或重写。 Another problem is that you attempt to call the method .Name without parentheses. 另一个问题是您尝试在没有括号的情况下调用方法.Name The corrected version looks like this: 更正后的版本如下所示:

public class Mammal
{
    protected string _Name;

    public virtual string Name()
    {
            return (this._Name + " - of type " + this.GetType());
    }
    public Mammal(string Name)
    {
        this._Name = Name;
    }
}
public class Dog : Mammal
{
    public Dog(string Name) : base(Name)
    {
    }

    public override string Name()
    {
        return (base._Name + "Dog");
    }
}

static void Main()
{
    Mammal AnimalA = new Mammal("SubjectA");
    Console.WriteLine("{0}", AnimalA.Name());

    Mammal AnimalB = new Dog("SubjectB");
    Console.WriteLine("{0}", AnimalB.Name());
    Console.ReadLine();
}

Constructors are not inherited/overridden. 构造函数不会被继承/覆盖。

  • Lose the 'virtual' keyword from the Mammal constructor in the Mammal class. 从Mammal类中的Mammal构造函数中丢失'virtual'关键字。
  • Remove the Mammal ctor override attempt in the Dog class. 删除Dog类中的Mammal ctor覆盖尝试。
  • Replace it with a Dog constructor that calls the Mammal one (like this): 将它替换为调用Mammal的Dog构造函数(如下所示):

public Dog(string name) : base(name) { public Dog(string name):base(name){

} }

Now when a dog is constructed it will call through to the mammal base class constructor passing the specified name. 现在,当构建一条狗时,它将调用传递给指定名称的哺乳动物基类构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM