无法从静态上下文中引用非静态 变量 this的问题,求指导

qq_37436770 2017-11-14 09:22:57
import java.util.Scanner;
import java.lang.Math;
public class rrrr
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Point q=new Point(0,0,0);
Point w=new Point(1,1,1);
Point e=new Point();
Point r=new Point();
q.setPoint(w);
System.out.println("请按x,y,z的顺序依次输入两个点的坐标,已回车结束。");
e.x=sc.nextInt();
e.y=sc.nextInt();
e.z=sc.nextInt();
r.x=sc.nextInt();
r.y=sc.nextInt();
r.z=sc.nextInt();
q.length(e,r);
}
//static class Point
class Point
{
private int x;
private int y;
private int z;
public Point(){}
public Point(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
public Point setPoint(Point s)//修改点位置
{
s.x=x;
s.y=y;
s.z=z;
return s;
}
public Point length(Point a,Point b)
{
int X,Y,Z,len;
X=(int)Math.pow(a.x-b.x,2);
Y=(int)Math.pow(a.y-b.y,2);
Z=(int)Math.pow(a.z-b.z,2);
len=(int)Math.sqrt(X+Y+Z);
System.out.println("两点间的距离为:"+len);
return a;
}
}
}
我的程序是这样的。为什么把Point类前加上static就对了啊?怎么理解import java.util.Scanner;
import java.lang.Math;
public class rrrr
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Point q=new Point(0,0,0);
Point w=new Point(1,1,1);
Point e=new Point();
Point r=new Point();
q.setPoint(w);
System.out.println("请按x,y,z的顺序依次输入两个点的坐标,已回车结束。");
e.x=sc.nextInt();
e.y=sc.nextInt();
e.z=sc.nextInt();
r.x=sc.nextInt();
r.y=sc.nextInt();
r.z=sc.nextInt();
q.length(e,r);
}
//static class Point
class Point
{
private int x;
private int y;
private int z;
public Point(){}
public Point(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
public Point setPoint(Point s)//修改点位置
{
s.x=x;
s.y=y;
s.z=z;
return s;
}
public Point length(Point a,Point b)
{
int X,Y,Z,len;
X=(int)Math.pow(a.x-b.x,2);
Y=(int)Math.pow(a.y-b.y,2);
Z=(int)Math.pow(a.z-b.z,2);
len=(int)Math.sqrt(X+Y+Z);
System.out.println("两点间的距离为:"+len);
return a;
}
}
}
为什么把point类前面加上static就对了啊?无法从静态上下文中引用非静态 变量 this这个问题怎么理解?
新手求指导
...全文
343 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
枫醉秋 2017-11-15
  • 打赏
  • 举报
回复
自己已经都写明白了,好好看看基础吧
lcyzc 2017-11-14
  • 打赏
  • 举报
回复
静态的只能调用静态的属性方法,不能调用非静态的
qq_36585995 2017-11-14
  • 打赏
  • 举报
回复
静态的不能用非静态的,这有啥难理解的?
繁华终归落尽 2017-11-14
  • 打赏
  • 举报
回复
说的这么直白了还不明白

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧