简体   繁体   English

从基础 class C# 继承字段

[英]Inheriting fields from a base class C#

I'm having trouble getting a class to inherit fields from its parent class.我无法让 class 从其父 class 继承字段。

I want the BullFrog class and Toad class to inherit all attributes from Amphibian class, and then overwrite the weight attribute.我想让 BullFrog class 和 Toad class 继承 Amphibian class 的所有属性,然后覆盖权重属性。 I thought it inherited all traits from the parent class by default, what am I missing here?我认为它默认继承了父 class 的所有特征,我在这里缺少什么?

public class Animal
    {
        public string sound { get; set; }
        public string move { get; set; }
        public string favSnack { get; set; }
        public double avgWeight { get; set; }
        public void Speak(string sound)
        {
            Console.WriteLine( $"I go {sound}.");
        }
        public void Move(string move)
        {
            Console.WriteLine($"I {this.move} around the farm all day.");
        }
        public void Eat(string favSnack)
        {
            Console.WriteLine($"I like to eat {favSnack}.");
        }
        public void Weight(int avgWeight)
        {
            Console.WriteLine($"I usually weigh about {avgWeight}lbs.");
        }
      
    }
    //decide on four or more animals
    //for each animal decide on four or more methods
    public class Amphibian : Animal
    {
        string sound = "ribbit";
        string move = "hop";
        string favSnack = "flies";
        double avgWeight = .05d;

    }
    public class BullFrog : Amphibian 
    {
        double avgWeight = .375d;
    }

    public class Toad : Amphibian
    {
        double avgWeight = .175d;
    }
    

where did I go wrong?我在哪里 go 错了? when I call the methods当我调用方法时

BullFrog kermit = new BullFrog();
            kermit.Move(kermit.movement);
            kermit.Eat(kermit.favSnack);

I get "I around the farm all day" and two blank lines as the output.我得到“我整天在农场”和两个空白行作为 output。 My question is我的问题是

Check the virtual and override keywords in C#.检查 C# 中的virtualoverride关键字。 This is to be used in case you want to actually change the way the derived class moves.如果您想实际更改派生的 class 的移动方式,则可以使用此选项。 Go with the constructor way if it's just a string change. Go 如果只是字符串更改,则使用构造函数方式。

public class Animal
{
     public virtual string move { get; set; }
public class Amphibian : Animal
{
     public override string move { get; set; } =  "hop";

Your line string move = "hop";你的线string move = "hop"; is just declaring a private property inside the Amphibian class, which will not affect the base class in any way.只是在Amphibian class 中声明了一个私有属性,这不会以任何方式影响基础 class。

Another way to handle this would be to set the value in a constructor, but you should remember to always set the value in any constructor you are creating.另一种处理方法是在构造函数中设置值,但您应该记住始终在您正在创建的任何构造函数中设置值。

public class Animal
{
     public string move { get; set; }
 
     public Animal(string move) {
         this.move = move;
     }

And then接着

public class Amphibian : Animal
{
     public Amphibian(): base("hop") {
     }

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

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