Here is the basic skeleton code to explain my situation.
This is the super abstract class:
public abstract class Person
{
public void buyFood(String foodName, int payment)
{
System.out.println("Buy " + foodName + " and pay $" + payment + ".");
pay(payment);
}
}
This is a sub class of the super abstract class: (note that I deleted other functions such as constructors and methods to make the post short
public class Visitor extends Person
{
public void pay(int amount)
{
money_v -= amount;
System.out.println(this.to_s() + " has got HK$" + money_v + "left.");
}
}
I want to use this public void pay(int amount) method in the abstract class; however, the super abstract class Person will not accept the pay(payment) because the method is not within the scope. How to make this work?
Thanks~
在超类中创建pay作为抽象方法,以便子类随后覆盖/实现它:
abstract public void pay(int amount);
@LarsChung : code is attached below:
public abstract class Person
{
public void buyFood(String foodName, int payment)
{
System.out.println("Buy " + foodName + " and pay $" + payment + ".");
pay(payment);
}
public abstract void pay(int amt);
}
public class Visitor extends Person
{
@Override
public void pay(int amount)
{
money_v -= amount;
System.out.println(this.to_s() + " has got HK$" + money_v + "left.");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.