2

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

抽象类:

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();
   }
}
4

6 回答 6

2

考虑以下抽象类和扩展实现的情况。 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 有一个允许调用者指定值的构造函数。

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

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

于 2012-07-25T19:18:12.207 回答
1

When you have explicit constructor defined in super class and no constructor without arguments defined, your child class should explicitly call the super class constructor.

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

or, if you don't want call super class constructor with parameters, you need to add constructor with no arguments in super class.

public abstract class TestParent {
    int size;
    public TestParent(){

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

}
于 2012-07-25T19:11:04.327 回答
1
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);
     }

 }
于 2012-07-25T19:11:23.783 回答
1

Use super to call parent (TestParent.TestParent(int)) constructor:

public class TestChild extends TestParent{

    public TestChild(int i) {
        super(i);
    }

    //...

}

or if you want to use some constant:

    public TestChild() {
        super(42);
    }

Note that there is no such thing as abstract constructor in Java. Essentially there is only one constructor in TestParent which must be called before calling TestChild constructor.

Also note that super() must always be the first statement.

于 2012-07-25T19:11:51.473 回答
0

You code wont compile because your base class does not have a default constructor. Either you need to provide it in base class or you need to provide parameterized constructor in derived class and invoke super.

于 2012-07-25T19:13:04.343 回答
0
 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;
             }

}

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

于 2012-07-25T19:13:14.577 回答