理解实现Java扩展类

huangapple go评论78阅读模式
英文:

understanding implementing Java Extended Classes

问题

我目前正在学习Java我被分配了一个理解面向对象编程OOP的任务
我理解类的理论但我在实现方面有一个问题

以以下代码为例

    class Vehicle {
      private String engine;
      private int wheels;
      private int seats;
      private int fuelTank;
      private String lights;
    
      public Vehicle() {
        this.engine = "petrol";
        this.wheels = 4;
        this.seats = 4;
        this.fuelTank = 35;
        this.lights = "LED";
      }
    
      public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {
        this.engine = engine;
        this.wheels = wheels;
        this.seats = seats;
        this.fuelTank = fuelTank;
        this.lights = lights;
      }
    
      public String getEngine() {
        return engine;
      }
    
      public int getWheels() {
        return wheels;
      }
    
      public int getSeats() {
        return seats;
      }
    
      public int getFuelTank() {
        return fuelTank;
      }
    
      public String getLights() {
        return lights;
      }
    }
    
    class Car extends Vehicle {
      private String steering;
    
      public Car() {
        super();
        this.steering = "Power Steering";
      }
    
      public Car(String steering, String engine, int wheels, int seats, int fuelTank, String lights) {
        super(engine, wheels, seats, fuelTank, lights);
        this.steering = steering;
      }
    
      public String getSteering() {
        return steering;
      }
    }
    
    class Demo {
      public static void main(String[] args) {
        Car car = new Car("Power steering", "deisel", 4, 4, 40, "LED");
    
        System.out.println("Steering: " + car.getSteering());
        System.out.println("Engine type: " + car.getEngine());
        System.out.println("Number of seats: " + car.getSeats());
        System.out.println("Fuel tank capacity: " + car.getFuelTank());
        System.out.println("Head lamp type: " + car.getLights());
        System.out.println("Number of wheels: " + car.getWheels());
      }
    }

我理解在这里您可以通过使用默认构造函数或参数化构造函数之一来创建`Vehicle`对象我也可以用从`Vehicle`继承的`Car`对象做同样的事情我理解通过在默认构造函数中使用`super()`,它将使用`Vehicle`中的默认构造函数以及`Car`对象的默认`steering`变量参数化构造函数使用`super(args)`也是同样的道理

我在理解如何在每个扩展的车辆类中硬编码`wheels`方面遇到了问题

为了更详细地解释我不想在创建`Car`对象时在构造函数中包含`wheels`。我希望它默认为4另外如果我要创建从`Vehicle`继承的`Bike`对象我希望`wheels`变量默认为2
英文:

I'm currently learning Java and I've been given the task of understanding OOP.
I understand the theory of classes, but I have a question on implementation.

Take the following code as example:

class Vehicle {
private String engine;
private int wheels;
private int seats;
private int fuelTank;
private String lights;
public Vehicle() {
this.engine = "petrol";
this.wheels = 4;
this.seats = 4;
this.fuelTank = 35;
this.lights = "LED";
}
public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {
this.engine = engine;
this.wheels = wheels;
this.seats = seats;
this.fuelTank = fuelTank;
this.lights = lights;
}
public String getEngine() {
return engine;
}
public int getWheels() {
return wheels;
}
public int getSeats() {
return seats;
}
public int getFueTank() {
return fuelTank;
}
public String getLights() {
return lights;
}
}
class Car extends Vehicle {
private String steering;
private String musicSystem;
private String airConditioner;
private String fridge;
private String entertainmentSystem;
public Car() {
super();
this.steering = "Power Steering";
}
public Car(String steering, String engine, int wheels, int seats, int fueTank, String lights) {
super(engine, wheels, seats, fueTank, lights);
this.steering = steering;
}
public String getSteering() {
return steering;
}
}
class Demo {
public static void main(String[] args) {
Car car = new Car("Power steering", "deisel", 4, 4, 40, "LED");
System.out.println("Steering: " + car.getSteering());
System.out.println("Engine type: " + car.getEngine());
System.out.println("Number of seats: " + car.getSeats());
System.out.println("Fuel tank capacity: " + car.getFueTank());
System.out.println("Head lamp type: " + car.getLights());
System.out.println("Number of wheels: " + car.getWheels());
}
}

I understand here that you can create a Vehicle object by either using the default constructor or the parameterized constructor. I can also do the same with a Car object that extends from Vehicle. I understand that by using super() in the default constructor it will use the default constructor in Vehicle as well as the Car objects steering variable default. The same with the parameterize constructor using the super(args).

I'm having an issue understanding how I can hardcode wheels in each extended class of vehicle.

To explain more, I don't want to have to include wheels in the constructor when creating a Car object. I want it to default to 4. Also, if I were to create a Bike object extended from Vehicle, I want the wheels variable to default to 2.

答案1

得分: 1

如果您真的想要使用您的超类属性请将它们的访问修饰符类型更改为protected否则您将无法在子类中直接使用它们

然后您可以使用IIB实例初始化块为每个不同的子类定义wheels变量的值

## 示例解决方案

### Vehicle.java

```java
public class Vehicle {

    // 使用protected访问修饰符的属性
    protected String engine;
    protected int wheels;
    protected int seats;
    protected int fuelTank;
    protected String lights;

    public Vehicle() { // 无参构造函数

    }

    
    public Vehicle(String engine, int seats, int fuelTank, String lights) {

         this.engine = engine;
         this.seats = seats;
         this.fuelTank = fuelTank;
         this.lights = lights;

    }

    public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {

         this.engine = engine;
         this.wheels = wheels;
         this.seats = seats;
         this.fuelTank = fuelTank;
         this.lights = lights;
    }

}

Car.java

public class Car extends Vehicle {

     private String steering;
     private String musicSystem;
     private String airConditioner;
     private String fridge;
     private String entertainmentSystem;

    {//IIB块初始化带有默认值的wheels

        wheels = 4; // 定义wheels变量


    }

    public Car(String steering, String engine, int seats, int fuelTank, String lights) 
    { // 重载的构造函数
         super(engine, seats, fuelTank, lights);
         this.steering = steering;

    }

}

<details>
<summary>英文:</summary>
If you really want to get use of your super class attributes please change their access modifier type to be protected, otherwise you wont be able to use them directly in your child classes.
Then you can use a IIB (Instance Initialization Block) to define your wheels variable value for each different child class.
## Sample solution:
### Vehicle.java

public class Vehicle{

//attributes with protected access modifier
protected String engine;
protected int wheels;
protected int seats;
protected int fuelTank;
protected String lights;
public Vehicle(){//No-args constructor
}
public Vehicle(String engine, int seats, int fuelTank, String lights) {
this.engine = engine;
this.seats = seats;
this.fuelTank = fuelTank;
this.lights = lights;
}
public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {
this.engine = engine;
this.wheels = wheels;
this.seats = seats;
this.fuelTank = fuelTank;
this.lights = lights;
}

}

### Car.java

public class Car extends Vehicle{

 private String steering;
private String musicSystem;
private String airConditioner;
private String fridge;
private String entertainmentSystem;
{//IIB block to initialize wheels with default value
wheels = 4;//Defining the wheels variable 
}
public Car(String steering,String engine,int seats,int fuelTank,String lights) 
{//Overloaded constructor
super(engine,seats,fuelTank,lights);
this.steering = steering;
}

}


</details>
# 答案2
**得分**: 1
你可以按照以下方式进行操作。
private final String steering; // final = 常量,不会改变。
public Car(String steering, String engine, int seats, int fueTank, String lights) {
super(engine, 4 /*轮子*/, seats, fueTank, lights);
this.steering = steering;
}
public Car(String steering, String engine, int fueTank, String lights) {
this(steering, engine, 4 /*座位*/, fueTank, lights);
}
正如你在这里看到的,多个构造函数和许多参数可能会变得繁琐。
有时候,为特定的字段使用设置器会更好。
<details>
<summary>英文:</summary>
You can do it as as follows.
private final String steering; // final = constant, will not change.
public Car(String steering, String engine, int seats, int fueTank, String lights) {
super(engine, 4 /*wheels*/, seats, fueTank, lights);
this.steering = steering;
}
public Car(String steering, String engine, int fueTank, String lights) {
this(steering, engine, 4 /*seats*/, fueTank, lights);
}
As you see here, several constructors and many parameters can become cumbersome.
Sometimes it is better to have setters for the special fields.
</details>

huangapple
  • 本文由 发表于 2020年10月21日 18:46:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64461916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定