如何覆盖在另一个已继承的 class 中赋值的变量

How do I override the variable that was given a value to in another class which has been inherited

我正在尝试让用户输入汽车在超级 class 中的座位数,但是创建了一个方法来打印子 class 中的座位数,称为 "Car".但是,当我声明存储用户输入的变量时,我收到一条错误消息,指出该变量不可见,这是因为它在另一个 class 中。

import java.util.Scanner;

class Vehicle {

    Scanner s = new Scanner(System.in);
    private String color;
    private int noOfCylinders;
    private int noOfSeats;

    public Vehicle() {
        color = "Black";
        noOfCylinders = 0;
        noOfSeats = 1;
    }

    public Vehicle(String color, int noOfCylinders, int noOfSeats) {
        this.color = color;
        this.noOfCylinders = noOfCylinders;
        this.noOfSeats = noOfSeats;
    }

    public void getColor() {
        System.out.print("Enter color of vehicle: ");
        color = s.nextLine();
    }

    public String setColor() {
        return color;
    }

    public void getNoOfCylinders() {
        System.out.print("Enter number of cylinders: ");
        noOfCylinders = s.nextInt();
    }

    public int setNoOfCylinders() {
        return noOfCylinders;
    }

    public void getNoOfSeats() {
        System.out.print("Enter numer of seats: ");
        int noOfSeats = s.nextInt();
    }

    public String toString() {
        String information;
        information = "is " + color + " and it has " + noOfCylinders + " cylinders.";
        return information;
    }
}

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        CarObject.getColor();
        CarObject.setColor();
        CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        CarObject.toString();
        CarObject.getNumOfSeats();

        TruckObject.getColor();
        TruckObject.setColor();
        TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        TruckObject.toString();

        System.out.print(("\nThe car ")+CarObject.toString());
        System.out.print(("\nThe truck ")+TruckObject.toString());      
    }
}


class Car extends Vehicle{

    public void getNumOfSeats(){
        System.out.print("\nThe car has " + noOfSeats + " seats.");
    }
}


    class Truck extends Vehicle {
        public void printTowingCapacity() {
            System.out.print("\nThe car has " + towingCapacity + ".");
        }
    }

noOfSeats 是 class 的私有成员,任何其他 class 都无法访问它。如果你想让它可以从继承的 class 访问,请将它的访问说明符设置为受保护的。

基于 OOP 概念,您无法访问 class 之外的私有变量,甚至在 child class.

中也不行

你在这里声明了

 private int noOfSeats;

并且您尝试在 child 中的 class 之外访问它,这超出了私有变量范围。

要在 child 中访问它 class 使 noOfSeats 变量

 public int noOfSeats;

 protected int noOfSeats;

在车辆中 class。

如果您想将 noOfSeats 变量设置为私有变量,请在 vehicitle class (parent class) 上创建 public 函数并从 Car [=44] 调用它=] (child class).

您正在尝试访问

 System.out.print("\nThe car has " + towingCapacity + ".")

Truck class 中的 towingCapacity 变量并且您尚未在 Vehicle class 或 Truck class.

中声明它

所以先声明再使用。

有关 java 访问修饰符的更多详细信息,请阅读本文

https://www.softwaretestingmaterial.com/access-modifiers-in-java/

如果要将变量保密,可以使用父 class 的 public 函数 getNoOfSeats()。

class Car extends Vehicle{
   public void getNumOfSeats(){
      System.out.print("\nThe car has " + super.getNoOfSeats() + " seats.");
   }
}

或者只是将变量 noOfSeats 更改为 protected 或 package-protected,