调用类对象时出错?

时间:2016-12-03 08:42:05

标签: java multiple-inheritance

所以我在GrownUp类中创建了一个ArrayList,然后生成一个for循环来从另一个类中调用一个方法。当我试图调用这个新方法的对象时,我收到一个错误,它没有识别该对象。这就是方法的样子:

public void personShowering()
{
    PowerShower callshower = new PowerShower(1,1,1,1);

if(people.size()>1)
callshower.shower(people.get(0));
    }

}

   import java.util.ArrayList;

public abstract class Person 
{    
    ArrayList<Person> people;


Person(int age, String name) 
{


}
public void shower(Person x)
{
    people.get(0).shower(//what goes here?);
}

}

错误:

method shwer() in class perosn cannot be applied to given types;
required: Person
found: no given types

callshower应该引用一个名为PowerShower的类,该类是使用PowerShower callshower = new PowerShower(1,1,1,1);创建的,所以我很困惑为什么它会在Person中查找{1}}课程? PowerShower类非常大,但我会发布它:

PowerShower

public class PowerShower extends Shower
{

    public int isOn = -1;
    public int isOff = 0;
    public int incrementTime;
    public int varPass = -1;

     @Override
     public int currentState()
    {

        if (varPass == 0)
        return isOff;
        else
        {
            return isOn;
        }
        //returns isOn;
}

    @Override
        public void useTime(int defaultTime)
        {

            defaultTime = 15;
            incrementTime = 1;

        }

    @Override
        public void shower()
        {

            PowerShower shower = new PowerShower(1,1,1,1);

            shower.timePasses();
        }

        @Override
         public void timePasses()
         {

             if(varPass == isOff)
                 varPass = 0;
             else
             {

             GasMeter.getInstance().incrementConsumed(waterUse);  
             GasMeter.getInstance().incrementConsumed(10);     
             int gasconsumed = GasMeter.getInstance().getGasUsed();    

             WaterMeter.getInstance().incrementConsumed(waterUse);  
             WaterMeter.getInstance().incrementConsumed(5);     
             int waterconsumed = WaterMeter.getInstance().getWaterUsed();

             System.out.println("Power shower water consumption = " + waterconsumed);
             System.out.println("Power shower gas consumption = " + gasconsumed);

             }

         }

    PowerShower(int electricityUse, int gasUse, int waterUse, int timeOn)  
{
    super(electricityUse, gasUse, waterUse, timeOn);

    this.electricityUse = 0 * incrementTime;
    this.gasUse = 10 * incrementTime; //10x1;
    this.waterUse = 5 * incrementTime; //5x1;
    this.timeOn = 15 * incrementTime;

} 

}

我不太清楚为什么我会收到此错误,因为我创建了该类的对象并调用了它,但它似乎并没有识别出该对象?任何事都会很棒,谢谢。

1 个答案:

答案 0 :(得分:2)

你想做的事是不可能的。我不知道为什么你认为它是可能的。除了接口和默认方法之外,这不是多重继承看起来像Java不支持的方式。如果有可能它会看起来像这样(WON&#39; T COMPILE):

public class GrownUp extends Person, SuperShower
.... 
people.get(0).shower()

您可以调用对象的方法。你不能做的是:

people.get(0).callshower

Person可能没有方法(或字段)callshowercallshower是包含对象的变量。如果存在,您可以在其上调用方法shower

好像你想把这个人和一起洗澡。你能做的是:

  • 将方法shower添加到Person并调用它:people.get(0).shower()

  • PowerShower.shower()更改为参数Person,然后执行以下调用:callshower.shower(people.get(0))