gpt4 book ai didi

java - 多级继承不起作用

转载 作者:行者123 更新时间:2023-11-30 04:08:32 25 4
gpt4 key购买 nike

我正在为学校建立一个实验室,它即将完成,但有一个部分我无法开始工作。继承有效,除非我到达 Cube。由于某种原因,它不会计算面积或体积(它只是得出 0)。我认为这是我从 Square 到 Cube 的继承方式的问题。帮助会很棒!

package InheritanceTest;

import javax.swing.JOptionPane;

public class InheritanceTest {

public static void main(String[] args) {
String input = "";
Point point = new Point();

input = getinput("Set variable X");
point.setx(input);
input = getinput("Set variable Y");
point.sety(input);
System.out.println("Point, x = " + point.getx() + " y = " + point.gety());

Square square = new Square();
input = getinput("Set variable Side Length");
square.setSideLength(input);
System.out.println("Square, x = " + point.getx() + " y = " + point.gety()
+ " Area = " + square.getAreaOfSquare() + " Perimeter = "
+ square.getPerimeterOfSquare());

Cube cube = new Cube();
input = getinput("Set variable depth");
cube.setDepth(input);
System.out.println("cube, x = " + point.getx() + " y = " + point.gety()
+ " Depth = " + cube.getDepth() + " Area = " + cube.getAreaOfCube()
+ " Volume = " + cube.getVolumeOfCube());
}

private static String getinput(String string) {
String x = JOptionPane.showInputDialog(string);
return x;
}
}
package InheritanceTest;

public class Cube extends Square {

private int depth;

Cube() {
super();
depth = 0;
}

Cube(int x, int y, int sideLength, int d) {
super(x, y, sideLength);
this.depth = d;
}

public int getAreaOfCube() {
return (6 * sideLength * sideLength);
}

public int getVolumeOfCube() {
return (sideLength * sideLength * sideLength);
}

public String getDepth() {
return Integer.toString(depth);
}

public void setDepth(String i) {
depth = Integer.parseInt(i);
}
}
package InheritanceTest;

public class Point {

private int x;
private int y;

Point() {
x = 0;
y = 0;
}

Point(int x, int y) {
this.x = x;
this.y = y;
}

public String getx() {
return Integer.toString(x);
}

public String gety() {
return Integer.toString(y);
}

public void setx(String input) {
x = Integer.parseInt(input);
}

public void sety(String input) {
y = Integer.parseInt(input);
}
}
package InheritanceTest;

public class Square extends Point {

protected int sideLength;

Square() {
super();
sideLength = 0;
}

Square(int x, int y, int l) {
super(x, y);
this.sideLength = l;
}

public int getAreaOfSquare() {
return sideLength * sideLength;
}

public int getPerimeterOfSquare() {
return sideLength + sideLength;
}

public String getSideLength() {
return Integer.toString(sideLength);
}

public void setSideLength(String input) {
sideLength = Integer.parseInt(input);
}
}

最佳答案

当您创建立方体(new Cube())时,您没有为其延伸的方形对象设置边长(或 x 和 y)。

Cube(){
// This is the constructor called.
super();
depth = 0;
}

Cube(int x, int y, int sideLength, int d){
super(x, y, sideLength);
this.depth = d;
}

您可能希望将 x、y 和长度值提取到变量中并使用“new Cube(x, y, length, depth)”

类似下面的内容

    String x = getinput("Set variable X");
String y = getinput("Set variable Y");
String sideLength = getinput("Set variable Side Length");
String depth getinput("Set variable depth");

Cube cube = new Cube(x, y, sideLength, depth);

关于java - 多级继承不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184976/

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