gpt4 book ai didi

java - 如何解决这个编译错误?

转载 作者:行者123 更新时间:2023-11-29 07:52:50 27 4
gpt4 key购买 nike

当我写“new Triangle();”时,我得到这个错误“没有类型 oppgave1 的封闭实例是可访问的”。我需要一些帮助来解释我哪里做错了(关于三角形类)。我是 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 成为顶级(非嵌套)类。
  • 让它成为一个static嵌套类
  • 在构造 Triangle 的实例时提供封闭类的实例

(参见 Java tutorial for more on nested/inner classes 。)

就我个人而言,我推荐第一个行动方案 - 嵌套类当然有用,但我建议使用顶级类,除非嵌套类有特定的好处。 p>

此外,即使您可以将多个类放在同一个源文件中,如果其中最多一个是公共(public)的(因此与文件同名),我还是建议将每个类放在顶部-level类在自己的文件中,根据类命名。

关于java - 如何解决这个编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com