我想根据模型model的x状态量对model实例进行处理,想问一下有经验的前辈们哪一种方式开销少一点?
模型函数如
public model{
private int x ;
......
public model(){
this.x = 0 ;
......
}
public void setX( int t ){
this.x = t ;
}
public int getX(){
return x ;
}
......
}
我这里想构造一个根据model实例的x状态量和一个与model实例无关的deal状态量进行综合判断的函数method,method是程序控制台里面的方法。
【1】方法函数的参数是直接使用model如
model m = new Model() ;
public static boolean method( Model modelparameter , int deal ){
private int temp ;
temp = modelparameter.getX() + deal ;
......
}
这个方法中的参数modelparameter是用指针指向Model实例m,还是重新创建了一个m作为参数专用?我做的Model有点复杂,用它做参数会不会计算开销太大?
【2】还是将里面的参数倒出来如
......{
......
model m = new Model() ;
private int parameter ;
parameter = m.getX() ;
} 再使用
public static boolean method( int temp , int deal ){
temp += deal ;
......
}
模型函数如
public model{
private int x ;
......
public model(){
this.x = 0 ;
......
}
public void setX( int t ){
this.x = t ;
}
public int getX(){
return x ;
}
......
}
我这里想构造一个根据model实例的x状态量和一个与model实例无关的deal状态量进行综合判断的函数method,method是程序控制台里面的方法。
【1】方法函数的参数是直接使用model如
model m = new Model() ;
public static boolean method( Model modelparameter , int deal ){
private int temp ;
temp = modelparameter.getX() + deal ;
......
}
这个方法中的参数modelparameter是用指针指向Model实例m,还是重新创建了一个m作为参数专用?我做的Model有点复杂,用它做参数会不会计算开销太大?
【2】还是将里面的参数倒出来如
......{
......
model m = new Model() ;
private int parameter ;
parameter = m.getX() ;
} 再使用
public static boolean method( int temp , int deal ){
temp += deal ;
......
}
JAVA中的参数传递,除了基本类型为都是按引用传递。基本类型是按值传递。
所以第一种传递的是引用,注意:这里m不是对像实例,它本身就是一个引用。