强制子类使用超类的@Override 方法。超类中的方法必须有主体
Force SubClasses to @Override method from SuperClass. Method in SuperClass must have body
我想从 SuperClass
.
强制 SubClasses
到 @Override
方法
SuperClass
中的方法不能 abstract
,因为我想提供一些基本实现。
这是我的代码示例:
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);
}
}
}
这个post很有趣:
MustOverrideException and MethodNotOverridenException
请帮帮我
您可以使用两种方法:
public abstract String methodToBeOverridenInSubClass(String name);
protected String commonMethodToUseInSubClasses(String name) {
// some basic implementation
return name;
}
这样子类必须重写 methodToBeOverridenInSubClass
但您仍然可以对 commonMethodToUseInSubClasses
.
中的所有子 类 使用通用代码
也许Template method pattern解决了您正在寻找的问题。
您在其中创建了一个父级 class 并实现了一种方法,该方法描述了调用其他方法(步骤)的过程。这些步骤是抽象的,因此它们由子 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
.
SubClasses
到 @Override
方法
SuperClass
中的方法不能 abstract
,因为我想提供一些基本实现。
这是我的代码示例:
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);
}
}
}
这个post很有趣: MustOverrideException and MethodNotOverridenException
请帮帮我
您可以使用两种方法:
public abstract String methodToBeOverridenInSubClass(String name);
protected String commonMethodToUseInSubClasses(String name) {
// some basic implementation
return name;
}
这样子类必须重写 methodToBeOverridenInSubClass
但您仍然可以对 commonMethodToUseInSubClasses
.
也许Template method pattern解决了您正在寻找的问题。 您在其中创建了一个父级 class 并实现了一种方法,该方法描述了调用其他方法(步骤)的过程。这些步骤是抽象的,因此它们由子 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. */
// ...
}