qq_瞳孔一抹蓝_04076840
2016-12-01 20:49
package math; public class all { public abstract class shape { public abstract void area(); public abstract void perimeter(); } public abstract class circle extends shape { double r = 2.2; public void area(){ System.out.println("circle area is "+3.14*r*r); } public void perimeter(){ System.out.println("circle perimete is "+2*r*3.14); } } public abstract class rectangle extends shape { double a=1.0; double b=1.0; public void area(){ System.out.println("rectangle ares is"+a*b); } public void perimeter(){ System.out.println("recrangle perimeter is "+2*(a+b)); } } public static void main(String[] args) { // TODO Auto-generated method stub shape area1 = new circle();//这里显示错误,Cannot instantiate the type circle area1.perimeter(); area1.area(); shape area2 = new rectangle();//这里显示错误,Cannot instantiate the type rectangle area2.perimeter(); area2.area(); } }
问一下这个情况该怎么解决。找不到解决方法,谢谢
子类不能加abstract修饰符
public abstract class shape {
public abstract void area();
public abstract void perimeter();
}
public abstract class circle extends shape {
double r = 2.2;
public void area(){
System.out.println("circle area is "+3.14*r*r);
}
public void perimeter(){
System.out.println("circle perimete is "+2*r*3.14);
}
}
public abstract class rectangle extends shape {
double a=1.0;
double b=1.0;
public void area(){
System.out.println("rectangle ares is"+a*b);
}
public void perimeter(){
System.out.println("recrangle perimeter is "+2*(a+b));
}
}
public class test{
public static void main(String[] args) {
// TODO Auto-generated method stub
shape area1 = new circle();//这里显示错误,Cannot instantiate the type circle
area1.perimeter();
area1.area();
shape area2 = new rectangle();//这里显示错误,Cannot instantiate the type rectangle
area2.perimeter();
area2.area();
}
}
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题