Java 用许多方法分解类以提高可读性
我的网格超级类已经变得很大,有很多公共方法,我正试图找出如何将其分解,使其更易于管理。方法分为几类,以下方法用于获取索引信息:Java 用许多方法分解类以提高可读性,java,processing,Java,Processing,我的网格超级类已经变得很大,有很多公共方法,我正试图找出如何将其分解,使其更易于管理。方法分为几类,以下方法用于获取索引信息: class grid{ int tot, cols, rows; float gw, gh, w, h,gx,gy,gcx,gcy; ArrayList<cell> cells = new ArrayList<cell>(); grid(float width, float height, int cols, int rows){
class grid{
int tot, cols, rows;
float gw, gh, w, h,gx,gy,gcx,gcy;
ArrayList<cell> cells = new ArrayList<cell>();
grid(float width, float height, int cols, int rows){
this.gx = 0;
this.gy = 0;
this.gw = width;
this.gh = height;
this.cols = cols;
this.rows = rows;
w = gw/float(cols);
h = gh/float(rows);
}
// how to move these methods somewhere else?
int rc(int row, int col){ // get index at row# col#
int val = 0;
for(int i = 0; i < cells.size(); i++){
if(cells.get(i).row == row && cells.get(i).col == col){
val = i;
}
}
return val;
}
int col(int inst){
if(altFlow == 1){
return floor(inst/rows);
} else {
return inst%cols;
}
}
int[] listRow(int indexIn){
int stIndex = cols*indexIn;
int[] arrayOut = new int[cols];
for(int i = 0; i < cols; i++) arrayOut[i] = i+stIndex;
return arrayOut;
}
}
您可以从定义一些方法的抽象类开始,然后通过添加更多方法来增强它(从中派生) 本教程介绍了抽象类: 但我不会这样做,除非您有从同一抽象基派生的不同类。我认为拥有大型源代码文件没有问题。每个IDE都提供了许多快速导航的特性,不管类有100行还是3000行
class grid{
gridInfo gi;
...
//still need one of these for each method?
int col(int inst){
return gi.col(inst);
}
}
class gridInfo(){
grid parent;
...
int col(int inst){
if(altFlow == 1){
return floor(inst/parent.rows);
} else {
return inst%parent.cols;
}
}
}