强制子类从 SuperClass 到 @Override 方法.SuperClass中的方法必须有主体 [英] Force SubClasses to @Override method from SuperClass. Method in SuperClass must have body

查看:25
本文介绍了强制子类从 SuperClass 到 @Override 方法.SuperClass中的方法必须有主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 SuperClass 强制 SubClasses@Override 方法.

I would like to force SubClasses to @Override method from SuperClass.

SuperClass 中的方法不能是 abstract,因为我想提供一些基本的实现.

Method in SuperClass can't be abstract, cause I wanna provide some basic implementation.

这是我的代码示例:

public abstract class GenericModel<T extends GenericModel> {
    long id, counter;

    public String methodToBeOverridenInSubClass(String name) {
        // some basic implementation
        // rest details will be provided by subclass
        return name;
    }

}
public class SubClass extends GenericModel<SubClass> {
    @Override
    public String methodToBeOverridenInSubClass(String name) {
        switch (name) {
            case "name": return "Real name";
            default: super.methodToBeOverridenInSubClass(name);
        }
    }
}

这篇文章很有趣:MustOverrideException 和 MethodNotOverridenException

请帮帮我

推荐答案

也许您正在寻找的内容已通过 模板方法模式.在其中创建一个父类,其中实现了一个方法,该方法描述了调用另一个方法(步骤)的过程.这些步骤是抽象的,因此它们由一个子类实现.

Maybe what your are looking for is resolved by Template method pattern. In which you create a parent class with one method implemented which describe a process that calls anothers methods (steps). These steps are abstract so they are implemented by a sub class.

来自维基百科的示例:

/**
* An abstract class that is common to several games in
* which players play against the others, but only one is
* playing at a given time.
*/

abstract class Game {

protected int playersCount;
abstract void initializeGame();
abstract void makePlay(int player);
abstract boolean endOfGame();
abstract void printWinner();

/* A template method : */
public final void playOneGame(int playersCount) {
    this.playersCount = playersCount;
    initializeGame();
    int j = 0;
    while (!endOfGame()) {
        makePlay(j);
        j = (j + 1) % playersCount;
    }
    printWinner();
}
}

//Now we can extend this class in order 
//to implement actual games:

class Monopoly extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
    // Initialize players
    // Initialize money
}
void makePlay(int player) {
    // Process one turn of player
}
boolean endOfGame() {
    // Return true if game is over 
    // according to Monopoly rules
}
void printWinner() {
    // Display who won
}
/* Specific declarations for the Monopoly game. */

// ...
}

class Chess extends Game {

/* Implementation of necessary concrete methods */
void initializeGame() {
    // Initialize players
    // Put the pieces on the board
}
void makePlay(int player) {
    // Process a turn for the player
}
boolean endOfGame() {
    // Return true if in Checkmate or 
    // Stalemate has been reached
}
void printWinner() {
    // Display the winning player
}
/* Specific declarations for the chess game. */

// ...
}

这篇关于强制子类从 SuperClass 到 @Override 方法.SuperClass中的方法必须有主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆