Can't access object in the array java

旧街凉风 提交于 2019-12-13 01:26:14

问题


else if (control.equals("Car") == true)
{
     owner = (scanner.nextLine());                     
     address = (scanner.nextLine());
     phone = (scanner.nextLine());
     email =(scanner.nextLine());
     convertible= (scanner.nextBoolean());
     color = (scanner.nextLine());

     vehicleLot[i] = new car(owner, address, phone, email, convertible, color);
     System.out.println(vehicleLot[i].getOwner());
     System.out.println(vehicleLot[i].getAddress());
     //System.out.println(vehicleLot[i].getColor());
}

The above code is in my main method. The line that is commented out throws out the error "cannot find symbol in the class vehicle". I am reading from a file and placing the information into the correct objects data fields. The array is an array of vehicles. From what I understand, an element of the array of vehicles can be a vehicle or any subclass of vehicle. Getters and setters methods are available for each respective class with subclass using the getters and setters for the parents class. Car is a subclass of vehicle. Why is it not trying to access cars methods first before trying vehicle when I just created the car object? Does the problem lie in my car constructor? car is static because it is a nested class and throws an error if you don't keep it static. Below is the summary of the car class.

static class Car extends vehicle
{
  private boolean convertible;
  private String color;
  public Car()
  {

  }
  public Car(String ownersName, String address, String phone, String email, boolean convertible, String color)
  {
      super.setOwner(ownersName) ;
      super.setAddress(address);
      super.setPhone(phone);
      super.setEmail(email);
      this.convertible = convertible;
      this.color = color;
      System.out.println(this.convertible);
  }//Car class ends

The System.out.println prints out the correct value for that string, so I'm interested as to why the object wants to try and only use the class vehicle for its methods instead of class car and class vehicle. Here is vehicle if that helps.

public static class Vehicle
{
    private String ownersName;
    private String address; 
    private String phone;
    private String email;

    public Vehicle()
    {
    }
    public Vehicle(String ownersName, String address, String phone, String email)
    {
        this.ownersName = ownersName;
        this.address = address;
        this.phone = phone;
        this.email = email;
     }
}//Vehicle class ends

回答1:


If I understood the question, you are asking why the following line doesn't compile :

System.out.println(vehicleLot[i].getColor());

vehicleLot is defined as an array of Vehicle. It can contain instances of any sub-classes of Vehicle. Therefore, vehicleLot[i] doesn't necessarily have a getColor() method, since that's a method of Car.

In order to access the methods of Car you have to cast the Vehicle reference to Car :

System.out.println(((Car)vehicleLot[i]).getColor());

This, of course, would only work if vehicleLot[i] refers to a Car instance. If not, a ClassCastException will be thrown.




回答2:


"Car is a subclass of vehicle. Why is it not trying to access cars methods first before trying vehicle when I just created the car object?"

This really only applies to polymorphic methods, those that are defined for a superclass, and then (possibly) overridden in a subclass. If you say

vehicle v;

the compiler knows only that v is a vehicle; therefore, it can only access methods defined for the vehicle class. If v is actually a car, and one of those methods is overridden for car, then it will call the method in car. But if you use a method that's defined only for car (not for vehicle), the compiler won't allow it, because it only knows that v is a vehicle, and it doesn't know that it will have a car method such as getColor. This is true even if you have just assigned v to a new car. The compiler looks only at the declaration vehicle v; it doesn't try to go backward through your program to figure out what you've assigned to it.

You can solve the problem with a cast, as in Eran's answer, but in this particular case you don't need to. Change

 vehicleLot[i] = new car(owner, address, phone, email, convertible, color);
 System.out.println(vehicleLot[i].getOwner());
 System.out.println(vehicleLot[i].getAddress());
 System.out.println(vehicleLot[i].getColor());

to

 car newCar = new car(owner, address, phone, email, convertible, color);
 vehicleLot[i] = newCar;
 System.out.println(newCar.getOwner());
 System.out.println(newCar.getAddress());
 System.out.println(newCar.getColor());

The compiler may think vehicleLot[i] can be any vehicle, but it knows that newCar is a car because you declared it that way.

(By the way, the Java convention is for class names, like car and vehicle, to start with upper-case letters.)




回答3:


It seems like you missed an intricate detail regarding polymorphism, you can put a Car Object into a Vehicle array(Due to Polymorphism) and on calling methods on the arrayElements the correct overloaded method is called (if you have overloaded it ie is ) , But since there is no getColor(); in your Vehicle class it doesn't work , suppose you had written a getColor() in Vehicle Class and overloaded it in the Car class ,

Then had you said

Vehicle v=new Car(); 
v.getColor();

The Car's getColor() will be called , However ,Please Note that the Compiler first checks if the vehicle class has the method , only then it checks if the actual object type and calls the overloaded method belonging to the car class , But in your case the first test fails and an error is thrown Try Reading about Polymorphism and Run-time Binding in Java



来源:https://stackoverflow.com/questions/26533269/cant-access-object-in-the-array-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!