如何从 Java 中的继承方法调用特定的重写方法?
How do I call a specific overridden method from an inherited method in Java?
我有一个名为 LotteryTicket
的 class,它有 3 个子 class:Pick4
、Pick5
和 Pick6
。我希望能够调用一个方法 public void pickNumbers()
,一旦被调用,将能够识别正在使用哪个 LotteryTicket subclass 并询问适当数量的参数(即调用 pickNumbers()
在 Pick5
的实例中将要求 5 个整数)。
我试图通过在 LotteryTicket
class 中为 4、5 和 6 提供 public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
并调用 pickNumbers()
方法来解决这个问题基于字段 pickAmount
的适当方法(将被覆盖)。不幸的是,这将需要提供论据。
这里是 LotteryTicket
class:
public class LotteryTicket
{
protected int pickAmount;
protected boolean isRandom;
protected ArrayList<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
public LotteryTicket(int pickAmount, boolean isRandom)
{
// INITIALIZATION OF VARIABLES
this.pickAmount = pickAmount;
this.isRandom = isRandom;
// CONSTRUCTION OF ARRAYLIST
numbersPicked = new ArrayList(pickAmount);
}
/**
* The number pick method for ALL subclasses. Running this method will run the appropriate pickxNumbers
* method, where x is the pickAmount.
*
*/
public void pickNumbers()
{
if(pickAmount == 4){
pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
}
if(pickAmount == 5){
pick5Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick)
}
if(pickAmount == 6){
pick6Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick, int sixthPick)
}
}
/**
* The number pick method for the Pick4 subclass.
*
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
}
Pick4
class:
public class Pick4 extends LotteryTicket
{
/**
* Constructor for objects of class Pick4
*/
public Pick4(boolean isRandom)
{
super(4, isRandom);
}
/**
* Overloaded pick4Numbers() method. Depending on the ticket type, the amount of picks will vary.
* For example, Pick4 tickets will only ask for 4 int values, Pick5 tickets will ask for 5, etc.
*
*@param int firstPick
*@param int secondPick
*@param int thirdPick
*@param int fourthPick
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
numbersPicked.add(new Integer(firstPick));
numbersPicked.add(new Integer(secondPick));
numbersPicked.add(new Integer(thirdPick));
numbersPicked.add(new Integer(fourthPick));
}
如果您想从 LotteryTicket
扩展,使 pickNumbers()
方法抽象并接受 List
或可变参数:
public abstract class LotteryTicket {
//...
abstract public void pickNumbers(int... numbers);
//...
}
然后在实施中类,e。 G。 Pick4
:
public class Pick4 extends LotteryTicket {
//...
@Override
public void pickNumbers(int... numbers) {
if (numbers.length != 4)
throw IllegalArgumentException("For Pick4, there must be exactly 4 numbers!");
for (int n : numbers) {
numbersPicked.add(n); // no need in explicit boxing, Java will do it for you
}
}
}
我认为这样做会更好:
public class LotteryTicket {
protected int pickAmount;
protected boolean isRandom;
protected List<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
protected int[] numbersToPick;
//To create random valued ticket
public LotteryTicket(int pickAmount) {
this.pickAmount = pickAmount;
isRandom = true;
}
//To create specified valued ticket
public LotteryTicket(int... numbersToPick) {
pickAmount = numbersToPick.length;
isRandom = false;
this.numbersToPick = numbersToPick;
}
public void pickNumbers() {
numbersPicked = new ArrayList<>(pickAmount);
if (isRandom) {
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(random.nextInt());
}
} else {
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(numbersToPick[i]);
}
}
}
}
然后Pick4, Pick5 ...等将是这样的:
public class Pick4 extends LotteryTicket {
//For random valued ticket
public Pick4() {
super(4);
}
//For specified valued ticket
public Pick4(int pick1, int pick2, int pick3, int pick4) {
super(pick1, pick2, pick3, pick4);
}
}
我有一个名为 LotteryTicket
的 class,它有 3 个子 class:Pick4
、Pick5
和 Pick6
。我希望能够调用一个方法 public void pickNumbers()
,一旦被调用,将能够识别正在使用哪个 LotteryTicket subclass 并询问适当数量的参数(即调用 pickNumbers()
在 Pick5
的实例中将要求 5 个整数)。
我试图通过在 LotteryTicket
class 中为 4、5 和 6 提供 public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
并调用 pickNumbers()
方法来解决这个问题基于字段 pickAmount
的适当方法(将被覆盖)。不幸的是,这将需要提供论据。
这里是 LotteryTicket
class:
public class LotteryTicket
{
protected int pickAmount;
protected boolean isRandom;
protected ArrayList<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
public LotteryTicket(int pickAmount, boolean isRandom)
{
// INITIALIZATION OF VARIABLES
this.pickAmount = pickAmount;
this.isRandom = isRandom;
// CONSTRUCTION OF ARRAYLIST
numbersPicked = new ArrayList(pickAmount);
}
/**
* The number pick method for ALL subclasses. Running this method will run the appropriate pickxNumbers
* method, where x is the pickAmount.
*
*/
public void pickNumbers()
{
if(pickAmount == 4){
pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
}
if(pickAmount == 5){
pick5Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick)
}
if(pickAmount == 6){
pick6Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick, int sixthPick)
}
}
/**
* The number pick method for the Pick4 subclass.
*
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
}
Pick4
class:
public class Pick4 extends LotteryTicket
{
/**
* Constructor for objects of class Pick4
*/
public Pick4(boolean isRandom)
{
super(4, isRandom);
}
/**
* Overloaded pick4Numbers() method. Depending on the ticket type, the amount of picks will vary.
* For example, Pick4 tickets will only ask for 4 int values, Pick5 tickets will ask for 5, etc.
*
*@param int firstPick
*@param int secondPick
*@param int thirdPick
*@param int fourthPick
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
numbersPicked.add(new Integer(firstPick));
numbersPicked.add(new Integer(secondPick));
numbersPicked.add(new Integer(thirdPick));
numbersPicked.add(new Integer(fourthPick));
}
如果您想从 LotteryTicket
扩展,使 pickNumbers()
方法抽象并接受 List
或可变参数:
public abstract class LotteryTicket {
//...
abstract public void pickNumbers(int... numbers);
//...
}
然后在实施中类,e。 G。 Pick4
:
public class Pick4 extends LotteryTicket {
//...
@Override
public void pickNumbers(int... numbers) {
if (numbers.length != 4)
throw IllegalArgumentException("For Pick4, there must be exactly 4 numbers!");
for (int n : numbers) {
numbersPicked.add(n); // no need in explicit boxing, Java will do it for you
}
}
}
我认为这样做会更好:
public class LotteryTicket {
protected int pickAmount;
protected boolean isRandom;
protected List<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
protected int[] numbersToPick;
//To create random valued ticket
public LotteryTicket(int pickAmount) {
this.pickAmount = pickAmount;
isRandom = true;
}
//To create specified valued ticket
public LotteryTicket(int... numbersToPick) {
pickAmount = numbersToPick.length;
isRandom = false;
this.numbersToPick = numbersToPick;
}
public void pickNumbers() {
numbersPicked = new ArrayList<>(pickAmount);
if (isRandom) {
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(random.nextInt());
}
} else {
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(numbersToPick[i]);
}
}
}
}
然后Pick4, Pick5 ...等将是这样的:
public class Pick4 extends LotteryTicket {
//For random valued ticket
public Pick4() {
super(4);
}
//For specified valued ticket
public Pick4(int pick1, int pick2, int pick3, int pick4) {
super(pick1, pick2, pick3, pick4);
}
}