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

问题描述

我刚开始使用C#,并在名称空间下创建了一个动物类,并在它之后创建了Dog类(仍然在名称空间中),当我尝试覆盖名为toString的函数时,我无法,我在课堂上这样声明了toString 动物:

I have just started with C# and have created an animal Class underneath the namespace, and have created a Dog class after it(Still in the namespace), when I try to override a function called toString, I am unable, I have declared toString like this in class animal:

public virtual string toString()
        {
            return string.Format("{0} lives in {1} and has a weight of {2} kgs and is {3} meters tall", name, habitat, weight, height);
        }

在狗里,我这样宣告

public override string toString()
        {
            return string.Format("{0} is a {1} and is owned by {2}, weighs {3} kgs and is {4} meters tall, and loves to eat {5}, and naturally lives in ", name, breed, owner, weight, height, favFood, habitat);
        }

但是,当我这样做时,Visual Studio会抛出错误,声明我无法从方法组转换为布尔值:

But visual studio throws errors declaring that I cannot convert from method group to bool, when I do this:

static void Main(string[] args)
        {
            animal giraffe = new animal(7, 200, "Giraffe", "Africa");

            Dog arik = new Dog("Cocker spaniel", "Tom", "Stew", 0.50, 10, "arik", "The americas");

            Console.WriteLine(arik.toString);

        }

感谢您提供的任何帮助!


Thanks for any of the help you may provide!


推荐答案

控制台 . WriteLine ( ark . toString () );


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

10-27 23:54