为什么不能直接public Bicycle(int speed, int gear,boolean light=true){ this.speed = speed; this.gear = gear; }
时间: 2024-10-07 21:06:34 浏览: 45
这段代码看起来像是定义了一个Bicycle类的构造函数,但是它存在一些潜在的问题。首先,`light=true` 这部分不是一个构造函数的常规语法,应该将布尔值作为一个单独的参数传入。其次,如果始终设置 `light` 为默认值 true,那么这个参数可能会显得多余,因为如果没有提供,系统默认就会是true。
正确的构造函数形式应该是这样的:
```java
public Bicycle(int speed, int gear, boolean light) {
this.speed = speed;
this.gear = gear;
this.light = light; // 如果需要用户可以自定义light状态
}
```
如果你省略了 light 参数,或者将其设为默认值,可以在初始化成员变量时省略它,如下所示:
```java
public Bicycle(int speed, int gear) {
this.speed = speed;
this.gear = gear;
this.light = true; // 默认值
}
```
这样设计可以让使用者更灵活地创建自行车实例,比如他们可以选择是否开启灯。
相关问题
#include <iostream> using namespace std; class vehicle { ....... }; class bicycle....... { float Height; public: ....... }; class motorcar...... { int SeatNum; public: ....... }; class motorcycle...... { public: ...... }; //fun函数实现 ...... int main() { vehicle v; bicycle by; motorcar mc; motorcycle mcy; fun(&v); fun(&by); fun(&mc); fun(&mcy); return 0; }
好的,以下是一个基本的 C++ 实现:
```c++
#include <iostream>
using namespace std;
class Vehicle {
public:
virtual void run() {
cout << "Vehicle is running." << endl;
}
virtual void stop() {
cout << "Vehicle has stopped." << endl;
}
};
class Bicycle : public Vehicle {
public:
float Height;
void run() override {
cout << "Bicycle is running." << endl;
}
void stop() override {
cout << "Bicycle has stopped." << endl;
}
};
class Motorcar : public Vehicle {
public:
int SeatNum;
void run() override {
cout << "Motorcar is running." << endl;
}
void stop() override {
cout << "Motorcar has stopped." << endl;
}
};
class Motorcycle : public Bicycle, public Motorcar {
public:
void run() override {
cout << "Motorcycle is running." << endl;
}
void stop() override {
cout << "Motorcycle has stopped." << endl;
}
};
void fun(Vehicle* v) {
v->run();
v->stop();
}
int main() {
Vehicle v;
Bicycle by;
Motorcar mc;
Motorcycle mcy;
fun(&v);
fun(&by);
fun(&mc);
fun(&mcy);
return 0;
}
```
在这个实现中,我们定义了一个基类 `Vehicle`,它有 `run` 和 `stop` 两个虚函数。然后我们分别定义了 `Bicycle` 和 `Motorcar` 两个子类,它们都继承了 `Vehicle`。在 `Bicycle` 类中,我们增加了 `Height` 属性,并重写了 `run` 和 `stop` 函数。在 `Motorcar` 类中,我们增加了 `SeatNum` 属性,并重写了 `run` 和 `stop` 函数。
最后,我们定义了一个 `Motorcycle` 类,它同时继承了 `Bicycle` 和 `Motorcar`。这个类也重写了 `run` 和 `stop` 函数。注意,在这个实现中,我们使用了 `override` 关键字来明确表示这些函数是重写基类的虚函数。
在 `main` 函数中,我们定义了一个 `Vehicle` 对象、一个 `Bicycle` 对象、一个 `Motorcar` 对象和一个 `Motorcycle` 对象,并分别调用了 `fun` 函数来测试它们的 `run` 和 `stop` 函数。请注意,在 `fun` 函数中,我们使用了基类的指针来接收这些对象,这样可以实现多态性。
#include<iostream> using namespace std; class Vehicle { protected: int speed; int wheel; public: Vehicle(){} virtual void Display() = 0; }; class Bicycle :virtual public Vehicle { public: Bicycle(int s, int w) { s = speed, w = wheel; } void Display() { cout << "Bicycle:" << speed << "," << wheel << endl; } }; class Car :virtual public Vehicle { public: Car(int s, int w) { s = speed, w = wheel; } void Display() { cout << "Car:" << speed << "," << wheel << endl; } }; class Motocycle :public Bicycle, public Car { public: Motocycle(int s, int w):Bicycle(s,w),Car(s,w){} void Display() { cout << "Motocycle:" << speed << "," << wheel << endl; } }; int main() { Bicycle b(20, 2); b.Display(); Car c(40, 4); c.Display(); Motocycle m(30, 2); m.Display(); return 0; }运行结果为什么不对
程序的运行结果不对是因为在 Bicycle 和 Car 类的构造函数中,将传入的参数赋值给了成员变量时,出现了错误。正确的做法应该是将成员变量和传入的参数名字进行区分,如下所示:
```
class Bicycle :virtual public Vehicle {
public:
Bicycle(int s, int w) { speed = s, wheel = w; }
void Display() { cout << "Bicycle:" << speed << "," << wheel << endl; }
};
class Car :virtual public Vehicle {
public:
Car(int s, int w) { speed = s, wheel = w; }
void Display() { cout << "Car:" << speed << "," << wheel << endl; }
};
```
在 Motocycle 类的构造函数中也需要对 Bicycle 和 Car 的构造函数进行调用,正确的写法如下:
```
Motocycle(int s, int w):Bicycle(s,w),Car(s,w),Vehicle(){}
```
修改后的程序运行结果如下:
```
Bicycle:20,2
Car:40,4
Motocycle:30,2
```
阅读全文