Java - 访问具有动态名称变体的 class 方法
Java - Access a class method with a dynamic name variant
我遇到这个问题有一段时间了,直到现在才设法避免它。基本上,我有一个 class 存储高尔夫球场每个洞的信息,而 getter 则 returns 此信息。在另一个 class 中,当用户从洞 1 移动到洞 2(当前存储为整数)时,我希望访问洞 2 的 getter 方法,但不知道如何完成。
代码:
课程信息class:
//Hole information
private static String[] hole1 = {"name", "Distance", "Par"};
private static String[] hole2 = {"name", "Distance", "Par"};
public static String[] getHole1(){
return hole1;
}
public static String[] getHole2(){
return hole2;
}
主要Class:
String[] holeInfo;
int currentHole = 1;
//How I call hole1 info just now
//I just access the elements of the array as needed then
holeCoOrds = course.getHole1();
//User starts next hole (this will be the case for all 18 holes)
currentHole++
//Call next hole information getter method here
//Something along the lines of;
holeCoOrds = course.getHole(currentHole)();
任何帮助将不胜感激,我已经绞尽脑汁一段时间了,但没有成功。
提前致谢。
您没有最佳方法:
//Hole information
private static String[] hole1 = {"name", "Distance", "Par"};
private static String[] hole2 = {"name", "Distance", "Par"};
可以这样存储在一个List<String[]>
中:
private static List<String[]> holes = new ArrayList<String[]>();
private static String[] hole1 = {"name", "Distance", "Par"};
holes.add(hole1);
private static String[] hole2 = {"name", "Distance", "Par"};
holes.add(hole2);
然后,用一种方法得到孔值:
public static String[] getHole(int holeNumber){
return holes.get(holeNumber);
}
如果你想更好地控制孔,使用Map
添加一个key来标识每个孔value:
private static Map<Integer, String[]> holes = new HashMap<Integer, String[]>();
private static String[] hole1 = {"name", "Distance", "Par"};
holes.put(1, hole1);
private static String[] hole2 = {"name", "Distance", "Par"};
holes.put(2, hole2);
然后,用一种方法得到孔值:
public static String[] getHole(int holeNumber){
return holes.get(holeNumber);
}
你可以通过反思来做到这一点。但是,更好的方法是重新设计 class,这样就没有必要了。将所有 hole
数组放入另一个数组或列表中,然后创建一个 getHole(int)
方法,其中 returns 一个项目。
所以一个简单的(没有完整性检查)版本应该是这样的:
private static String[] holes = {{"name", "Distance", "Par"},
{"name", "Distance", "Par"}};
public static String[] getHole(int hole){
return holes[hole];
}
这使得将来添加额外的漏洞变得容易,并让您的代码更加灵活,这样您就不需要在编译时知道您想要哪个漏洞。
为什么不为 Hole
实体创建一个 class,如下所示:
public class Hole {
private int holeNo;
private String name;
private String dist;
private String par;
//getters and setters for above properties
}
这样当你从 main class 调用你的代码时,很容易遍历一组孔(2 个或更多)。
您甚至可以将 Hole 对象的所有实例保存在一个 Map 中,以便于检索。
我遇到这个问题有一段时间了,直到现在才设法避免它。基本上,我有一个 class 存储高尔夫球场每个洞的信息,而 getter 则 returns 此信息。在另一个 class 中,当用户从洞 1 移动到洞 2(当前存储为整数)时,我希望访问洞 2 的 getter 方法,但不知道如何完成。
代码:
课程信息class:
//Hole information
private static String[] hole1 = {"name", "Distance", "Par"};
private static String[] hole2 = {"name", "Distance", "Par"};
public static String[] getHole1(){
return hole1;
}
public static String[] getHole2(){
return hole2;
}
主要Class:
String[] holeInfo;
int currentHole = 1;
//How I call hole1 info just now
//I just access the elements of the array as needed then
holeCoOrds = course.getHole1();
//User starts next hole (this will be the case for all 18 holes)
currentHole++
//Call next hole information getter method here
//Something along the lines of;
holeCoOrds = course.getHole(currentHole)();
任何帮助将不胜感激,我已经绞尽脑汁一段时间了,但没有成功。
提前致谢。
您没有最佳方法:
//Hole information
private static String[] hole1 = {"name", "Distance", "Par"};
private static String[] hole2 = {"name", "Distance", "Par"};
可以这样存储在一个List<String[]>
中:
private static List<String[]> holes = new ArrayList<String[]>();
private static String[] hole1 = {"name", "Distance", "Par"};
holes.add(hole1);
private static String[] hole2 = {"name", "Distance", "Par"};
holes.add(hole2);
然后,用一种方法得到孔值:
public static String[] getHole(int holeNumber){
return holes.get(holeNumber);
}
如果你想更好地控制孔,使用Map
添加一个key来标识每个孔value:
private static Map<Integer, String[]> holes = new HashMap<Integer, String[]>();
private static String[] hole1 = {"name", "Distance", "Par"};
holes.put(1, hole1);
private static String[] hole2 = {"name", "Distance", "Par"};
holes.put(2, hole2);
然后,用一种方法得到孔值:
public static String[] getHole(int holeNumber){
return holes.get(holeNumber);
}
你可以通过反思来做到这一点。但是,更好的方法是重新设计 class,这样就没有必要了。将所有 hole
数组放入另一个数组或列表中,然后创建一个 getHole(int)
方法,其中 returns 一个项目。
所以一个简单的(没有完整性检查)版本应该是这样的:
private static String[] holes = {{"name", "Distance", "Par"},
{"name", "Distance", "Par"}};
public static String[] getHole(int hole){
return holes[hole];
}
这使得将来添加额外的漏洞变得容易,并让您的代码更加灵活,这样您就不需要在编译时知道您想要哪个漏洞。
为什么不为 Hole
实体创建一个 class,如下所示:
public class Hole {
private int holeNo;
private String name;
private String dist;
private String par;
//getters and setters for above properties
}
这样当你从 main class 调用你的代码时,很容易遍历一组孔(2 个或更多)。
您甚至可以将 Hole 对象的所有实例保存在一个 Map 中,以便于检索。