Pergunta

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~

Foi útil?

Solução 2

@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.");
    }

}

Outras dicas

Create pay as an abstract method in the super class, so that the sub-class then overrides/implements it:

abstract public void pay(int amount);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top