简体   繁体   中英

Using dot method in Object type array in Java

Using dot method in Object type array in Java:

class patient {
  int id;
  public String name;
  String pNum;

  patient() {
    id=0;
    name=null;
    pNum=null;
  }

  patient(int i, String n, String p) {
    id =i;
    name=n;
    pNum=p;
  }

  String getName() {
    return name;
  }

  public String toString() {
    String str="ID           : "+id+"\n"+"Name         : "+name+"\n"+"Phone Number :"+pNum+"\n";
    return str;
  }
}

When I tried to use it with conventional queue class by q static object I couldn't do so[i].getName(); function or so[i].name; and it gives cannot find symbol error.

like this code below:

 static void ShowAllPatient() {
    Object [] so=q.toArray();

    String str=so[0].getName();
    for(int i=0; i<ob.length; i++) {
      System.out.println(so[i].name);
    }
  }

The variable so is of type Object[] , the Object doesn't have getName method. The Collection q should define a type of the element, then use

patient[] so=q.toArray(new patient[q.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM