在eclipse中的java中没有定义构造函数,尽管它是已定义的
请帮忙。。。。 我是Java新手。我正试图在Eclipse(Luna)中编写一个包含类“point”的代码,该类被放入公共类“PeterAndSnowBlower”中。 我还尝试在类“point”中公开变量x,y,它给出了相同的错误。 我还使用了this.x和this.y,而不是构造函数中的x和y 这是我的密码:在eclipse中的java中没有定义构造函数,尽管它是已定义的,java,eclipse,constructor,Java,Eclipse,Constructor,请帮忙。。。。 我是Java新手。我正试图在Eclipse(Luna)中编写一个包含类“point”的代码,该类被放入公共类“PeterAndSnowBlower”中。 我还尝试在类“point”中公开变量x,y,它给出了相同的错误。 我还使用了this.x和this.y,而不是构造函数中的x和y 这是我的密码: import java.util.*; public class PeterAndSnowBlower { class point{ int x;
import java.util.*;
public class PeterAndSnowBlower {
class point{
int x;
int y;
public point() {
x = y = 0;
}
public point(int a, int b){
x = a;
y = b;
}
public point(point p) {
x = p.x;
y = p.y;
}
public double distance(point P){
int dx = x - P.x;
int dy = y - P.y;
return Math.sqrt(dx*dx + dy*dy);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n, x, y;
point P = new point(0, 0);
n = in.nextInt();
P.x = in.nextInt();
P.y = in.nextInt();
Vector<point>points = new Vector<point>();
for(int i = 0; i < n; i++){
x = in.nextInt();
y = in.nextInt();
points.add(new point(x, y));
}
double r1, r2;
r1 = r2 = P.distance(points.get(0));
for(point point:points){
r1 = Math.max(r1, P.distance(point));
}
}
}
除非内部类是定义为静态的,否则不能从外部类的静态方法实例化它。
非静态
类需要此
引用外部类
在这里,将此类声明为
static
是有意义的。显然,它并没有引用外部类。您正在静态地访问内部类(这意味着您不会从外部类的实例进行访问)
您需要的是一个静态的内部类,这在这里很有意义,因为内部类不引用外部类
将声明更改为
static class point {
不要在类中创建类。特别是如果您是新手,请从没有IDE的情况下开始学习基础知识。使您的点类静态如果您想要静态访问,请使用静态类点
?谢谢@kvr000,它正在工作。
static class point {