문제

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~

도움이 되었습니까?

해결책 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.");
    }

}

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top