gpt4 book ai didi

java - 如何扩展或实现类?

转载 作者:行者123 更新时间:2023-12-02 06:29:27 28 4
gpt4 key购买 nike

图片显示任务: enter image description here

首先,我很抱歉,因为我表达了自己的想法。

我有这样的任务,我不需要你帮我做。

VehicleSedan 的父类(因为Sedan类是String类型)。

  1. 如何使用通用类扩展或实现 Vehicle 类?
  2. 我忘了问我的老师,但也许你会知道,Owner 类的条纹指针是什么意思,那是什么:has a

附注如果您需要我已经编写的代码,我会向您展示。

这是我的父车辆类:

public class Vehicle {

private int vehicleNumber;
protected int fuelTankSize;
protected int maxSpeed;
protected Owner owner;

//1
public Vehicle(int vehicleNumber){
this.vehicleNumber = vehicleNumber;
}
//2
public Vehicle(int vehicleNumber, int fuelTankSize) {
this.vehicleNumber = vehicleNumber;
this.fuelTankSize = fuelTankSize;
}
//3
public Vehicle(int vehicleNumber, int fuelTankSize, int maxSpeed) {
this.vehicleNumber = vehicleNumber;
this.fuelTankSize = fuelTankSize;
this.maxSpeed = maxSpeed;
}
//4
public Vehicle(int vehicleNumber, int fuelTankSize, int maxSpeed, Owner owner) {
this.vehicleNumber = vehicleNumber;
this.fuelTankSize = fuelTankSize;
this.maxSpeed = maxSpeed;
this.owner = owner;
}

//1
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed (int maxSpeed){
this.maxSpeed = maxSpeed;
}
//2
protected int getFuelTankSize(){
return fuelTankSize;
}
protected void setFuelTankSize (int fuelTankSize){
this.fuelTankSize = fuelTankSize;
}
//3
public Owner getOwner(){
return owner;
}
public void setOwner (Owner owner){
this.owner = owner;
}

}

child 轿车:

public class Sedan extends Vehicle {

private String registrationIndex;{
}

public Sedan (int vehicleNumber, int fuelTankSize, int maxSpeed, String registrationIndex, Owner owner) {
super(vehicleNumber, fuelTankSize, maxSpeed, owner);
this.setRegistrationIndex (registrationIndex);
}

public String getRegistrationIndex (){
return registrationIndex;
}
public void setRegistrationIndex (String registrationIndex) {
this.registrationIndex = registrationIndex;
}

}

第二个没有错误的通用子项:

public class Universal extends Vehicle {
private int trunkSize;

public Universal (int vehicleNumber, int fuelTankSize, int maxSpeed, int trunkSize, Owner owner) {
super(vehicleNumber, fuelTankSize, maxSpeed, owner);
this.setTrunkSize (trunkSize);
}

public int getTrunkSize() {
return trunkSize;
}
public void setTrunkSize(int trunkSize) {
this.trunkSize = trunkSize;
}

public void printDescription() {
super.printDescription();
System.out.println("Universalo bagažinės tūris: " + getTrunkSize() + "l.");
}

}

还有一些神秘的(对我来说)所有者类别:

public class Owner  {
public String firstName;
public String lastName;

public Owner (String firstName){
this.firstName = firstName;
}

public Owner (String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}

添加了 VechileTest 进行测试:

public class VehicleTest {

public static void main(String[] args) {


Vehicle vehicleInf = new Vehicle (1, 45, 260);
Universal universalInf = new Universal(2, 50, 220, 70);
Sedan sedanInf = new Sedan (3, 40, 180, "AVA 123");

vehicleInf.printDescription();
universalInf.printDescription();
sedanInf.printDescription();
}
}

最佳答案

首先,我建议您阅读一个很好的 UML 类图教程/解释,例如 this例如这里。

了解基础知识后,将其转换为 Java 代码应该很容易。我会给你 Universal 的代码类(class)和您的开始Vehicle 。其余的事情你必须自己完成。

通用类:

public class Universal extends Vehicle {

private int trunkSize;

public int getTrunkSize() {
return this.trunkSize;
}

public void setTrunkSize(int trunkSize) {
this.trunkSize = trunkSize;
}
}

正如您所看到的,类框中的第一个 block 引用了变量。 -+指示可见性( - 为私有(private), + 为公共(public))。

下一个 block 是关于方法的,指定可见性、返回类型、方法名称和参数(类型和名称)。

Universal之间的箭头和Vehicle表示继承关系(见代码 Universal extends Vehicle )。

总而言之,该图是您的类的构建计划;至少对于静态部分来说,这意味着它们可以拥有的关系和状态。

车辆类(class)开始:

public class Vehicle {
private int vehicleNumber;
// the rest here ...
}

编辑:好吧,现在我看到你的代码,你似乎有一些误解:

  1. Sedan 类型不是来自 String 类型,而是来自 Sedan 类型(它扩展了 Vehicle)。只是Sedan类型中新增的成员变量是String类型,没有关系。

  2. 对于您的第一个问题:Vehicle class 是 Sedan 的基(父)类。你不需要用它做任何事情,继承是从 child 向 parent 表达的,而不是相反。 Vehicle通常应声明为抽象(因为您无法创建泛型 Vehicle 的实例),但这不在图中。

  3. 关于第二个问题: has a关系就是这样。它表示一个类有另一个类作为其成员(这已在类图中冗余地表示),因此无需执行此操作。

此外,您的代码还有一些问题:

  1. 我没有看到在 Vehicle 类中声明任何构造函数,这 4 个可以。
  2. 您的Sedan有一对多余的{}声明您的 registrationIndex 后变量。
  3. 自从您的 Vehicle没有默认构造函数,您必须从 Sedan 调用此构造函数类(或从 Vehicle 中删除构造函数。
  4. 您的Universal类调用 Vehicle带有 trunkSize 的构造函数,而 Vehicle构造函数需要 vehicleNumber在那里。

关于java - 如何扩展或实现类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211491/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com