JAVA高手帮忙,这个怎么回事呀?

来源:百度知道 编辑:UC知道 时间:2024/05/28 09:59:39
我看孙鑫老师的视频写下来的代码.怎么运行错误.
class Point
{
public static void change(Point pt)
{
pt.x=pt.x+pt.y;
pt.y=pt.x-pt.y;
pt.x=pt.x-pt.y;
}

public static void main(String[] args)
{

Point pt=new Point();
int pt.x=3;
int pt.y=4;
change(Point);
System.out.println("x="+pt.x+","+"y="+pt.y);

}
}
E:\JAVA>javac Point.java
Point.java:16: 需要 ';'
int pt.x=3;
^
Point.java:17: 需要 ';'
int pt.y=4;
^
2 错误

class Point
{
int x,y;//在这里定义
public static void change(Point pt)
{
pt.x=pt.x+pt.y;
pt.y=pt.x-pt.y;
pt.x=pt.x-pt.y;
}

public static void main(String[] args)
{

Point pt=new Point();
pt.x=3;
pt.y=4;
change(pt);
System.out.println("x="+pt.x+","+"y="+pt.y);

}
}

int pt.x=3;
int pt.y=4;

这个声明量的代码是错的.声名类量应该是: 类型名 变量名

应该
int x=3;
int y=4;

你的类 Point 的定义也有问题,你可以把x,y 作为类的私有成变量进行声明,也就是这样
class Point{
private int x;
private int y;

//默认构造
public Point(){
}
//构造
public Point(x,y){
this.x = x;
this.y = y;
}

public static void change(int x,int y){
this.x = x;
this.y = y;
}

然后需要生成类实例的时候这样:
Point point = new Point(x,y);

需要改变时,
point.change(x,y);

}

public clas