1.我有这个班级
class Student {
protected String name;
protected int courseNumber;
protected TreeMap<String, Integer> marks;
protected double successRate;
protected Pair<String, Integer>[] conferences; //name, place
void setName(String name) { this.name = name; }
//... setters
String getName() { return name; }
//... getters
//...constructors
int getHashCode() {
int res = 0;
//...
return res;
}
boolean less (Student b) {
boolean ls = false;
//...
return ls;
}
//... functions to compare objects
String lastConferenceName() {
return conferences[conferences.length - 1].getKey();
}
//... some last data
double marksMedium(String[] s) {
int res = 0;
for (int i = 0; i < s.length; i++)
res += marks.get(s[i]);
return (double) res / s.length;
}
//... some medium functions
//... some group of functions
//... some other groups
}
我想做那样的事情
class Student{
protected String name;
protected int courseNumber;
protected TreeMap<String, Integer> marks;
protected double successRate;
protected Pair<String, Integer>[] conferences; //name, place
void setName(String name) { this.name = name; }
//... setters
String getName() { return name; }
//... getters
//...constructors
}
class StudentComparator{
int getHashCode() {
int res = 0;
//...
return res;
}
boolean less (Student b) {
boolean ls = false;
//...
return ls;
}
//... functions to compare objects
}
class LastStudentInfoGetter{
String lastConferenceName() {
return conferences[conferences.length - 1].getKey();
}
//... some last data
}
class StudentMediumGetter{
double marksMedium(String[] s) {
int res = 0;
for (int i = 0; i < s.length; i++)
res += marks.get(s[i]);
return (double) res / s.length;
}
//... some medium functions
}
class SomeStudentFunctions{
//... some group of functions
}
///... SomeOtherClasses
如果这些类不使用setter和getter会更好
UPD
例如,我有这个课程
class Handler{
void handle(){
Student s = new Student();
///////........
System.out.println(s.lastConferenceName());
System.out.println(s.getHashCode());
}
}
我希望handle()(或类似的东西)在转换后工作
请注意,lastConferenceName()和getHashCode()位于不同的组中
答案 0 :(得分:0)
从每个子类扩展Student类,如下所示。
public class Student {
private String name;
private int courseNumber;
protected TreeMap<String, Integer> marks;
private double successRate;
// protected Pair<String, Integer>[] conferences; // name, place
public void setName(String name) {
this.name = name;
}
// ... setters
public String getName() {
return name;
}
// ... getters
// ...constructors
}
class StudentComparator extends Student{
public int getHashCode() {
int res = 0;
// ...
return res;
}
public boolean less(Student b) {
boolean ls = false;
// ...
return ls;
}
// ... functions to compare objects
}
class StudentMediumGetter extends Student {
double marksMedium(String[] s) {
int res = 0;
for (int i = 0; i < s.length; i++)
res += marks.get(s[i]);
return (double) res / s.length;
}
// ... some medium functions
}
class SomeStudentFunctions extends Student {
// ... some group of functions
}