我是一名 AP java 学生,在从事一个项目时,我想知道拥有一个全局变量的参数是否是一种好习惯。如果你想知道为什么我想做得很好,那么我就不必这样做了:
public class Circle {
private DrawingTool pen;
private SketchPad paper;
private double myX;
private double myY;
private double myWidth;
private double myHeight;
public Circle(double x, double y, double width, double height){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
x = myX; //I don't want to have to assign this every time
y = myY; //like here
width = myWidth; // and here
height = myHeight; // and here
}
}
是否允许仅执行以下操作:
public Circle(double myX, double myY, double myWidth, double myHeight){
paper = new SketchPad(500,500);
pen = new DrawingTool(paper);
}
}
每次我将参数传递给参数时,它们都会自动分配给全局变量?