简体   繁体   English

类继承/方法覆盖

[英]Class Inheritance/Method Override

This is my first time working with classes, so excuse my ignorance. 这是我第一次上课,所以请原谅我的无知。

I have a Pet class that is my base class. 我有一个Pet类,它是我的基类。 I have two children classes, Dog and Cat. 我有两个孩子课,狗和猫。 What I'm trying to do is have the Cat and Dog methods override the Pet method by saying "Woof!" 我想要做的是让Cat和Dog方法通过说“Woof!”来覆盖Pet方法。 and "Meow!" 和“喵!” instead of speak. 而不是说话。 Then in another form I have to print the information (name, color, and them speaking) on a button press. 然后在另一种形式中,我必须在按下按钮上打印信息(名称,颜色和说话)。

       class Pet
    {
        protected string name, color, food;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Color
        {
            get 
            {
                return color;
            }
            set
            {
                color = value;
            }
        }
        public string Food
        {
            get
            {
                return food;
            }
            set
            {
                food = value;
            }
        }
        public void speak(string s)
        {
            s = "Speak";
            MessageBox.Show(s);
        }

        public Pet(string name, string food, string color)
        {
            //Constructor
            this.Food = food;
            this.Name = name;
            this.Color = color;
        }




    class Dog : Pet
    {

        public Dog(string name, string food, string color)
            : base(name, food, color)
        {

        }

        protected override void speak()
        {

        }

    }

}

(left out the cat because it's the same as dog pretty much) (遗漏了猫,因为它和狗差不多)

I keep getting the error "Error 1 'Lab12.Cat.speak()': cannot change access modifiers when overriding 'public' inherited member 'Lab12.Pet.speak()' " 我一直收到错误“错误1'Lab12.Cat.speak()':覆盖'public'继承成员'Lab12.Pet.speak()'时无法更改访问修饰符

What am I doing wrong? 我究竟做错了什么? I know it has to do with protection levels somewhere, and I keep switching things from public to protected or private, but it isn't fixing anything. 我知道它与某些地方的保护级别有关,而且我一直将事物从公共转换为受保护或私有,但它并没有修复任何东西。 Help, anybody? 帮忙,有人吗?

Since Speak() was originally public, you need to keep it public. 由于Speak()最初是公开的,因此您需要将其公开。 You "cannot change access modifier" (public vs private). 你“无法改变访问修饰符”(公共与私人)。

Also, you cannot override a non-virtual or static method. 此外,您不能覆盖非虚拟或静态方法。 The overridden base method must be virtual, abstract, or override. 重写的基本方法必须是虚拟,抽象或覆盖。

Take a read: http://msdn.microsoft.com/en-us/library/ebca9ah3(v=vs.100).aspx 阅读: http//msdn.microsoft.com/en-us/library/ebca9ah3(v = vs.100).aspx

The Speak method will have to be virtual in your base class to override Speak方法必须在您的基类中是虚拟的才能覆盖

Pet class 宠物类

  public virtual void speak(string s)
  {
      s = "Speak";
      MessageBox.Show(s);
  }

and you must use the same modifier (public) 你必须使用相同的修饰符(公共)

Dog Class 狗类

  public override void speak(string s)
  {
     base.speak(s);
  }
protected override void speak()
    {

    }

Pretty sure it is because you are changing a public method to protected in the subclass. 很确定这是因为您正在将子public方法更改为protected的子类。

There error is telling you that you cannot change the type of access when overriding the method. 有错误告诉您在覆盖方法时无法更改访问类型。 So, to fix this just keep the method as public in Cat and Dog : 所以,要解决这个问题,只需将方法保持为CatDog公共方法:

public override void speak()
    {

    }

Well, the reason why you are getting that error is that you are inheriting the "speak" method from the parent class Pet. 那么,你得到这个错误的原因是你从父类Pet继承了“speak”方法。 You have declared speak() method as a public method and then you are inheriting it and making in protected. 您已将speak()方法声明为公共方法,然后继承它并使其处于受保护状态。 I suggest you leave it public once you inherit that and override that in the class Dog, Cat, Monkey. 我建议你继承它并将其覆盖在Dog,Cat,Monkey类中。

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

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