【问题标题】:Extending an abstract constructor?扩展抽象构造函数?
【发布时间】:2012-07-25 19:08:29
【问题描述】:

所以我在一些我正在使用的代码中遇到了一些障碍。基本上我有以下三个代码:

抽象类:

public abstract class TestParent {
    int size;

    public TestParent(int i){
        size = i;
    }

}

儿童班:

     public class TestChild extends TestParent{
     public void mult(){
         System.out.println(this.size * 5);
     }

 }

实施:

public class TestTest {

   public static void main(String args[]) {
       TestChild Test = new TestChild(2);
       Test.mult();
   }
}

【问题讨论】:

  • 我错过了吗?我没有看到问题:)
  • @jeff 问题是:为什么不编译
  • 当你的超类不使用默认的无参数构造函数时——实现者必须提供构造函数

标签: java constructor abstract


【解决方案1】:

考虑以下抽象类和扩展实现的情况。 https://stackoverflow.com/a/260755/1071979

abstract class Product { 
    int multiplyBy;
    public Product( int multiplyBy ) {
        this.multiplyBy = multiplyBy;
    }

    public int mutiply(int val) {
       return muliplyBy * val;
    }
}

class TimesTwo extends Product {
    public TimesTwo() {
        super(2);
    }
}

class TimesWhat extends Product {
    public TimesWhat(int what) {
        super(what);
    }
}

超类 Product 是抽象的并且有一个构造函数。具体类 TimesTwo 有一个默认构造函数,它只是硬编码值 2。具体类 TimesWhat 有一个允许调用者指定值的构造函数。

注意:由于父抽象类中没有默认(或无参数)构造函数,因此必须指定子类中使用的构造函数。

抽象构造函数将经常用于强制类约束或不变量,例如设置类所需的最小字段。

【讨论】:

    【解决方案2】:
    public class TestChild extends TestParent{
         public TestChild(int i){
             super(i); // Call to the parent's constructor.
         }
         public void mult(){
             System.out.println(super.size * 5);
         }
    
     }
    

    【讨论】:

      【解决方案3】:

      使用super调用父(TestParent.TestParent(int))构造函数:

      public class TestChild extends TestParent{
      
          public TestChild(int i) {
              super(i);
          }
      
          //...
      
      }
      

      或者如果你想使用一些常量:

          public TestChild() {
              super(42);
          }
      

      请注意,Java 中没有抽象构造函数之类的东西。本质上,TestParent 中只有一个构造函数,必须在调用 TestChild 构造函数之前调用它。

      还要注意super() 必须始终是第一个语句。

      【讨论】:

        【解决方案4】:

        当您在超类中定义了显式构造函数并且没有定义没有参数的构造函数时,您的子类应该显式调用超类构造函数。

        public class TestChild extends TestParent{
                TestChild ()
                {
                    super(5);
                }
            }
        

        或者,如果你不想调用带参数的超类构造函数,你需要在超类中添加不带参数的构造函数。

        public abstract class TestParent {
            int size;
            public TestParent(){
        
            }
            public TestParent(int i){
                size = i;
            }
        
        }
        

        【讨论】:

          【解决方案5】:

          您的代码无法编译,因为您的基类没有默认构造函数。要么你需要在基类中提供它,要么你需要在派生类中提供参数化构造函数并调用super。

          【讨论】:

            【解决方案6】:
             public class TestChild extends TestParent{
                         public TestChild (int i)
                         {
                           super(i * 2);
                         }
            
            }
            

            此代码将使用 i 的双精度值。这是一个压倒一切的,虽然我不确定你想问什么。

            其他解决方案:

             public class TestChild extends TestParent{
                         public TestChild (int i)
                         {
                           super(i);
                           this.size = 105;
                         }
            
            }
            

            对于此解决方案,大小必须是受保护的或公开的。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-09-26
              • 2015-06-02
              • 1970-01-01
              • 2014-09-13
              • 1970-01-01
              • 2012-01-09
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多