这个问题已经在这里被问到和回答了,但我找不到一个对我有意义的例子.我正在测试一些代码,并试图在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";
        }

    }
}

推荐答案

'... what can I do to fix the "AbstractShape.this cannot be referenced from a static context" error I am getting regarding my creation of cube1. ...'

This is because you are working within a method marked for static context.
The inner-class Cube requires an instance of its enclosing-class, AbstractShape; it's inferring this and cannot find the instance.

您可以做两件事,这取决于您的数据需求.

如果您计划从enclosing-class之外实例化100,则将其标记为static.

static abstract class Shapes
static class Solids extends Shapes implements Comparable
static class Cube extends Solids

否则,如果Cube需要特定于AbstractShape实例的数据,只需先创建一个实例.

AbstractShape abstractShape = new AbstractShape();
Cube cube1 = abstractShape.new Cube(4,4,4);
cube1.calcArea();
cube1.print();
Solids [] MySolids = new Solids[2];

Java相关问答推荐

CriteriaQuery with max

工件部署期间出错[Tomcat 8.5.45]

springboot start loge change

我无法将附件发送到NetBeans之外

如何使用AWS CLI从S3存储桶中的所有对象中删除用户定义的元数据?

Mapstruct不能正确/完全映射属性

自定义批注的外推属性值

如何在Spring Java中从数据库列中获取JSON中的具体数据

在Java 15应用程序中运行Java脚本和Python代码

如何在JavaFX循环中完美地制作一个AudioClip/MediaPlayer?

解析方法";javax/imageio/metadata/IIOMetadata.getAsTree(Ljava/lang/String;)Lorg/w3c/dom/Node时加载约束冲突

为什么有两种实现来检索数组类的组件类型?

在macOS上读取文件会导致FileNotFound,即使文件存在(并且具有权限)

Dijkstra搜索算法的实现

错误:未找到扩展元素在JBossEAP 7.2中安装FUSE时出错

如何从HttpResponse实例获取Entity对象的内容?

我该如何为我的类编写getter和setter方法?

我们可以在方法中声明接口吗?

使用Java线程进行并行编程

在数组列表中找到对象后,未从数组中删除对象