类型"MyProject.Bike"不包含采用'0'参数的构造函数C# [英] The type "MyProject.Bike" does not contain a constructor that takes '0' arguments C#

查看:59
本文介绍了类型"MyProject.Bike"不包含采用'0'参数的构造函数C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题也许很简单,但是我刚开始用C#编程.

My problem is perhaps pretty simple, but I just started programming in C#.

如上所述,我的问题是:类型"MyProject.Bike"不包含采用'0'参数的构造函数".

My problem is, as listed above: "The type "MyProject.Bike" does not contain a constructor that takes '0' arguments".

我不明白,因为我不尝试用空括号对此构造函数进行调用,并且阅读类似的问题都用您必须拥有太多/没有足够的参数..."来回答.

I don't understand, because I don't try to make a call to this constructor with empty parentheses, and reading similar questions were all answered with "You have to much/not enough parameters...".

我只是为了学习而做的,但是我认为继承是一个非常重要的概念,所以我希望能够做到这一点...

I just made this for learning, but I think inheritance is a pretty important concept, so I would like to be able to do that...

我的代码:

using System;  
namespace MijnConsoleProject
{
    public class Bike
    {
        protected int speed = 0;
        public String name
        {
            get;
            set;
        }
        public void speedUp(int increment)
        {
            this.speed += increment;
        }
        public void slowDown(int decrement)
        {
            this.speed -= decrement;
            {

        public override string ToString ()
        {
            return name + ": speed = " + speed;
        }

        public Bike(int initSpeed)
        {
            this.speed = initSpeed;
        }
    }

    public class GearedBike : Bike
    {
        private int gear;

        public GearedBike(string name)
        {
            this.name = name;
        }

        public bool changeGear(int gear)
        {
            if(gear < 8 && gear > 0)
            {
                this.gear = gear;
                return true;
            }
            else
            {
                return false;
            }
        }

        public override string ToString ()
        {
             return name + ": speed=" + speed + ", gear=" +gear;
        }

        public static void main(String[] args)
        {
            Bike fiets = new Bike(10);
            Console.WriteLine("[Normal Bike:]");
            Console.WriteLine("{0}\n", fiets);

            GearedBike fiets2 = new GearedBike("Fiets");
            Console.WriteLine("[Geared Bike:]");
            Console.WriteLine("{0}\n", fiets2);
        }
    }
}

推荐答案

您的 Bike 类只有一个构造函数:

Your Bike class only has one constructor:

    public Bike(int initSpeed)
    {
        this.speed = initSpeed;
    }

这需要一个参数.

当您派生一个类时,派生类的构造函数会从基类中调用一个构造函数.

When you derive a class that derived class' constructor calls a constructor from the base class.

在您的 GearedBike 类的构造函数中,您没有指定要调用的 Bike 构造函数,因此编译器假定为 Bike(),不存在.

In your GearedBike class' constructor you don't specify which constructor of Bike to call so the compiler assumes Bike(), which doesn't exist.

您可能想要类似下面的内容,我们在其中指定要调用的基本构造函数,并传递适当的值.

You probably want something like below, where we specify what base constructor to call, and pass in an appropriate value.

    public GearedBike(string name)
        : base(0)
    {
        this.name = name;
    }

您可能还需要一个 GearedBike 构造函数,您可以在其中设置速度和名称,如下所示:

You might also want a GearedBike constructor where you can set the speed and name, like below:

    public GearedBike(string name, int initSpeed)
        : base(initSpeed)
    {
        this.name = name;
    }

这篇关于类型"MyProject.Bike"不包含采用'0'参数的构造函数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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