创建一个具有加速和制动的车类 [英] Creating a Car Class with Acceleration and Braking
问题描述
编辑:我试过这个不同的方式,并更新了我的新代码。我现在得到一些错误:
1.'Car_Class_BBrantley.Car.Car()'必须声明一个正文,因为它没有标记为抽象,外部或部分
2.没有超载方法'GetCarData'接受0个参数
3.方法'GetCarData'的重载不需要0个参数
最后两个错误属于GetCarData();
I have tried this a different way and updated my new code. I am now getting a few errors:
1. 'Car_Class_BBrantley.Car.Car()' must declare a body because it is not marked abstract, extern, or partial
2. No overload for method 'GetCarData' takes 0 arguments
3. No overload for method 'GetCarData' takes 0 arguments
These last two errors fall under the GetCarData(); lines which are under the two button sections.
好的,所以我的任务是创建一个应用程序,显示3个主要特征:年,制造和汽车的速度。年份和年份是用文本框输入的,速度从0开始。
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
有一个加速按钮,每次按下时加速5次,一个制动按钮,每次按下时速度降低5个。
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
我无法使用类和表单一起显示结果。我需要在messagebox中显示make,year和speed。我一直坐在这里几个小时,我得到无处。我得到的错误速度不存在当前上下文和汽车不存在当前上下文在我的按钮下。我不确定我应该如何解决这个问题。
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere. I am getting the errors " speed does not exist in current context" and "car does not exist in current context" under my buttons. I am unsure of how I should go about fixing this.
任何和所有的帮助是非常感谢。对不起,如果这是一团糟。
Any and all help is much appreciated. I'm sorry if this is a mess. I have never worked with classes before.
以下是形式:
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. ");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
If you would like to see the class:
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;
}
}
}
推荐答案
此代码:
public int AccSpeed
{
get { return Speed + 5; }
}
..说获取属性值的副本速度
,并在返回结果前将该副本增加5。
.. says "get a copy of the value in property Speed
and increase that copy with five before returning the result"
使用五增加属性 Speed
的值,然后返回结果。这是通过使用:
What you want is: "increase the value of property Speed
with five and then return the result". That is done by using:
public int AccSpeed
{
get
{
Speed = Speed + 5; //shorter code: Speed += 5;
return Speed;
}
}
但是,属性不应该改变get方法。
However, Properties should never change state of classes in the get methods. Anyone using your code would get really confused if it did.
而是使用一个方法让它变得清晰:
Instead use a method to make it crystal clear:
public int Accelerate()
{
Speed += 5;
return Speed;
}
这篇关于创建一个具有加速和制动的车类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!