gpt4 book ai didi

Cannot create object in main(无法在Main中创建对象)

转载 作者:bug小助手 更新时间:2023-10-24 18:37:39 26 4
gpt4 key购买 nike



This question has been asked and answered here, but I can't find an example that makes sense to me. I am testing some code and trying to create an object in main from a class I made called Cube that inherits from a class called Solids that inherits from an abstract class called Shapes (I have to do it this way). My question is 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. Sorry if this is remedial, I am learning at the moment

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

}
}

更多回答

This is because all of the class definitions are nested inside the definition for AbstractShape. This makes them “inner classes”, which is probably a concept you haven’t learned yet, and not what you intended. The solution is to move those other class definitions after the closing brace (}) for the definition of AbstractShape.

这是因为所有类定义都嵌套在AbstractShape的定义中。这使得它们成为“内部类”,这可能是您还没有学到的概念,也不是您想要的。解决方案是将那些其他类定义移到AbstractShape定义的右大括号(})之后。

Okay that fixed my issue, that makes sense. Thank you so much! Yes I'm not sure I've heard the concept before though I'll probably remember it now.

好的,这解决了我的问题,这是有道理的。非常感谢!是的,我不确定我以前听过这个概念,但我现在可能会记住它。

A better (all round) solution would be to move all your classes to their own files

更好的(全面的)解决方案是将所有类移动到它们自己的文件中

That's what I would normally do and probably why I was confused in the first place, but I'm meant to be printing this out at the end to turn in and his own example is one long mess so that's sort of how I started it

这就是我通常会做的事情,可能也是我一开始感到困惑的原因,但我应该在结束时打印出来交上去,他自己的例子是一大堆乱七八糟的东西,所以我就是这样开始的

@TimMoore To be more specific, they aren't inner classes only because they're nested. They're inner classes because they're nested and non-static (note in Java, "inner class" as a specific meaning, and it means "non-static nested class"). Another solution would be to make them static.

@TimMoore更具体地说,它们不仅仅是内部类,因为它们是嵌套的。它们是内部类,因为它们是嵌套和非静态的(请注意,在Java中,“内部类”是一个特定的含义,它的意思是“非静态嵌套类”)。另一种解决方案是将它们设置为静态的。

优秀答案推荐


'... 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.

这是因为您正在标记为静态上下文的方法中工作。内部类Cube需要它的派生类AbstractShape的实例;它正在推断这一点,但找不到该实例。


There are two things you can do, and it is dependent upon your data requirements.

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


If you plan on instantiating your nested-classes from outside of your enclosing-class, then mark them as static.

如果您计划从封闭类外部实例化嵌套类,则将它们标记为静态。


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

Otherwise, if Cube requires data specific to an instance of AbstractShape, just create an instance first.

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


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

更多回答

Thank you for expanding my knowledge a bit, good answer

谢谢你扩大了我的知识面,回答得好

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com