簡體   English   中英

從另一個類的方法中調用mutator

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

我試圖了解我在這里做錯了什么

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;
        }
    }

顯然,它給了我一個錯誤:public static Roots findRoots 的最后一行找不到符號,但我不明白還有什么其他方法可以調用 mutator

更換有什么問題

Roots(x1, x2);

return new Roots(x1, x2);

?

另外,我不知道您對“mutator”的理解是什么,但是您可能想在 Java 初學者指南中查找的關鍵字是“constructor”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM