2
単純なものJavaコンストラクタが同じパッケージ内で動作しない
オブジェクトを作成するためにコンストラクタを使用しようとしていますが、オブジェクトが空に作成されています。コンストラクタは、同じパッケージ内の別のクラスに存在します。
public static void main(String[] args) {
//Initialize all data:
ArrayList<Airport_example> all_airports = new ArrayList<Airport_example>();
Airport_example perth = new Airport_example("01","Perth","PER","Australia","WST");
Airport_example brisbane = new Airport_example("02","Brisbane","BNE","Australia","EST");
//Add airports to ArrayList
all_airports.add(perth);
all_airports.add(brisbane);
//debugging
System.out.println(all_airports);
}
別々のクラスのコンストラクタは次のようになります
public class Airport_example extends HashMap<String,String> {
//list of variables
private String airportID;
private String city;
private String code3;
private String country;
private String timezone;
// constructor to initialize objects
public Airport_example(String airportID, String city, String code3, String country, String timezone) {
// Constructor variable initialization
this.airportID = airportID;
this.city = city;
this.code3 = code3;
this.country = country;
this.timezone = timezone;
}
}
するSystem.out.println文は、空の配列を返します。私はここで簡単なトリックを逃したことがありますか?
[{}, {}]
なぜ 'HashMap.toString()'が 'Airport_example'のプライベートフィールドについて知りたいのですか?さらに、なぜあなたは 'HashMap'を最初に拡張していますか? – Siguza
なぜあなたはハッシュマップを拡張しますか? –
コンストラクタはうまく動作します... –