简体   繁体   English

类构造函数中的抽象方法

[英]Abstract method in the class constructor

I want to implement an Abstract Java class.我想实现一个抽象 Java 类。 One of the abstract methods must be implemented by each child class, to ensure that that part of the code will be executed individually in each child class.其中一个抽象方法必须由每个子类实现,以确保该部分代码将在每个子类中单独执行。 Is there any other way to do it and thus avoid the "warning" that appears by calling an abstract method from the constructor of the main class?有没有其他方法可以做到这一点,从而避免通过从主类的构造函数调用抽象方法而出现的“警告”?

    public abstract class Listener {

    protected Date checkTime = new Date();
    protected TypeUpdate type = UNKNOW;

    public Listener(){
        super();
        this.setTypeListener();
    }

    public void setTime(Date date) {
        if (date != null) {
            return;
        }
        this.checkTime = date;
    }

    /* Abstract methods */
    public abstract void execute();

    protected abstract void setTypeListener();
    }

Thank you.谢谢你。 ------------------------------ EDITED ---------- ------------------------------ 编辑 ----------

Ok, It's an error call an abstract method in constructor.好的,在构造函数中调用抽象方法是错误的。 So, what can I do to force inheriting classes to make a concrete constructor implementation (for example, initialize a member in one way or another?)那么,我该怎么做才能强制继承类进行具体的构造函数实现(例如,以一种或另一种方式初始化成员?)

I want to implement an Abstract Java class.我想实现一个Abstract Java类。 One of the abstract methods must be implemented by each child class, to ensure that that part of the code will be executed individually in each child class.每个子类都必须实现一种抽象方法,以确保部分代码将在每个子类中单独执行。 Is there any other way to do it and thus avoid the "warning" that appears by calling an abstract method from the constructor of the main class?还有其他方法可以避免通过从主类的构造函数中调用抽象方法来避免出现“警告”吗?

    public abstract class Listener {

    protected Date checkTime = new Date();
    protected TypeUpdate type = UNKNOW;

    public Listener(){
        super();
        this.setTypeListener();
    }

    public void setTime(Date date) {
        if (date != null) {
            return;
        }
        this.checkTime = date;
    }

    /* Abstract methods */
    public abstract void execute();

    protected abstract void setTypeListener();
    }

Thank you.谢谢你。 ------------------------------ EDITED ---------- ------------------------------编辑----------

Ok, It's an error call an abstract method in constructor.好的,在构造函数中调用抽象方法是错误的。 So, what can I do to force inheriting classes to make a concrete constructor implementation (for example, initialize a member in one way or another?)因此,我该怎么做才能强制继承类以实现具体的构造函数实现(例如,以一种或另一种方式初始化成员?)

You are arriving in the base class constructor and on returning from that constructor, in the child class constructor all field initialisations will happen and the remaining code form the child constructor.您将到达基类构造函数并从该构造函数返回,在子类构造函数中,所有字段初始化都将发生,其余代码形成子构造函数。

To ensure that some method is called you can either call that method lazily later.为了确保调用某个方法,您可以稍后延迟调用该方法。 Or pass an independent object, as "part" of the entire child.或者传递一个独立的对象,作为整个孩子的“一部分”。

public Child() {
    super(createPart());
}

private static Part createPart() {
    return new Part().withAnswer(42);
}

public Base(Part part) { ... }

One may also have a case of也可能有这样的情况

  • service offered (public final)提供的服务(公开决赛)
  • requirement implemented (abstract protected)要求已实现(抽象保护)

So:所以:

class Base {

     public final void foo() {
         onFoo();
     }

     protected void onFoo() {
         throw new IllegalStateException("Missing onFoo implementation "
             + getClass().getName());
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM