gpt4 book ai didi

Java编译器无法识别派生类,即 "symbol not found"错误

转载 作者:行者123 更新时间:2023-12-01 22:39:47 24 4
gpt4 key购买 nike

当我尝试编译此代码时,出现“找不到符号”错误。总的来说,我可以创建 Ball 类型的对象,没有问题,但如果我尝试创建篮球类型的对象,编译器会提示。我对java很陌生,请帮忙吗?

class Ball {
public double radious;
public String color;

Ball( double radious, String color ) {
this.radious = radious;
this.color = color;
}

public double area() {
return (4 * Math.pow(this.radious, 2) * Math.PI );
}

public void display() {
System.out.println("\nRadious: " + radious );
System.out.println("Color: " + color );
}

class Basketball extends Ball {
public int noOfStripes;

Basketball( int n, double r, String c ) {
super( r, c );
noOfStripes = n;
}

Basketball( double r, String c ) {
super( r, c );
noOfStripes = 8; // Default value
}

public void display() {
super.display();
System.out.println("Area: " + area() );
System.out.println("noOfStripes: " + noOfStripes );
}
}
}

class Driver {
public static void main( String[] args ) {
Ball basket1 = new Ball( 29.5, "Orange" );
basket1.display();

Ball basket2 =new Basketball( 29.5, "Black" );
}
}

最佳答案

您不仅创建了 Basketball 作为 Ball 的子类,而且还创建了一个内部类。它不需要是内部类。

Ball 类的大括号之外定义 Basketball

    }
} // End of Ball class

class Basketball extends Ball
{
// ...

关于Java编译器无法识别派生类,即 "symbol not found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26366585/

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