创建具有数组成员的类的实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建具有数组成员的类的实例相关的知识,希望对你有一定的参考价值。
我试图在没有运气的情况下创建这个对象的实例,我知道它因为数组但我怎样才能使它与数组一起工作?
public class City {
private String Country_Name;
private String [] Cities ;
private String [] Destinations;}
public City(String Country_Name, String [] Cities, String [] Destinations) {
this.Country_Name = Country_Name;
this.Cities = new String [2];
this.Destinations = new String [2];
}
public class ASSIGNMENT {
public static void main(String[] args) throws ParseException {
LinkedList<FLIGHT> ob1 = new LinkedList<FLIGHT>();
LinkedList<FLIGHT_BOOKING> ob = new LinkedList<FLIGHT_BOOKING>();
LinkedList<City> ob3 = new LinkedList<City>();
Scanner sc = new Scanner(System.in);
Scanner ss = new Scanner(System.in);
FLIGHT_BOOKING obj = new FLIGHT_BOOKING();
FLIGHT obj1 = new FLIGHT();
City c1 = new City("Malaysia","Kuala","Johor","melaka"},{"Cyberjaya","Putrajaya","Sunway"});
}
}
答案
你错了大括号,你在构造函数之前关闭了类定义。它应该如下:
public class City {
private String Country_Name;
private String [] Cities ;
private String [] Destinations;
public City(String Country_Name, String[] Cities, String[] Destinations) {
this.Country_Name = Country_Name;
this.Cities = new String [2];
this.Destinations = new String [2];
}
}
此外,你错过了打开大括号。按如下方式创建对象:
String[] cities = {"Kuala","Johor"}; // Giving only 2 cities as in constructor you've given as new String[2]
String[] destinations = {"Cyberjaya","Putrajaya"}; // Same as above
City c1 = new City("Malaysia", cities, destinations);
如果您想要更多城市和目的地,请将构造函数更新为
public City(String Country_Name, String[] Cities, String[] Destinations) {
this.Country_Name = Country_Name;
this.Cities = Cities;
this.Destinations = Destinations;
}
以上是关于创建具有数组成员的类的实例的主要内容,如果未能解决你的问题,请参考以下文章
Activator.CreateInstance - 如何创建具有参数化构造函数的类的实例