【问题标题】:Java Inheritance constructor troubleJava继承构造函数的麻烦
【发布时间】:2016-07-17 23:09:58
【问题描述】:

我正在尝试用 Java 实现一个程序,它有一个父类 RoundShapes 和两个子类 Sphere 和 Cone。当我尝试在我的子类中创建一个构造函数时,它给了我一个错误,即构造函数不能应用于该给定类型。 然后我研究并找到了一个关于继承的教程,他们使用了一个超级构造函数。我尝试使用它,但现在它给了我一个错误,即在超级构造函数之前不能使用半径?!我不在构造函数的范围内,所以我不确定那是什么意思。

import java.util.*;

public class Miller_A03Q4 {


    public static void main(String[] args) {
        Sphere sphere1 = new Sphere();
        RoundShape cone1 = new RoundShape();
        RoundShape.setRadius(4);

    }




    public static class RoundShape{
         double area;
         double volume;
         double radius;
         String shape;       
        //            
        public RoundShape(double r){

          radius = r;

        }
        // set radius method
       public  void setRadius(double r){
           radius = r;
       }   
       // get area method
       public double getArea(){
           return area;
       }
       // get volume
       public double getVolume(){
           return volume;


    }
    }   
       // sphere is-a from roundshape
    static class Sphere extends RoundShape{

        public Sphere(double r){
            radius = r;

        }
         //set area 
         public void setArea(){
         area = 4 * Math.PI * radius * radius;

         } 






        // get volume
        public void setVolume(){
        volume = (4/3) * Math.PI * radius * radius * radius;        

    }

    }

    //cone class is-a from roundshape
    class Cone extends RoundShape{
        double height;

        //set area
        public void setArea(){
            area = Math.PI * radius * (radius + Math.sqrt( height * height + radius * radius));

    } 
// cone volume
        public void setVolume(){
            volume = Math.PI * radius * radius * (height/3);

    } 
}


}

【问题讨论】:

  • @AndrewL。它们被称为静态内部类,它们是一种语言特性。
  • @4castle 真的吗?我猜你每天都会学到一些新东西

标签: java inheritance constructor


【解决方案1】:

所有构造函数都会在调用super() 调用自己的构造函数之前调用其超类的构造函数。如果您忽略对super() 的调用,它会隐式插入调用。

在你的例子中,你没有在你的超类中指定一个带有 0 个参数的构造函数,所以 Java 无法推断 super() 的参数应该是什么,所以它会抛出一个编译器错误并要求你指定这些参数应该是什么。

要修复您的代码,您有 2 个选项

  1. RoundShape 中创建一个接受0 个参数的构造函数。
  2. 显式调用super(),在其中指定参数。

选项 #2 看起来像这样。请注意,对super() 的调用必须在任何其他语句之前。

public Sphere(double r) {
    super(r);
    // other statements here
}

【讨论】:

  • @4castle 谢谢你的帮助!我有 super(radius) 而不是 super(r)....smh.
【解决方案2】:

感谢这个论坛上的建议和一些额外的研究,我能够更正我的程序。我在发布的原始代码中也有几个运行时错误,所以我想提供更新后的代码,以防像我这样的新手偶然发现。

一些专业是: 1. 我不需要包含 setArea() 和 setVolume() 方法,而是将公式放在 getArea() 和 getVolume() 中。 2. 我不需要在构造函数中包含体积和面积。 3. 我还包括了一个 toString() 方法,这样我就可以访问我的数据了。

我确信这远不是最好和最有效的代码,但它似乎有效。我在下面提供了我的新代码和更新代码以供参考。感谢所有容忍我愚蠢问题的人!

import java.util.*;

public class Miller_A03Q4 {


    public static void main(String[] args) {
        Sphere sphere1 = new Sphere(4);
        Cone cone1 = new Cone(3,7);
        System.out.println(cone1.toString());
        System.out.println(sphere1.toString());
        cone1.setHeight(10);
        sphere1.setRadius(3);
        System.out.println(cone1.toString());
        System.out.println(sphere1.toString());



    }



// parent Roundhape
    public static class RoundShape{
         double area;
         double volume;
         double radius;
         String shape;
         double height;
        //            
        //constructor
        public RoundShape(double r){

          radius = r;

        }
        // set radius method
       public  void setRadius(double r){
           radius = r;
       }   
       // get area method
       public double getArea(){
           return area;
       }
       // get volume
       public double getVolume(){
           return volume;


    }
       //toString
         public String toString(){
            return "Shape: " + shape +" Radius: " + radius + " Height: "
                    + height+ " Volume: " + this.getVolume() + " Area: "+ this.getArea();


        }   
    }   
       // sphere is-a from roundshape
    public static class Sphere extends RoundShape{
        //constructor
        public Sphere(double r){
            super(r);
            radius = r;
            shape = "sphere";


        }
        //get area
        public double getArea(){

               area = 4 * Math.PI * radius * radius;
                return area;
       }
       // get volume
       public double getVolume(){

           volume = 4 * Math.PI * (radius * radius * radius)/3;
           return volume;
    }
    }
    //cone class is-a from roundshape
    public static class Cone extends RoundShape{

        // cone with radius and height
        public Cone(double r, double h){
            super(r);
            radius = r;
            height = h;
            shape = "cone";


        }
        //set height
        public void setHeight(double h){
            height = h;
        }
        // get area
        public double getArea(){

               area = Math.PI * radius * (radius + Math.sqrt( height * height + radius * radius));
                return area;
       }
       // get volume
       public double getVolume(){

           volume = Math.PI * radius * radius * (height/3);
           return volume;
    }
}


}

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2011-09-09
    • 2010-12-11
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多