gpt4 book ai didi

Java找不到方法

转载 作者:行者123 更新时间:2023-12-01 10:56:41 28 4
gpt4 key购买 nike

当我尝试在另一个类中使用我成功编译的代码时,它显示错误:在我的 max 和compareTo 方法上找不到符号。

我真的可以使用这个帮助,我对编码基本上是新手,这是我在这里发表的第一篇文章,所以如果我公然违反规则,请原谅我。

public class assignment13_5 {
public static void main(String[] args){
TriangleFromSimpleGeometricObject x = new TriangleFromSimpleGeometricObject(4, 4, 4, "blue", true);
TriangleFromSimpleGeometricObject y = new TriangleFromSimpleGeometricObject(4, 4, 4, "white", false);
int i = x.compareTo(y);
//this code is from my text book
System.out.print(max(i));
}
}

/**I got the base code from http://www.cs.armstrong.edu/liang/intro9e/html/GeometricObject.html?
then modified it for the comparable method*/

public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

/** Return color */
public String getColor() {
return color;
}

/** Set a new color */
public void setColor(String color) {
this.color = color;
}

/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}

/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}

/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();


public static String max(int x){
if (x == 1)
return "The first is bigger.";
else if (x == -1)
return "The second is bigger.";
else
return "They are equal.";
}
public abstract class ComparableGeometricObject extends GeometricObject
implements Comparable<ComparableGeometricObject> {

//@Override // Implement the compareTo method defined in Comparable
public abstract int compareTo(GeometricObject o) {
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}

@Override // Implement the toString method in GeometricObject
public String toString() {
return super.toString() + " Area: " + getArea();
}
}
}

public class TriangleFromSimpleGeometricObject extends SimpleGeometricObject {

private double s1 = 1;
private double s2 = 1;
private double s3 = 1;
private double side;

public TriangleFromSimpleGeometricObject(){
}





public TriangleFromSimpleGeometricObject(double s1, double s2, double s3, String color, boolean filled) throws IllegalTriangleException{

this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
setColor(color);
setFilled(filled);

if (s1 + s2 <= s3){
if (s1 < s2){
if (s1 < s3)
side = s1;
}
else if (s2 < s1){
if (s2 < s3)
side = s2;
}
else
side = s3;
throw new IllegalTriangleException("N/A");
}
}

public double getArea(){
double s = (s1 + s2 + s3) / 2;

double area = Math.sqrt(s * (s - s1) * ( s - s2) * ( s - s3) );

return area;
}

public double getPerimeter(){
double perimeter = s1 + s2 + s3;;

return perimeter;
}

public void setSide(int side, int newSide) {
if (side == 1)
s1 = newSide;

if (side == 2)
s2 = newSide;

if (side == 3)
s3 = newSide;
}

public void getSide(int side){
if (side == 1)
System.out.println("The first side is: " + s1);

if (side == 2)
System.out.println("The first side is: " + s2);

if (side == 3)
System.out.println("The first side is: " + s3);
}

public String toString(){
return "Triangle: side1 = " + s1 + " side2 = " + s2 + " side3 = " + s3 + " color = " + getColor() + " is filled = " + isFilled();
}
}

public class RectangleFromSimpleGeometricObject
extends SimpleGeometricObject {

private double width;
private double height;

public RectangleFromSimpleGeometricObject() {
}

public RectangleFromSimpleGeometricObject(
double width, double height) {
this.width = width;
this.height = height;
}

public RectangleFromSimpleGeometricObject(
double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

/** Return width */
public double getWidth() {
return width;
}

/** Set a new width */
public void setWidth(double width) {
this.width = width;
}

/** Return height */
public double getHeight() {
return height;
}

/** Set a new height */
public void setHeight(double height) {
this.height = height;
}

/** Return area */
public double getArea() {
return width * height;
}

/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}

最佳答案

对compareTo()的引用位于TriangleFromSimpleGeometricObject类型的对象上;在该类中需要有一个声明,位于大括号内,例如

public int compareTo(TriangleFromSimpleGeometricObject y) {
// with code in here
}

方法 max() 显然应该位于您发布的类中,因此其中应该有声明

private static ? max(int i) {  // could also be public, or even protected
// with code in here
}

(更正为静态)它需要是静态的,因为它是从静态方法调用的,而不引用实例。我们不知道它的返回类型。

这些事情显然不是这样的,因为编译器说它找不到它们。造成这种情况的原因有多种;要知道它们是什么,我们必须查看此类和 TriangleFromSimpleGeometricObject 的所有代码。

--- 已编辑,现在我们有了更多信息

方法max(int)是GeometricObject上的静态方法;为了调用它,您要么必须在方法调用前面加上类名,要么位于该类中。所以改变你的路线

System.out.println(max(i));

System.out.println(GeometricObject.max(i));

这应该可以修复那里的编译错误。这能解决所有问题吗?

顺便说一句,max 对于该方法来说是一个特别糟糕的名字(我这么说是在不知道是你还是其他人写的情况下)。它不计算最大值,与最大值没有任何关系,因此它的名称并不反射(reflect)它的作用。但是修复这个问题并不能修复编译错误,即使假设您能够更改它,而且这不是您所要求的。

关于Java找不到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590525/

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