C#中的重写和继承 [英] Overriding and Inheritance in C#

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

问题描述

好吧,在我学习的过程中,请和我一起拥抱男孩和女孩.这是我的问题.

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!");
    }
}

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

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

今天快乐的对象感觉如何? 我今天心情不好!呵呵呵呵呵呵 按任意键继续 . .

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

与我预期的不同.我试图使基本方法成为虚拟方法,并在尝试覆盖它时调用覆盖,这只是让我收到此错误无法覆盖继承的成员'MoodyObject.getMood()',因为它没有标记为虚拟,抽象或覆盖".我也尝试了不使用virtual和override的情况,并且认为我正在尝试隐藏基本方法.再次,我是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"项.我将其拖到解决方案资源管理器中的位置,瞧!现在可以使用了.我在下面将Luc的答案标记为答案,因为他提供了解决问题所需要的帮助...我在这里学到了很多东西.真是太神奇了,你们男孩和女孩都疯了聪明!

推荐答案

在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() {...}
}

如果不指定"override"关键字,则将获得隐藏基本类型的方法(并警告编译器输入"new"关键字,以便该方法明确声明为该状态).

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() {...}

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

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