简体   繁体   English

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

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

I am trying to get user input for the number of seats a car has in the super class but make a method that prints the number of seats in the subclass called "Car". 我试图让用户输入超级类中汽车的座位数,但是要创建一种方法来打印子类“汽车”中的座位数。 However, when I state the variable that stores the user input I get an error stating the variable is not visible and that is because it is in another 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 + ".");
        }
    }

You can use the public function of the parent class, getNoOfSeats(), if you want to keep the variables private. 如果要将变量设为私有,可以使用父类的公共函数getNoOfSeats()。

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

Or just change the variable noOfSeats to protected or package-protected, 或者只是将变量noOfSeats更改为受保护或受包保护的变量,

noOfSeats is a private member of the class which cannot be accessed by any other class. noOfSeats是该类的私有成员,任何其他类都无法访问。 If you want to make it accessible from inherited class, make it access specifier to protected. 如果要使它可以从继承的类访问,请使其对说明符的访问权限为protected。

Based on OOP concept you can't access private variabale outside the class not even in child class. 基于OOP概念,您甚至不能在子类中访问课外的私有变量。

Here you have declare 在这里您声明

 private int noOfSeats;

And you try to access it outside the class in child which is out of private variable scope. 并且您尝试在子类的类外部访问它,这超出了私有变量范围。

To access it in child class make noOfSeats variable 要在子类中访问它,请使noOfSeats变量

 public int noOfSeats;

or 要么

 protected int noOfSeats;

in Vehicle class. 在车辆类中。

If you want to put noOfSeats variable as private the create on public function on vehicitle class (parent class) and call it from Car class (child class). 如果要将noOfSeats变量设置为私有变量,请在车辆类(父类)上基于public函数创建并从Car类(子类)调用它。

And you are try to access 您正在尝试访问

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

towingCapacity variable in Truck class and you have't declare it in either Vehicle class or Truck class. Truck类中的towingCapacity变量,而您没有在Vehicle类或Truck类中声明它。

So first declare it then use it. 因此,首先声明它,然后使用它。

For more details about java access modifier read this article 有关Java访问修饰符的更多详细信息,请阅读本文。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中,如何在继承的类中覆盖变量的类类型? - In Java, how do I override the class type of a variable in an inherited class? 我如何获取已从jdialog类创建的对象以在另一个jdialog类中使用 - how do i get a object that has been created from a jdialog class to be use in another jdialog class 如何从另一个Java类更改变量的值? - How do i change value of variable from another java class? 如何确定已输入的最小值? - How would I determine the smallest value which has been entered? 如何判断其他课程何时更新? - How can I tell when another class has been updated? 如何检查变量是否已初始化 - How do I check if a variable has been initialized 在继承的类上重写类变量/方法 - Override class variable/method on inherited classes 使用来自另一个 class 的数组时,如何从继承的 class 获取特定属性? - how do i get a specific attribute from an inherited class when using an array from another class? 如何在另一个类中更改变量的值,并且仍然能够在另一个类中更改它的值? - How do I change the value of a variable from another class and still be able to change it within the other class? 我有一个模型类,使用它可以存储到领域。 如何使用“ contains()”查看对象是否已经存储 - I have a model class, using which I save to realm. How do I use `contains()` to see if the object has already been stored
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM