帮助重写与传承 [英] Help With Overriding and Inheritance

查看:153
本文介绍了帮助重写与传承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,听我慢慢家伙和女孩,我正在学习。这里是我的问题。

Ok, bear with me guys and girls as I'm learning. Here's my question.

我想不通为什么我不能重写从父类的方法。下面是从基类(是的,我偷从OOP书的Java代码,我试图重写它在C#)的代码。

I can't figure out why I can't override a method from a parent class. Here's the code from the base class (yes, I pilfered the java code from an OOP book and am trying to rewrite it in C#).

using System;

public class MoodyObject
{
    protected String getMood()
    {
        return "moody";
    }

    public void queryMood()
    {
        Console.WriteLine("I feel " + getMood() + " today!");
    }
}

和这里有我的其他2个对象继承基类(MoodyObject):

and here are my other 2 objects that inherit the base class (MoodyObject):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class SadObject: MoodyObject
    {
        protected String getMood()
        {
            return "sad";
        }

        //specialization
        public void cry()
        {
            Console.WriteLine("wah...boohoo");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class HappyObject: MoodyObject
    {
        protected String getMood()
        {
            return "happy";
        }

        public void laugh()
        {
            Console.WriteLine("hehehehehehe.");
        }
    }
}

和这里是我的主:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MoodyObject moodyObject = new MoodyObject();
            SadObject sadObject = new SadObject();
            HappyObject happyObject = new HappyObject();

            Console.WriteLine("How does the moody object feel today?");
            moodyObject.queryMood();
            Console.WriteLine("");
            Console.WriteLine("How does the sad object feel today?");
            sadObject.queryMood();
            sadObject.cry();
            Console.WriteLine("");
            Console.WriteLine("How does the happy object feel today?");
            happyObject.queryMood();
            happyObject.laugh();
        }
    }
}



正如你所看到的,漂亮基本的东西,但这里的输出:

As you can see, pretty basic stuff, but here's the output:

如何穆迪今天物体的感觉?
我今天感觉喜怒无常!

How does the moody object feel today? I feel moody today!

如何悲伤的对象今天感觉如何?我
今天感觉喜怒无常!华...号泣

How does the sad object feel today? I feel moody today! wah...boohoo

如何幸福的对象今天感觉如何?
我今天感觉喜怒无常! hehehehehehe。
按任意键继续。 。

How does the happy object feel today? I feel moody today! hehehehehehe. Press any key to continue . . .

不是如我所料。我试图使基虚拟方法,并试图覆盖它,这只是让我这个错误时调用覆盖不能覆盖继承成员MoodyObject.getMood()',因为它未标记的虚拟,抽象或覆盖。我也试了一下没有虚拟和覆盖,它认为我试图隐藏基方法。同样,我是新来的OOP并希望任何指导

Not as I expected. I've tried to make the base method virtual and calling override when trying to override it and that just gets me this error "cannot override inherited member 'MoodyObject.getMood()' because it is not marked virtual, abstract, or override". I also tried it without the virtual and override and it thinks I'm trying to hide the base method. Again, I'm new to OOP and would appreciate any guidance.

编辑补充:我找到了!该MoodyObject.cs只是在解决方案资源管理解决方案项目,而不是一个ConsoleApplication1项目。我拖下来到它在Solution Explorer中,瞧属于!它的工作原理吧。我标志着吕克的回答下面的答案,因为他提供了我需要去的地步,我已经解决了它......我学了这么多在这里帮助。这是惊人的,你们和女孩是疯狂的聪明!

推荐答案

在C#方法默认情况下不虚,那么如果你设计一些方法重写,你应该将它指定为虚拟的:

In C# methods are not virtual by default, so if you design some method as overridable, you should specify it as virtual:

class Base 
{
  protected virtual string GetMood() {...}
}

二,你要指定你要在派生类的基类重写方法。

Second, you have to specify that you are going to override method from base class in derived class.

class Derived : Base
{
  protected override string GetMood() {...}
}

如果你不指定覆盖的关键字,你会得到方法,隐藏基地类型(从编译器警告将新的关键字进行明确说明这样的方法)。

If you don't specify "override" keyword, you will get method that hides base type (and warning from compiler to put "new" keyword for the method to explicitly state so).

如果要停止继承链并禁止该方法的进一步覆盖作为密封,这样你应该标注方法:

If you want to stop inheritance chain and disallow further overrides of the method, you should mark method as sealed, like this:

  protected sealed override string GetMood() {...}

这篇关于帮助重写与传承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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