- 我有一個擁有許多受保護數據成員和處理這些數據成員的許多不同函數組的類。
- 我想分開這些類的子類,以便不同的方法組將在不同的子類。
1.I有這個類Java中按組分類的單獨方法
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
這將是更好,如果這些類不使用getter和setter方法
Upd
例如,我有這個類
class Handler{
void handle(){
Student s = new Student();
///////........
System.out.println(s.lastConferenceName());
System.out.println(s.getHashCode());
}
}
而且我要手柄()(或類似的東西),改造後的工作
注意lastConferenceName()和GetHashCode()是在不同的組
您可以使用這些方法創建接口,然後讓'Student'實現它們。 – markspace
我希望學生班更小。我不想在學生類中實現所有的方法 – PolyProgrammist
如果你想訪問它們,你必須使它們的訪問範圍受到保護或公開。我將添加一個代碼.. –