这个问题已经在这里被问到和回答了,但我找不到一个对我有意义的例子.我正在测试一些代码,并试图在Main中从我创建的一个名为Cube的类中创建一个对象,该对象继承自一个名为Solids的类,而这个类继承自一个名为Shape的抽象类(我必须这样做).我的问题是,我可以做些什么来修复我在创建cube1时遇到的"AbstractShape.This Cannot be Reference from a Static Context"错误.抱歉,如果这是补救措施,我现在正在学习
public class AbstractShape {
public static void main(String[] args)throws Exception{
Cube cube1 = new Cube(4,4,4);
cube1.calcArea();
cube1.print();
Solids [] MySolids = new Solids[2];
}
abstract class Shapes{
public abstract double getArea();
public abstract double getVolume();
public abstract String getName();
}
class Solids extends Shapes implements Comparable{
protected int len;
protected int wid;
protected int hig;
protected double area;
protected double volume;
public Solids(int len, int wid, int hig){
}
public int compareTo(Object o){
if (getVolume() >((Shapes)o).getVolume())
return 1;
else
if (getVolume()<((Shapes)o).getVolume())
return -1;
else
return 0;
}
public void setLen(int len){
return;
}
public void setWid(int wid){
return;
}
public void setHig(int hig){
return;
}
public int getLen(){
return len;
}
public int getWid(){
return wid;
}
public int getHig(){
return hig;
}
public String getName(){
return "solid";
}
public double getArea(){
return 0.0;
}
public double getVolume(){
return 0.0;
}
public void print(){
System.out.println("Solid of len"+len+" wid"+wid+" hig"+hig);
}
}
class Cube extends Solids{
protected int len;
protected int wid;
protected int hig;
public Cube(int len, int wid, int hig){
super(len,wid,hig);
this.len=len;
this.wid=wid;
this.hig=hig;
}
public void calcArea(){
area = 6*len*len;
}
public void calcVol(){
volume = len*len*len;
}
public double getVolume(){
return volume;
}
public double getArea(){
return area;
}
public String getName(){
return "Cube";
}
}
}