创建具有加速和制动功能的汽车类
本文关键字:功能 汽车类 加速 创建 | 更新日期: 2023-09-27 17:59:20
EDIT:我尝试了不同的方式,并更新了我的新代码。我现在遇到一些错误:1."Car_Class_BBrantley.Car.Car()"必须声明一个主体,因为它没有标记为抽象、外部或部分
2.方法"GetCarData"的重载不包含0个参数
3.方法"GetCarData"的重载不接受0个参数最后两个错误属于GetCarData();位于两个按钮部分下方的线条。
好吧,所以我的任务是创建一个显示3个主要功能的应用程序:年份、品牌和汽车速度。年份和品牌通过文本框输入,速度从0开始。
有一个加速按钮,每次按下速度都会增加5,还有一个制动按钮,每次按压速度都会减少5。
我在将类和窗体一起使用以显示结果时遇到问题。我需要在信息框中显示品牌、年份和速度。我已经在这里坐了好几个小时了,却一无所获。在我的按钮下,我得到了错误"速度不存在于当前上下文中"answers"汽车不存在于目前上下文中"。我不确定该如何解决这个问题。
我们非常感谢所有的帮助。如果这一团糟,我很抱歉。我以前从未与班级合作过。
这是表格:
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; }
}
表示"获取属性Speed
中值的副本,并在返回结果之前将该副本增加5"
您想要的是:"将属性Speed
的值增加5,然后返回结果"。这是通过使用:来完成的
public int AccSpeed
{
get
{
Speed = Speed + 5; //shorter code: Speed += 5;
return Speed;
}
}
但是,属性不应该更改get方法中类的状态。任何使用你的代码的人都会感到非常困惑。
相反,使用一种方法使其清晰可见:
public int Accelerate()
{
Speed += 5;
return Speed;
}
您可以这样声明您的变量:
public int name { get; set; }
这会自动使它成为一个带有getter和setter的变量。
至于提高速度等方法,你可以制作这样的方法fx:
public void AccSpeed() {
Speed += 5;
}
至于其他的,你应该能够从中弄清楚。
编辑:细化。
正如我认为你对课堂运作方式的困惑,让我详细阐述一下。在你的类名中,我猜你已经包含了汽车的类型,因为这个类应该代表某种类型的汽车。
在编程中你不应该这样想。相反,您应该创建一个Car类,它可以表示任何和所有类型的汽车。
因此,可以添加一个名为type的字符串变量,并将其插入构造函数fx中。从而声明它是哪种特定类型的汽车。然后简单地调用您的类car。
如果以后想将方法添加到特定类型的汽车中,可以为汽车类创建子类,以扩展主汽车类。
Fx。如下所示。然后将赛车应该具有的附加方法添加到该类中。
RacingCar : Car {
// class content
}