编写一个BWMCar类,继承Car类,要求: 1)BWMCar类增加一个属性int maxSpeed(最高速度) 2) BWMCar类设一个带4个参数的构造函数,前三个参数给父类Car构造赋值,最后一个参数给自己的属性maxSpeed赋值; 3) BWMCar类重载Run()方法,输出“我是什么车,颜色是什么,产地在哪里,最高车速多。” 4) 编写一个测试MainApp类,主方法为main(),其功能: 创建一个BWMCar对象bc,给属性赋值,并调用它的Run()方法。
时间: 2023-03-09 20:02:59 浏览: 68
class BWMCar extends Car{
int maxSpeed;
//构造函数
public BWMCar(String brand,String color,String origin,int maxSpeed){
super(brand,color,origin);
this.maxSpeed = maxSpeed;
}
//重载Run()方法
public void Run(){
System.out.println("我是"+this.brand+"车,颜色是"+this.color+",产地在"+this.origin+",最高速度为"+this.maxSpeed+"。");
}
}
//MainApp类
class MainApp {
public static void main(String[] args){
BWMCar bc = new BWMCar("宝马","红色","德国",200);
bc.Run();
}
}
相关问题
设计一个交通工具类,包含属性:品牌、颜色、最高时速,包含方法:驾驶、显示基本信息。
好的,这是一个关于设计交通工具类的问题。根据您的要求,我可以设计一个包含品牌、颜色、最高时速属性以及驾驶、显示基本信息方法的交通工具类,以便使用者方便地驾驶、显示基本信息。您可以参考以下代码:
```
class Vehicle {
String brand;
String color;
int maxSpeed;
public Vehicle(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
public void drive() {
System.out.println("I am driving at " + maxSpeed + " km/h!");
}
public void showInfo() {
System.out.println("Brand: " + brand);
System.out.println("Color: " + color);
System.out.println("Max Speed: " + maxSpeed + " km/h");
}
}
```
这样,您就可以使用这个交通工具类来创建交通工具,并且调用驾驶和显示基本信息的方法了。希望这个答案能够帮助到您!
编写个Java类与对象的简单案例
### 回答1:
答:以下是一个简单的Java类和对象的案例:
public class Person {
// 定义Person类的属性
private String name;
private int age;
// 定义构造器,用于初始化Person类的实例
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 定义getName()方法,用于获取Person类的name属性
public String getName() {
return name;
}
// 定义setName()方法,用于设置Person类的name属性
public void setName(String name) {
this.name = name;
}
// 定义getAge()方法,用于获取Person类的age属性
public int getAge() {
return age;
}
// 定义setAge()方法,用于设置Person类的age属性
public void setAge(int age) {
this.age = age;
}
}
### 回答2:
一个简单的Java类与对象的案例是实现一个学生类(Student)。首先,通过定义成员变量(学号、姓名、性别、年龄)来表示学生的相关信息。然后,在类中定义构造方法,用于创建学生对象时对其属性进行初始化。接下来,为每个属性定义相应的getter和setter方法,以便对学生对象进行读取和修改。此外,可以在类中定义一些其他的方法来表示学生的行为,比如study()方法用来表示学生学习,sleep()方法用来表示学生休息等。
下面是一个简单的示例代码:
```java
public class Student {
private String studentId;
private String name;
private String gender;
private int age;
public Student(String studentId, String name, String gender, int age) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void study() {
System.out.println(name + "正在学习...");
}
public void sleep() {
System.out.println(name + "正在休息...");
}
}
```
在使用该类时,可以创建一个学生对象,并设置其属性:
```java
public class Main {
public static void main(String[] args) {
Student student = new Student("001", "张三", "男", 18);
System.out.println("学号:" + student.getStudentId());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getGender());
System.out.println("年龄:" + student.getAge());
student.study();
student.sleep();
}
}
```
以上代码便是一个简单的Java类与对象的案例,通过定义一个学生类,创建学生对象并设置其属性,最后调用对象的方法来表示学生的行为。
### 回答3:
以下是一个简单的Java类与对象的案例:
```java
public class Car {
// 类的属性
private String brand;
private String color;
private int maxSpeed;
// 构造方法
public Car(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
}
// 类的方法
public void start() {
System.out.println("汽车启动了!");
}
public void accelerate() {
System.out.println("汽车加速中...");
}
public void stop() {
System.out.println("汽车停止了!");
}
// 获取品牌
public String getBrand() {
return brand;
}
// 设置品牌
public void setBrand(String brand) {
this.brand = brand;
}
// 获取颜色
public String getColor() {
return color;
}
// 设置颜色
public void setColor(String color) {
this.color = color;
}
// 获取最大速度
public int getMaxSpeed() {
return maxSpeed;
}
// 设置最大速度
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
public class Main {
public static void main(String[] args) {
// 创建一个Car对象
Car car = new Car("BMW", "红色", 250);
// 调用对象的方法
car.start();
car.accelerate();
car.stop();
// 调用对象的属性
System.out.println("汽车品牌:" + car.getBrand());
System.out.println("汽车颜色:" + car.getColor());
System.out.println("汽车最大速度:" + car.getMaxSpeed());
// 修改对象的属性
car.setBrand("Audi");
car.setColor("蓝色");
car.setMaxSpeed(280);
// 再次调用对象的属性
System.out.println("汽车品牌:" + car.getBrand());
System.out.println("汽车颜色:" + car.getColor());
System.out.println("汽车最大速度:" + car.getMaxSpeed());
}
}
```
上述代码中定义了一个`Car`类,包含了品牌、颜色和最大速度等属性,并具有启动、加速和停止等方法。`Main`类中的`main`方法展示了如何创建一个`Car`对象,调用对象的方法和获取/修改对象的属性。这个例子简单地展示了Java类与对象的基本概念与用法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)