实施 类 在 Java 中定义三个形状

Implementing classes that define three shapes in Java

我想我已经实现了这些说明中的所有要求:

设计并实现一组定义形状的三个 classes:RoundShape、Sphere、Cone。对于每个 class,存储有关其大小的基本数据并提供访问和修改此数据的方法。此外,提供适当的方法来计算 Sphere 和 Cone 的面积和体积。在您的设计中,考虑形状是如何相关的,从而可以在何处实现继承。不要创建重复的实例变量。创建一个实例化 2 个 Sphere 对象(任意参数)、2 个 Cone 对象(任意参数)的 main 方法,用 ToString() 显示它们,更改每个参数中的一个参数(您的选择),然后再次显示它们。

这是我的代码:

class RoundShape{
    double shape = 9;
    double radius = 4;
    int cone1 = 3;
    int sphere1;

    public String toString(){
        return  " the man" + cone1 + "this also" + sphere1;
    }

}

//--------------------------------------------------------------
// class Sphere that extends RoundShape
//--------------------------------------------------------------
class Sphere extends RoundShape{
    double getArea(){
        double area = 4 * Math.PI * Math.pow(radius, 2);
        return area;
    } // end of getArea

    double getVolume(){
        double volume = (4/3) * Math.PI * Math.pow(radius, 3);
        return volume;  
    } // end of getVolume
} // end of the class Sphere

//---------------------------------------------------------------
// class Cone that extends RoundShape
//---------------------------------------------------------------
class Cone extends RoundShape{
    double height = 8;
    double getArea(){
        double area = Math.PI * radius * (radius + Math.sqrt(Math.pow(height, 2) + Math.pow(radius, 2)));
        return area;
    } // end of getArea for Cone

    double getVolume(){
        double volume = Math.PI * Math.pow(radius, 2) * (height/3);
        return volume;
    } // end of getVolume for Cone
} // end of the class Cone



public class Murray_A03A4 {
    public static void main(String[] args) {

        Sphere sphere1 = new Sphere();
            sphere1.getArea();
            sphere1.getVolume();
        System.out.println(sphere1);

        Cone cone1 = new Cone();
            cone1.getArea();
            cone1.getVolume();
        System.out.println(cone1);

    } // End of class header

} // End of method header

我的主要问题是,如何从 main 方法中的内容返回到 toString 方法?此外,是否在正确的 class 中找到了 toString,或者我应该将它放在新的 class 中,还是应该为每个 class 创建一个 toString?

感谢您的帮助!

使用 super 允许您访问父 class 和接口。 另外,请随意使用 casting:

var self = (BaseOfBase)super;
self.VirtualMethod();

使用 csharps 的 base 出现了一点,完全相同。

SphereCone 中实施 toString() 方法。在那些 toString 方法中,放置特定于那些 类 的细节以及超类调用的字段 super.toString()

对于 Cone,它就像:

public String toString() {
     return height + super.toString();
}

我不太确定你的问题是什么,但我认为你想问的是:"How to call the toString method on Sphere objects and Cone objects when in the main method they are simply RoundObject?"

您应该在每个 class 中覆盖 toString 方法,并且它应该 return 与那个 class 相关的信息。所以 RoundObject 不需要 return 体积,只需要半径(可能类似于 return "RoundShape with radius " + radius;)。然后你会对球体和圆锥体做同样的事情,但它也会包括形状体积。

然后在main中,你可以简单地调用RoundObjects上的toString方法。因为 toString 方法是实例方法(其标题中没有 static 关键字)所以它是 dynamically bound。意思是,将使用来自实际底层 object 的方法。

我看到您正在尝试将 sphere/cone 字段(sphere1、cone1)拉入 RoundObject,但这不是必需的。 (实际上 parent 对 child class 什么都不知道更好)

有关详细信息,请查找 polymorphism, or this video