【问题标题】:How do I override the variable that was given a value to in another class which has been inherited如何覆盖在另一个已继承的类中赋予值的变量
【发布时间】:2019-07-19 04:18:52
【问题描述】:

我试图让用户输入汽车在超类中的座位数,但制作了一个方法来打印名为“汽车”的子类中的座位数。但是,当我声明存储用户输入的变量时,我收到一条错误消息,指出该变量不可见,这是因为它在另一个类中。

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 + ".");
        }
    }

【问题讨论】:

  • 使用getter而不是直接访问变量
  • 您的代码结构存在一些严重问题。 getNoOfSeats() 应该返回座位数,而不是从用户那里读取值。修复你的代码对于 StackOverflow 来说太宽泛了,你需要从你的教授那里得到一些帮助。

标签: java class inheritance overriding superclass


【解决方案1】:

如果您想保持变量私有,可以使用父类的公共函数 getNoOfSeats()。

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

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

【讨论】:

  • 这将导致编译器错误,因为Vehicle.getNoOfSeats 具有 void 返回类型。它不是一个合适的吸气剂。最好在发布之前测试您的答案代码。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    基于 OOP 概念,即使在子类中,您也无法访问类外的私有变量。

    在这里你已经声明了

     private int noOfSeats;
    

    你尝试在子类之外访问它,它超出了私有变量范围。

    要在子类中访问它,请使 noOfSeats 变量

     public int noOfSeats;
    

     protected int noOfSeats;
    

    在车辆类中。

    如果您想将 noOfSeats 变量设为私有,请在车辆类(父类)上创建公共函数并从 Car 类(子类)调用它。

    你正在尝试访问

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

    Truck 类中的 towingCapacity 变量,并且您没有在 Vehicle 类或 Truck 类中声明它。

    所以先声明再使用。

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2011-03-11
      • 2012-11-15
      相关资源
      最近更新 更多