创建包含速度,模型和制造的汽车类

时间:2014-04-22 21:28:59

标签: c#

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

  

'Car_Class_BBrantley.Form1.myCar()'必须声明一个正文,因为它没有标记为abstract,extern或partial   错误发生在第14行。

好吧,我的任务是创建一个显示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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

更改

private Car myCar();

private Car myCar;

private void GetCarData(Car car)

private void GetCarData()

答案 1 :(得分:0)

更改

    myCar = new Car;  
to    myCar = new Car();