有速度的汽车类

时间:2014-04-22 20:31:45

标签: c#

编辑:将()添加到Car实例后,我收到一个新错误。 它说:

  

' Car_Class_BBrantley.Form1.myCar()'必须声明一个正文,因为它没有标记为abstract,extern或partial

好吧,我的任务是创建一个显示3个主要功能的应用程序:汽车的年份,品牌和速度。年份和品牌输入文本框,速度从0开始。

每次按下时都会有一个加速按钮,加速按钮和速度加5,制动按钮每次按下时减速5。

我无法一起使用该类和表单来显示结果。我需要在消息框中显示make,year和speed。我一直坐在这里几个小时,我无处可去。我收到了错误

  

当前上下文中不存在速度

  

汽车在当前背景下不存在

在我的按钮下。我不确定如何解决这个问题。

非常感谢任何和所有帮助。如果这是一团糟,我很抱歉。我以前从未上过课。

以下是表格:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Car_Class_BBrantley
{
    public partial class Form1 : Form
    {
        private Car myCar;

        public Form1()
        {
            myCar = new Car;

            InitializeComponent();
        }

        private void GetCarData(Car car)
        {
            try {
            myCar.Make = txtMake.Text;

            myCar.Year = int.Parse(txtModel.Text);

            myCar.Speed = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
            } 
        }

        private void btnAcc_Click(object sender, EventArgs e)
        {
            GetCarData();
            myCar.AccSpeed(5);
            MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is     traveling " + myCar.Speed + " mph. ");
        }

        private void btnBrake_Click(object sender, EventArgs e)
        {
            GetCarData();
            myCar.DecSpeed(5);
            MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is     traveling " + myCar.Speed + " mph. ");
        }
    }
}

如果你想看课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Car_Class_BBrantley
{
    class Car
    {
    private int year;
    private string make;
    private int speed;

    public Car()
    {
        this.year = 1994;
        this.make = "Ford";
        this.speed = 0;
    }

    public Car(string make, int year, int speed)
    {
        this.year = year;
        this.make = make;
        this.speed = speed;
    }

    public string Make
    {
        get { return make; }
        set { make = value; }
    }

    public int Year
    {
        get { return Year; }
        set { Year = value; }
    }

    public int Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    public void AccSpeed(int speedIncrement)
    {
        //Add check for speed limit ranges
        Speed += speedIncrement;
    }

    public void DecSpeed(int speedDecrement)
    {
        //Add check for speed limit ranges
        Speed -= speedDecrement;
    }
  }
}

3 个答案:

答案 0 :(得分:1)

您错误地实例化了Car课程:

myCar = new Car;   // wrong

myCar = new Car();

您并未将Car实例传递给GetCarData()方法,即使它期望它:

GetCarData();      // wrong

GetCarData(myCar);

(您实际上并没有使用 Car 的实例,而是要传递给方法...要么从方法中删除参数,或在您的代码中引用它。)

答案 1 :(得分:0)

1 - 您没有在Car构造函数中使用括号:

myCar = new Car;

应该是

myCar = new Car();

2 - 您声明GetCarData需要一个参数,但它没有使用它,当你调用它时,你没有传递它。

更改

private void GetCarData(Car car)

private void GetCarData()

答案 2 :(得分:0)

在类名中使用括号来创建类的实例:

myCar = new Car();

将您的方法定义更改为:

private void GetCarData()

该方法需要一个Car类型的参数,但是你在调用它而不提供任何参数。据我所知,你没有在方法中使用该参数,所以你可以删除它。否则你必须将Car实例传递给您的方法。

相关问题