优化这段代码public class point { class point1{ 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+")"); } } }
时间: 2023-05-30 11:03:33 浏览: 165
public class Point { // 首字母大写,命名规范化
private int x; // 私有化属性
private int y;
public int getX() { // getter方法
return x;
}
public void setX(int x) { // setter方法
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 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(); } }
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变量,因为在类中已经声明过了。
定义一个构造方法对坐标值进行初始化,定义一个成员方法用于输出坐标值,输出格式public class point { class point1{ 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+")"); } } }:(1,2)。
以下是代码示例:
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,2)。
阅读全文