Java 报错求解答 non-static variable this cannot be referenced from a static context
答案:2 悬赏:10 手机版
解决时间 2021-12-03 00:51
- 提问者网友:别再叽里呱啦
- 2021-12-02 15:24
Java 报错求解答 non-static variable this cannot be referenced from a static context
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-12-02 15:47
错误1:大括号位置有误
错误2:zui.getVolume()拼错
下面是改正后的完整代码
abstract class GraphicObject {
public abstract double getArea();// 抽象方法
}
class TiXing extends GraphicObject {
double a, b, h;
TiXing(double a, double b, double h) {
this.a = a;
this.b = b;
this.h = h;
}
public double getArea() {
return (a + b) * h / 2;
}
}
class Circle extends GraphicObject {
double r;
Circle(double r) {
this.r = r;
}
public double getArea() {
return 3.14 * r * r;
}
}
class Zhui {
GraphicObject bottom;
Double height;
Zhui(GraphicObject bottom, double height) {
this.bottom = bottom;
this.height = height;
}
void setBottom(GraphicObject bottom) {
this.bottom = bottom;
}
public double getVolum() {
return (bottom.getArea() * height) / 3;
}
}
public class aa {
public static void main(String args[]) {
Zhui zui;
GraphicObject tuxing;
tuxing = new TiXing(2, 3, 4);
System.out.println("梯形的面积是" + tuxing.getArea());
zui = new Zhui(tuxing, 30);
System.out.println("梯形底的锥的体积是" + zui.getVolum());
tuxing = new Circle(10);
System.out.println("圆形的面积是" + tuxing.getArea());
zui.setBottom(tuxing);
System.out.println("圆锥的体积是" + zui.getVolum());
}
}
错误2:zui.getVolume()拼错
下面是改正后的完整代码
abstract class GraphicObject {
public abstract double getArea();// 抽象方法
}
class TiXing extends GraphicObject {
double a, b, h;
TiXing(double a, double b, double h) {
this.a = a;
this.b = b;
this.h = h;
}
public double getArea() {
return (a + b) * h / 2;
}
}
class Circle extends GraphicObject {
double r;
Circle(double r) {
this.r = r;
}
public double getArea() {
return 3.14 * r * r;
}
}
class Zhui {
GraphicObject bottom;
Double height;
Zhui(GraphicObject bottom, double height) {
this.bottom = bottom;
this.height = height;
}
void setBottom(GraphicObject bottom) {
this.bottom = bottom;
}
public double getVolum() {
return (bottom.getArea() * height) / 3;
}
}
public class aa {
public static void main(String args[]) {
Zhui zui;
GraphicObject tuxing;
tuxing = new TiXing(2, 3, 4);
System.out.println("梯形的面积是" + tuxing.getArea());
zui = new Zhui(tuxing, 30);
System.out.println("梯形底的锥的体积是" + zui.getVolum());
tuxing = new Circle(10);
System.out.println("圆形的面积是" + tuxing.getArea());
zui.setBottom(tuxing);
System.out.println("圆锥的体积是" + zui.getVolum());
}
}
全部回答
- 1楼网友:酒醒三更
- 2021-12-02 16:02
主函数是静态的,必须调用静态方法。最简单的方法,把get写成静态吧。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯