The type “MyProject.Bike” does not contain a constructor that takes '0' arguments C#

陌路散爱 提交于 2021-02-10 22:18:10

问题


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

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...

My code:

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

回答1:


Your Bike class only has one constructor:

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

This takes a single parameter.

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

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;
    }

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;
    }



回答2:


The constructor public GearedBike(string name) { ... } is a shorthand for public GearedBike(string name) : base() { ... }

You have to call the base-class constructor with an argument from your sub-class constructor or add a 0-param constructor to your base-class.

public GearedBike(string name) : base(42) {
  // ...
}



回答3:


The constructor are not inherited so in the class the extends bike you have to call the base class construcotr:

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

you don't need to do that if your base class has got a parameter less constructor




回答4:


GearedBike does not call a constructor on the base class. If you do not explicitly call the constructor on the base class, the compiler will call the default constructor on the base class. The base class does not contain a default constructor; therefor you receive an error stating such.




回答5:


C# compiler can generate a default constructor which may look like

public Bike()
{

}

This will only happen if the type doesn't have any user-defined constructors. In your case the class Bike has a constructor that takes an int:

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

Thefore compiler doesn't generate you a default parameterless constructor.


Later in your code a type GearedBike use the parent type's constructor by calling :base() (which is added by the compiler) and this call fail as there is no parameterless constructor in the base class.



来源:https://stackoverflow.com/questions/9097767/the-type-myproject-bike-does-not-contain-a-constructor-that-takes-0-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!