如何清除 CS0029 C# 无法将类型 'void' 隐式转换为 'int' 和 CS1729 C# 'Car'不包含接受 4 个参数的构造函数



我无法弄清楚要更改什么。我玩过void and int。

问题是使用SlowDownSpeedUp方法,在行Car car1 = new Car("Ford", "Focus", 2010, car1Speed);中,CS1729在("Ford", "Focus", 2010, car1Speed)之前标记了汽车。

如何解决这些错误?

class Car
{
    private int Speed;
    private string Make;
    private string Model;
    private int Year;
    public void Car1(string make, string model, int year, int speed)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = speed;
    }
    public void Car2(string make, string model, int year, int speed)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = 0;
    }
    public void SpeedUp()
    {
        Speed = Speed ++;
    }

    public void SlowDown()
    {
        Speed = Speed --;
    }
    public void Display()
    {
        Console.WriteLine(Year + " " + Make + " " + Model + " is going " + Speed + " MPH.");
    }
    public static void Main(string[] args)
    {
        int car1Speed = 20;
        int car2Speed = 0;
        Car car1 = new Car("Ford", "Focus", 2010, car1Speed);
        Car car2 = new Car("Chevy", "Cruze", 2018, car2Speed);
        for (int i = 0; i < 60; i++)
        {
            if (i % 2 == 0)
            {
                car2Speed = car2.SpeedUp();
            }
            if (i % 3 == 0)
            {
                car1Speed = car1.SpeedUp();
            }
            if (i % 5 == 0)
            {
                car1Speed = car1.SlowDown();
                car2Speed = car2.SlowDown();
            }
        }
        car1.Display();
        car2.Display();
    }
}

}

从您在您的评论中收集的内容中,这似乎是这些不好的作业他们教给您不好的编码实践书面代码不佳。这是灾难性的做法,因为不良习惯并不容易出现。这是应该写的完整代码,并评论为什么应该这样。

public class Car
{
    // Member fields should be either "_camelCase" or "camelCase"
    private string _make;
    private string _model;
    private int _year;
    private int _speed;
    // Don't duplicate the code, default value for speed makes it optional parameter. 
    // There is no need for a second constructor.
    public Car(string make, string model, int year, int speed = 0)
    {
        _make = make;
        _model = model;
        _year = year;
        _speed = speed;
    }
    public int SpeedUp()
    {
        // Increments speed and returns the value.
        // Might as well be void without return since example doesn't use it anywhere.
        return _speed++;
    }
    public int SlowDown()
    {
        // If speed is more than 1, decrement and then return the value
        return _speed > 0 ? _speed-- : 0;
    }
    public void Display()
    {
        // String interpolations are much easier to read
        Console.WriteLine($"{_year} {_make} {_model} is going {_speed} MPH.");
    }
}
public class Program
{
    // Main is entry point for your app, it doesn't belong in any type class like Car, 
    // because it's not relevant to it
    private static void Main()
    {
        // Use descriptive names for variables, you already know one car is Ford and the 
        // other is Chevy so name them as such so you know which is which
        // later in the code. 
        var fordCar = new Car("Ford", "Focus", 2010, 20);
        var chevyCar = new Car("Chevy", "Cruze", 2018); // No speed parameter
        // Ommited variables car1Speed, car2Speed. You shouldn't declare variables 
        // that serve no purpose and aren't even in use.
        for (int i = 0; i < 60; i++)
        {
            if (i % 2 == 0)
            {
                // Returns int, but variable assignment ins't necessary to anything here
                chevyCar.SpeedUp(); 
            }
            if (i % 3 == 0)
            {
                fordCar.SpeedUp();
            }
            if (i % 5 == 0)
            {
                fordCar.SlowDown();
                chevyCar.SlowDown();
            }
        }
        fordCar.Display();
        chevyCar.Display();
    }
}

听起来您所需要的只是一个构造函数可选 parameter

class Car
{
    public void Car(string make, string model, int year, int speed = 0)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = speed;
    }
    ...

附加资源

  • 可选参数(C#编程指南(

  • 构造函数(C#编程指南(

相关内容

最新更新