帮我优化这段代码public class point { point point1 = new point(); private int x; private int y; public int getx(){ return x; } public void setx(int x){ this.x=x; } public int gety(){ return y; } public void sety(int y){ this.y=y; } public void printpoint(){ System.out.println("("+x+"<"+y+")"); } public static void main(String[] args) { point point1= new Point(1, 2); point1.printpoint(); } }
时间: 2023-05-30 21:04:31 浏览: 49
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void printPoint() {
System.out.println("(" + x + "," + y + ")");
}
public static void main(String[] args) {
Point point1 = new Point(1, 2);
point1.printPoint();
}
}
1. 类名应该是首字母大写的Point。
2. 构造方法中的参数名应该与成员变量名不同,避免混淆。
3. 方法名应该是驼峰式命名,即首字母小写,后面的单词首字母大写。
4. printPoint()方法中的字符串拼接应该使用加号,而不是小于号。
5. 在main方法中不需要再次声明point1变量,因为在类中已经声明过了。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)