Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
抽象类的层次继承和具体方法给出&书信电报;标识符>;“预期”;Java中的错误_Java_Inheritance - Fatal编程技术网

抽象类的层次继承和具体方法给出&书信电报;标识符>;“预期”;Java中的错误

抽象类的层次继承和具体方法给出&书信电报;标识符>;“预期”;Java中的错误,java,inheritance,Java,Inheritance,我正在尝试运行以下简单代码 public abstract class Shape{ abstract double area(); abstract double circumference(); public void show() { System.out.println("Area = "+area()); System.out.println("Circumference = "+circumference());

我正在尝试运行以下简单代码

public abstract class Shape{
    abstract double area();
    abstract double circumference();

    public void show()
    {
        System.out.println("Area = "+area());
        System.out.println("Circumference = "+circumference());
    }
}

public class Circle extends Shape{
    double r;

    public double area()
    {
        return 3.14*r*r;
    }

    double circumference()
    {
        return 2*3.14*r;
    }


    Circle(double radius)
    {
        r=radius;
    }
}


public class Rectangle extends Shape{
    double x,y;

    double area()
    {
        return x*y;
    }

    double circumference()
    {
        return 2*(x+y);
    }

    Rectangle(double length, double width)
    {
        x = length;
        y = width;
    }
}

public class Geometry
{
    Circle r = new Circle(2.22);
    Rectangle s = new Rectangle(2.33, 3.44);
    r.show();
}
但我一直从Java编译器中获取标识符预期错误。我做错了什么。一切都是公开的,似乎没有语法错误。请帮助。

这就是问题所在:

class Geometry
{
    Circle r = new Circle(2.22);
    Rectangle s = new Rectangle(2.33, 3.44);
    r.show();
}
class Test {
    System.out.println("Oops");
}
您的最终语句没有声明变量-它只是一个语句。需要属于初始值设定项块、构造函数或方法的。例如:

public class Geometry {
    public static void showCircle() {
        Circle r = new Circle(2.22);
        Rectangle s = new Rectangle(2.33, 3.44);
        r.show();
    }
}
请注意,这与继承无关-此代码将给出相同的问题:

class Geometry
{
    Circle r = new Circle(2.22);
    Rectangle s = new Rectangle(2.33, 3.44);
    r.show();
}
class Test {
    System.out.println("Oops");
}
您对
r.show()的调用不在代码块中。我怀疑你打算把这是一个主要的方法

public static void main(String... args) {
    Circle r = new Circle(2.22);
    Rectangle s = new Rectangle(2.33, 3.44);
    r.show();
}
添加主要方法:

public class Geometry
{
     public static void main(String[] args) {
         Circle r = new Circle(2.22);
         Rectangle s = new Rectangle(2.33, 3.44);
         r.show();
     }
 }

我应该在岩石下面爬一会儿。从岩石下面出来,抓取一个带有语法高亮显示的IDE。流行的免费IDE包括Eclipse、Netbeans和我个人最喜欢的IntelliJ IDEA。