简体   繁体   English

从另一个类的方法中调用mutator

[英]calling mutator from within a method of another class

Im trying understand what am I doing wrong here我试图了解我在这里做错了什么

import java.lang.Math

public class QuadraticEquation {
        public static Roots findRoots(double a, double b, double c) {
            double d = b*b-4*a*c;
            double x1 = (-b + Math.sqrt(d))/2*a;
            double x2 = (-b - Math.sqrt(d))/2*a;
            Roots( x1, x2);

        }

        public static void main(String[] args) {
            Roots roots = QuadraticEquation.findRoots(2, 10, 8);
            System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
        }
    }

    class Roots {
        public final double x1, x2;

        public Roots(double x1, double x2) {         
            this.x1 = x1;
            this.x2 = x2;
        }
    }

Obviously it gives me a error: cannot find symbol on last line of public static Roots findRoots but I don't understand what other way of calling the mutator is there显然,它给了我一个错误:public static Roots findRoots 的最后一行找不到符号,但我不明白还有什么其他方法可以调用 mutator

What's wrong with replacing更换有什么问题

Roots(x1, x2);

with

return new Roots(x1, x2);

? ?

Also, I don't know what your understanding of a "mutator" is, but the keyword you might want to look up in your Java beginners guide is "constructor".另外,我不知道您对“mutator”的理解是什么,但是您可能想在 Java 初学者指南中查找的关键字是“constructor”。

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

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