如何使用instanceof确定一个类的左右对象是否属于另一个类

时间:2018-06-28 04:23:51

标签: java

因此,我得到以下提示:

  

调用静态方法以返回ArithExpression数组中有多少个元素是SIMPLE二进制表达式的计数(是ArithBinaryExpression对象,其中leftExpression和rightExpression变量引用ArithValue的对象),您必须在此处使用instanceinstanceofoperator

我很难理解的是,如何实现instanceof来确定某个类的左右表达式对象是另一个类的实例

这是我所需的类的代码:

public interface ArithExpression {
    public abstract double result();
}

public class ArithValue implements ArithExpression {
private double doubValue;

ArithValue(double dVal) {
    this.doubValue = dVal;
}

public double result() {
    return doubValue;
}
@Override
public String toString() {
    return String.valueOf(doubValue);
}

}

public abstract class ArithBinaryExpression implements ArithExpression {
protected static ArithExpression leftExpression;
protected static ArithExpression rightExpression;

// constructor
public ArithBinaryExpression(ArithExpression left, ArithExpression right) {
    if (left == null) {
        leftExpression = new ArithValue(0);
    }
    else 
    leftExpression = left;

    if (right == null) {
        rightExpression = new ArithValue(0);
    }
    else
    rightExpression = right;


}
// end constructor

// accessors
public ArithExpression getLeftExpression() {
    return leftExpression;
}

public ArithExpression getRightExpression() {
    return rightExpression;
}

更新#2: 这是isSimple()方法:

public boolean isSimple() {
    boolean isSimple = false;
    if (leftExpression instanceof ArithValue && rightExpression instanceof ArithValue) {
        isSimple = true;
    }
    return isSimple;
}

这是ArithExpression数组实现的迭代:

    public static int simpleBinary(ArithExpression[]arithExpressions) {
    int sum = 0;
    for (int i = 0; i < arithExpressions.length; i++)
    {
        if(arithExpressions[i].isSimple() == true) {
            sum++;
        }
    }
    return sum;
}

我收到一条错误消息,指出ArithExpression类型的isSimple()方法未定义。

1 个答案:

答案 0 :(得分:0)

您好,您可以在instanceof之下确定对象的类别在

之下
public static void main(String[] args) {
    SuperClass s = new SuperClass();
    Child1 c = new Child1();
    Child2 cc = new Child2();

    checkInstance(s);
    checkInstance(c);
    checkInstance(cc);
}

private static void checkInstance(SuperClass c) {
    if(c instanceof SuperClass){
        System.out.println(" c Instance of superclass");
    }
    if(c instanceof Child1){
        System.out.println(" c Instance of Child1");
    }
    if(c instanceof Child2){
        System.out.println(" c Instance of Child1");
    }
}

这将帮助您查明确切的类实例。 请注意,Child1和Child2扩展了SuperClass。

下面的代码用于保留每个类的实例数

static int CHILD_1_COUNTER = 0;
static int CHILD_2_COUNTER = 0;

public static void main(String[] args) {
    SuperClass s = new SuperClass();
    Child1 c1 = new Child1();
    Child2 c2 = new Child2();
    Child2 c3 = new Child2();
    Child2 c4 = new Child2();
    List<SuperClass> list = new ArrayList<SuperClass>();
    list.add(s);
    list.add(c1);
    list.add(c2);
    list.add(c3);
    list.add(c4);

    checkInstance(list);

    System.out.println("No. of Child1 instances" + CHILD_1_COUNTER);
    System.out.println("No. of Child2 instances" + CHILD_2_COUNTER);

}

private static void checkInstance(List<SuperClass> list) {
    list.forEach(element ->{
        if(element instanceof Child1)
            CHILD_1_COUNTER++;
        if(element instanceof Child2)
            CHILD_2_COUNTER++;
    });

}

如果解决了问题,请您接受答案:)