简体   繁体   English

Java - 比较父类对象上的子属性

[英]Java - Compare child property on Parent Class Object

I have 2 classes, Foo, and BabyFoo which inherits Foo.我有 2 个类,Foo 和继承 Foo 的 BabyFoo。 In the Main method, I create an object Foo f1 = new BabyFoo(3);在 Main 方法中,我创建了一个对象Foo f1 = new BabyFoo(3); . . BabyFoo has a compare method that overrides its parent method, that compares to make sure an object is of the same class, and making sure that the thing property is the same value as well. BabyFoo 有一个比较方法,它覆盖了它的父方法,比较以确保一个对象是同一个类,并确保thing属性也是相同的值。

My question, is in the compare method in the BabyFoo class, how do I access the thing property of the arguement getting passed in, as it is of type Foo , as the Foo class doesn't have a thing property, even though it was created as a new BabyFoo(3) .我的问题是在BabyFoo类的compare方法中,我如何访问传入的参数的thing属性,因为它是Foo类型,因为Foo类没有thing属性,即使它是创建为一个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
    }
}

You'll need to cast the other object to be a BabyFoo by writing something like您需要通过编写类似的内容将other对象转换为 BabyFoo

((BabyFoo)other).thing

This is assuming that everything else is how you want it.这是假设其他一切都是你想要的。

Check to see if the other object is of type BabyFoo .检查other对象是否属于BabyFoo类型。 You can then perform a cast on the object, which would allow you to access the thing variable:然后,您可以对对象执行强制转换,这将允许您访问thing变量:

if (other instanceof BabyFoo)
    BabyFoo bFoo = (BabyFoo) other;

Since the method gets Foo as a variable and not BabyFoo , you can't get to it's thing field without casting.由于该方法将Foo作为变量而不是BabyFoo ,因此您无法在不进行转换的情况下访问它的事物字段。

However, casting should be done safely, you need to verify you are comparing to a BabyFoo and not Foo但是,转换应该安全地完成,您需要验证您正在比较的是BabyFoo而不是Foo

@Override
public boolean compare(Foo other) {
    return other instanceof BabyFoo &&
       super.compare(other) && 
       this.thing == ((BabyFoo)other).thing;
}

You need to downcast Foo class to BabyFoo class.您需要将FooBabyFooBabyFoo类。

@Override
public boolean compare(Foo other) {
   if (other instanceof BabyFoo) { // check whether you got the BabyFoo type class
       BabyFoo another = (BabyFoo) other;
       return super.compare(another) && another.thing == this.thing;
   }
   return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM