我的程序出了问题。 Eclipse写道:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static method umfang() from the type GeoPolygon
Cannot make a static reference to the non-static method laenge() from the type GeoPolyline
我做错了什么?我的App.class中有2个错误。你能改进我的代码并解释一下吗? 提前谢谢。
import java.util.ArrayList; //import Array List (java.util)
import java.util.List; //import List (java.util)
public class App { //new public class App
public static void main(String[] args) { //main methode
GeoPoint p1 = new GeoPoint(10.681,-48.857); // new Points (x,y)
GeoPoint p2 = new GeoPoint(96.980,20.724);
GeoPoint p3 = new GeoPoint(66.647,-66.558);
GeoPoint p4 = new GeoPoint(-2.674,-58.571);
GeoPoint p5 = new GeoPoint(40.11,-12.342);
GeoPoint p6 = new GeoPoint(27.782,46.809);
GeoPoint p7 = new GeoPoint(54.759,-46.709);
GeoPoint p8 = new GeoPoint(-33.89,-90.787);
GeoPoint p9 = new GeoPoint(15.84,-67.553);
GeoPoint p10 = new GeoPoint(19.481,51.331);
List<GeoPoint> list1 = new ArrayList<GeoPoint>(); // Create ArrayList and add Points
list1.add(p1);
list1.add(p2);
list1.add(p3);
list1.add(p4);
list1.add(p5);
list1.add(p6);
list1.add(p7);
list1.add(p8);
list1.add(p9);
list1.add(p10);
BoundingBox bbox = new BoundingBox(list1); // get the list from BoundingBox
double c = GeoPolygon.umfang(); //new variable c = height
double d = GeoPolyline.laenge(); //new variable d = width
System.out.println("Das Array beinhaltet die Punkte: " + list1 ); // This prints: "Das Array beinhaltet die Punkte" and the list
System.out.println("Der Gesamtlaenge des Polygons betraegt (" + d +")." ); // This prints the polygons circumference
System.out.println("Der Umang des Polygons betraegt: (" + c +")." ); // This prints the polygons area
}
}
多边形
import java.util.List;
public class GeoPolygon implements Geometry {
private List<GeoPoint> Polylinelist;
private String name;
private double umfang;
public double umfang() {
umfang = 0;
for (int i = 0; i < Polylinelist.size() -1 ; i++) {
GeoPoint p1 = Polylinelist.get(i);
GeoPoint p2 = Polylinelist.get(i + 1);
umfang += GeoUtil.distance(p1, p2);
}
umfang += GeoUtil.distance(Polylinelist.get(0), Polylinelist.get(Polylinelist.size() -1 ));
return umfang;
}
public GeoPolygon(List<GeoPoint> punktliste, String name) {
this.Polylinelist = punktliste;
this.name = name;
}
public BoundingBox getBoundingBox() {
BoundingBox bbox = new BoundingBox(Polylinelist);
return bbox;
}
public String getName() {
{
return name;
}
}
public void addGeoPoint(GeoPoint geoPoint) {
Polylinelist.add(geoPoint);
}
}
GeoPolyline
import java.util.List;
public class GeoPolyline implements Geometry {
private List<GeoPoint> Polylinelist;
private String name;
private double laenge;
public double laenge() {
laenge = 0;
for (int i = 0; i < Polylinelist.size() - 1; i++) {
GeoPoint p1 = Polylinelist.get(i);
GeoPoint p2 = Polylinelist.get(i + 1);
laenge += GeoUtil.distance(p1, p2);
}
return laenge;
}
public BoundingBox getBoundingBox() {
BoundingBox bbox = new BoundingBox(Polylinelist);
return bbox;
}
public GeoPolyline(List<GeoPoint> punktliste, String name) { //Konstruktor
this.Polylinelist = punktliste;
this.name = name;
}
public String getName() {
{
return name;
}
}
public List<GeoPoint> getPolylinelist() {
{
return Polylinelist;
}
}
public void addGeoPoint(GeoPoint geoPoint) {
Polylinelist.add(geoPoint);
}
}
答案 0 :(得分:3)
Show Text3
这些不是静态方法。如果您希望它们是静态的,请将它们声明为。
或者,您可以在调用它之前创建该类的实例:
例如:
double c = GeoPolygon.umfang();
double d = GeoPolyline.laenge();
答案 1 :(得分:1)
在App类中,您在public static void main
内有以下行:
double c = GeoPolygon.umfang();
注意它里面的方法是静态的。因此,您无法调用umfang()
,因为它不是静态的。 laenge()
也是如此。
如果要从中调用非静态方法,则必须创建GeoPolygon
和GeoPolyline
的新实例。
您可以直接在类中访问静态项,但不能直接在类中访问非静态项。要访问非静态项(例如方法或字段),必须创建该类的新实例。
答案 2 :(得分:0)
您需要将关键字static添加到GeoPolygon上的方法
GeoPolygon x = new GeoPolygon();
double c = x.umfang();