[英]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.