在数组的java不能访问对象 [英] Can't access object in the array java

查看:148
本文介绍了在数组的java不能访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 否则如果(control.equals(汽车)==真)
{
     雇主=(scanner.nextLine());
     地址=(scanner.nextLine());
     电话=(scanner.nextLine());
     电子邮件=(scanner.nextLine());
     兑换=(scanner.nextBoolean());
     颜色=(scanner.nextLine());     vehicleLot [I] =新车(所有者,地址,电话,电子邮件,敞篷车,颜色);
     的System.out.println(vehicleLot [I] .getOwner());
     的System.out.println(vehicleLot [I] .getAddress());
     //System.out.println(vehicleLot [I] .getColor());
}

以上code是在我的主要方法。被注释掉的行抛出错误无法找到该类车辆的象征。我从文件中读取并把信息转换为正确的对象数据字段。该阵列是车辆的阵列。据我了解,车辆的数组的元素可以是车辆或车辆的任何子类。 getter和setter方法​​可用于使用getter和setter为父母类的子类,每个相应的类。车是车辆的一个子类。为什么不尝试尝试​​车辆,当我刚刚创建的车对象之前先访问汽车的方法呢?请问问题出在我的车的构造?汽车是静态的,因为它是一个嵌套类,如果你不保持它的静态抛出一个错误。
下面是汽车类的总结。

 静态类汽车整车延伸
{
  私人布尔转换;
  私人串色;
  大众汽车()
  {  }
  公共停车场(字符串ownersName,字符串地址,电话字符串,字符串电子邮件,布尔敞篷车,串色)
  {
      super.setOwner(ownersName);
      super.setAddress(地址);
      super.setPhone(电话);
      super.setEmail(电子邮件);
      this.convertible =转换;
      this.color =颜色;
      的System.out.println(this.convertible);
  } // Car类两端

本的System.out.println打印出该字符串正确的值,所以我很感兴趣,为什么对象要尽量只使用类车辆的方法,而不是一流的汽车和汽车类。下面是车辆是否有帮助。

 公共静态类车辆
{
    私人字符串ownersName;
    私人字符串的地址;
    私人字符串电话;
    私人字符串电子邮件;    大众汽车()
    {
    }
    公共车辆(字符串ownersName,字符串地址,电话字符串,字符串电子邮件)
    {
        this.ownersName = ownersName;
        this.address =地址;
        this.phone =电话;
        this.email =电子邮件;
     }
} //车辆类别结束


解决方案

如果我明白的问题,你会问,为什么下面的行不能编译:

 的System.out.println(vehicleLot [I] .getColor());

vehicleLot 定义为的数组。它可以包含的任何子类的实例。因此, vehicleLot [I] 不一定有一个的getColor()方法,因为这是一个方法

为了访问的方法,你必须投车辆参考车:

 的System.out.println(((汽车)vehicleLot [I])的getColor());

这,当然,也只有当 vehicleLot [I] 实例工作。如果没有, ClassCastException异常将被抛出。

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

解决方案

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.

这篇关于在数组的java不能访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆