import java.util.Scanner;
public class ShapeTrouble {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double radius = scanner.nextDouble();
double length = scanner.nextDouble();
double width = scanner.nextDouble();
Circle circle = new Circle(radius);
Rectangle rectangle = new Rectangle(length, width);
System.out.println(circle.getAcreage());
System.out.print(rectangle.getAcreage());
}
}
/********** Begin *********/
abstract class Shape {
abstract double getAcreage();//定義抽象方法
}
class Circle extends Shape {
private final double PI=3.1415926;
private double radius;
public Circle(double radius){
this.radius=radius;
}
public double getAcreage(){
return PI*this.radius*this.radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
private Rectangle(double length,double width){
this.length=length;
this.width=width;
}
private double getAcreage(){
return this.length*this.width;
}
}
錯誤:
0/3src/step1/ShapeTrouble.java:13: error: Rectangle(double,double) has private access in Rectangle
Rectangle rectangle = new Rectangle(length, width);
^
src/step1/ShapeTrouble.java:16: error: getAcreage() has private access in Rectangle
System.out.print(rectangle.getAcreage());
^
src/step1/ShapeTrouble.java:43: error: getAcreage() in Rectangle cannot override getAcreage() in Shape
private double getAcreage(){
^
attempting to assign weaker access privileges; was
3 errors
uj5u.com熱心網友回復:
你的錯誤出現的地方不是抽象方法的重寫,是修飾符的訪問范圍問題,private私有修飾符,只能在自己的類中進行訪問使用。所以你需要將Rectangle類中的建構式以及重寫父類的抽象方法的訪問修飾詞改為public或是默認的(什么都不寫)轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/88865.html
標籤:Eclipse
上一篇:求大佬提意見,幫忙規劃一下
