Fooを継承する2つのクラスFooとBabyFooがあります。 Mainメソッドでは、オブジェクトFoo f1 = new BabyFoo(3);
を作成します。 BabyFooには、その親メソッドをオーバーライドするcompareメソッドがあります。このメソッドは、オブジェクトが同じクラスであることを確認し、thing
プロパティも同じ値であることを確認します。Java - 親クラスオブジェクトの子プロパティを比較
私の質問は、Foo
クラスはthing
性質を持っていないとして、私は、それがタイプFoo
であるようarguementのthing
プロパティは、渡されたばかりアクセスしないか、BabyFoo
クラスでcompare
方法でありますnew BabyFoo(3)
として作成されています。
public abstract class Foo
{
public boolean compare(Foo other)
{
//compare checks to make sure object is of this same class
if (getClass() != other.getClass())
return true;
else
return false;
}
}
public class BabyFoo extends Foo
{
protected int thing;
public void BabyFoo(int thing)
{
this.thing = thing;
}
@Override
public boolean compare(Foo other)
{
//compares by calling the parent method, and as an
//additional step, checks if the thing property is the same.
boolean parent = super.compare(other);
//--question do-stuff here
//how do I access other.thing(), as it comes in
//as a Foo object, which doesn't have a thing property
}
}