繁体   English   中英

用Java中的超类的两个参数扩展类

[英]Extends class with two parameters of super class in Java

我正在尝试编写一个扩展另一个抽象类的抽象类。 超类是Expression

public abstract class Expression {
    String expression;

    /*Empty Constructor for Expression object */
    public Expression(){
    }

    /*Constructor for Expression object with String expression*/
    public Expression(String expression){
        this.expression = new String(expression);
    }

而extends类是CompoundExpression ,它具有两个来自Expression类型的变量:

public abstract class CompoundExpression extends Expression {
    Expression firstOperand, secondOperand;

    public CompoundExpression(Expression first, Expression second){
        String strFirst = first.expression;
        String strSecond = second.expression;

        this.firstOperand = super(strFirst);
        this.secondOperand = super(strSecond);
    }
}

我认为自己做错了方法,因为它无法正常工作...有人可以帮我理解原因吗? 以及我如何才能正确地做到这一点?

我想也许是这样写的:

public abstract class CompoundExpression extends Expression {

    Expression firstOperand, secondOperand;

    public CompoundExpression(Expression first, Expression second){

        this.firstOperand.expression = first.expression;
        this.secondOperand.expression = second.expression;
    }
}

你怎么看?

传递给CompoundExpression的构造函数的Expression对象已经创建。 无需调用父构造函数(实际上,在那个地方这是非法的)。

更换

this.firstOperand = super(strFirst);
this.secondOperand = super(strSecond);

简单地通过

this.firstOperand = first;
this.secondOperand = second;

另一种可能性是在此构造函数中构造Expression对象。 然后,您无需传递Expression而只需传递字符串:

public CompoundExpression(String strFirst, String strSecond){
    this.firstOperand = new Expression(strFirst);
    this.secondOperand = new Expression(strSecond);
}

这都是您要实现的目标。

在这种情况下,您似乎既在封装又在扩展。 两者都是合理的做法,但这是两者的混搭。 您想要哪一个取决于您的特定用例,因此我将同时解释两者

扩展

这是亲子关系。 在这种情况下,孩子父母。 如猫黑社会。 这将实现如下

public abstract class CompoundExpression extends Expression {


    public CompoundExpression(String first){
        super(first);
    }

    //Other methods that make this class different from the parent class
}

封装

这是一种“有”的关系。 例如,一只猫“有一条”腿。 在这种情况下,您不需要使用“ extends”关键字

public abstract class CompoundExpression{

    Expression firstOperand, secondOperand;

    public CompoundExpression(Expression first, Expression second){

        this.firstOperand = first;
        this.secondOperand = second;
    }

    //other methods to use the encapsulated Expressions
}

扩展和封装相同的类

它有其用途,但远不及以上两个用途。 您需要非常小心,但是正如您提到的那样,您可以扩展一个类并封装一个或多个该类。 这个比喻可能是一个既FemaleCat 一只猫,可以包含猫(小猫的形式)。 这样做时,封装的类和扩展的类总是一样,这是“巧合”

如下

public abstract class CompoundExpression extends Expression{

    Expression firstOperand, secondOperand;

    public CompoundExpression(String myString, Expression first, Expression second){

        super(myString);
        this.firstOperand = first;
        this.secondOperand = second;
    }

    //other methods to make this class unique

}

就像我说的那样,我个人从没有做过,也不应该将其视为封装的“默认”方式。 在执行此操作之前,请确保您确实知道自己在做什么

结论

鉴于单个CompoundExpression包含2个单独的表达式,我怀疑您想要一个封装关系

您不能这样做:

this.firstOperand = super(strFirst);
this.secondOperand = super(strSecond);

您正在通过super调用构造函数(这是不可能的,因为super应该始终是第一个),并且您尝试直接使用抽象类的构造函数,这也是不可能的。

您需要做的是拥有一个不是抽象的类来实现“表达式”,然后直接调用它:

this.firstOperand = new ExpressionImpl(strFirst);
this.secondOperand = new ExpressionImpl(strSecond);

同样,在这种情况下,您无需在CompoundExpression类中扩展Expression。

祝好运!

我看到你有些困惑。 扩展是一回事。 我想你想要的是另一种。 这是扩展名:

public class Point2D {
    int x;
    int y;

    public Point2D(int x, int y) {
        this.x = x;
        this.y = y;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

}

Point3D类扩展了前一个

public class Point3D extends Point2D{

    int z;

    public Point3D(int z, int x, int y) {
        super(x, y);
        this.z = z;
    }

    public int getZ() {
        return z;
    }

    public void setZ(int z) {
        this.z = z;
    }

}

您只需要一个简单的POJO ,它正在使用您先前定义的Expression类。 只是封装您定义的类型而已。

public abstract class CompoundExpression{

    Expression firstOperand, secondOperand;

    public CompoundExpression(Expression first, Expression second){

        this.firstOperand = new Expression(strFirst);
        this.secondOperand = new Expression(strSecond);
    }
}

暂无
暂无

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

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