[英]How to solve this compile error?
當我編寫“ new Triangle();”時,出現此錯誤“無法訪問類型為oppgave1的封閉實例”。 我需要som幫助來解釋我做錯了的地方(關於三角課)。 我是java的新手。謝謝。
public static void main(String[] args) {
Triangle T1 = new Triangle(1, 1, 1, "green", false);
Scanner input = new Scanner(System.in);
System.out.println("Enter three sides of the triangle: ");
double side1 = input.nextInt();
double side2 = input.nextInt();
double side3 = input.nextInt();
System.out.println("Enter a color: ");
String color = input.nextLine();
System.out.println("Enter true or false (to indicate triangle is filled or no): ");
String isFilled = input.nextLine();
}
public class GeometricObject {
//Data fields
private String color = "blue";
private boolean filled;
//The default geometricObject/constructor
public GeometricObject() {
this("No color", false);
}
//The geometricObject with the specified colour and filled value
public GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
//Returning the colour
public String getColor() {
return color;
}
//Setting a new colour
public void setColor(String color) {
this.color = color;
}
//Returning the filled
public boolean isFilled() {
return true;
}
//Setting a new filled
public void setFilled(boolean filled) {
this.filled = filled;
}
public String toString() {
return (color + " - " + filled + " - ");
}
}
public class Triangle extends GeometricObject {
//Data fields
double side1 = 1.0;
double side2 = 1.0;
double side3 = 1.0;
//no-arg constructor
Triangle(){
this(0.0, 0.0, 0.0, "No color", false);
}
//A constructor that creates a triangle with the specified sides
public Triangle(double side1, double side2, double side3, String color, boolean filled) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
setColor(color);
setFilled(filled);
}
//Returning the sides
public double getside1() {
return side1;
}
public double getside2() {
return side1;
}
public double getside3() {
return side1;
}
//setting the new ones
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side3;
}
//getting the rule
/* public void setSide(double side1, double side2, double side3) {
if (((this.side1 + this.side2) > this.side3 ) && ((this.side2 + this.side3) > this.side1)
&& ((this.side1 + this.side3) > this.side2))
System .out.println("The rule (the sum of any two sides is greather"
+ " than the other side) is adhered");
} */
//Returning area
public double getArea() {
double p = ((side1 + side2 + side3) / 2);
double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
//Returning perimeter
public double getPerimeter() {
return (side1 + side2 + side3);
}
public String toString() {
return (super.toString() + "Triangle: side1 = " + side1 + "side2 = " + side2 + " side3 = " + side3 +'\n'+
"Area i: " + getArea() + '\n' + "Perimeter is: " + getPerimeter()) ;
}
}
}
Triangle
當前是一個內部類 -這意味着您只能通過具有封閉類的實例來創建它。 簡單的選擇:
Triangle
設為頂級(非嵌套)類。 Triangle
的實例時提供封閉類的實例 (有關嵌套/內部類的更多信息,請參見Java教程 。)
就個人而言,我建議采取第一個操作步驟-嵌套類當然可以很有用,但是我建議使用頂級類,除非嵌套類有特定的好處 。
另外,即使您最多可以將多個類放在同一個源文件中(即使它們中的一個是公共的(因此與文件名稱相同)),我還是建議將每個頂級類都放在自己的文件中,命名方式如下:上課。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.