JAVA出现x可以在Point中访问private是什么意思 高手进

[复制链接]
查看11 | 回复3 | 2010-7-24 13:02:35 | 显示全部楼层 |阅读模式
class Point{
private int x;
private int y;
public Point(int x,int y){

this.x=x;

this.y=y;
}
public void move(int xSet,int ySet){
this.x+=xSet;
this.y+=ySet;
}
}
public class Test1{
public static void main(String[]args){
Point myP=new Point(10,10);
System.out.println("Point:x="+myP.x+"y="+myP.y+"\n");
myP.move(10,10);
System.out.println("Point:x="+myP.x+"y="+myP.y+"\n");
}
}
错误指令:
x ,y可以在point中访问private该怎么解决

回复

使用道具 举报

千问 | 2010-7-24 13:02:35 | 显示全部楼层
因为xy是private变量,你需要加上设置和读取的方法:public int getX(){
return this.x;}public void setX(int s){
this.x = x;}然后从外部访问的时候只能通过调用getX()和setX来读取和写入。y的你自己写一下,当作作业。System.out.println("Point:x=" + myP.getX() + "y=" + myP.getY() + "\n");
回复

使用道具 举报

千问 | 2010-7-24 13:02:35 | 显示全部楼层
拉取方式
回复

使用道具 举报

千问 | 2010-7-24 13:02:35 | 显示全部楼层
LS回答正解结贴吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行