【问题标题】:How can I access information from a Node that is being passed into a method?如何从传递给方法的节点访问信息?
【发布时间】:2019-06-15 19:40:55
【问题描述】:

我有一个 Node(float coeff, int degree, Node next) 正在传递给一个方法。例如,如何访问在方法中传递的系数?

public Node(float coeff, int degree, Node next) {
        term = new Term(coeff, degree);
        this.next = next;
    }
public static Node add(Node poly1, Node poly2) {
        /** COMPLETE THIS METHOD **/
        // FOLLOWING LINE IS A PLACEHOLDER TO MAKE THIS METHOD COMPILE
        // CHANGE IT AS NEEDED FOR YOUR IMPLEMENTATION
        return null;
    }

例如,如果我想使用来自 poly1 的系数创建一个名为 poly1Coeff 的新浮点变量,我该怎么做?

会 浮动 poly1Coeff = poly1(coeff); 工作吗?

【问题讨论】:

  • "float poly1Coeff = poly1(coeff); 有效吗?" - 为什么不试试呢?

标签: java constructor nodes


【解决方案1】:

poly1(coef) 将调用一个名为 poly1 的方法,将参数 coeff 作为其单个参数传递。完全不是你想要的。

如果coeff是Term的公共变量,那么你想要的是double poly1Coeff = poly1.term.coeff; 如果 coeff 是私有或受保护的实例变量,则必须假设存在 getter:

double poly1Coeff = poly1.term.getCoeff();

【讨论】: