非静态变量不能从静态上下文引用

时间:2013-09-20 18:26:54

标签: java non-static

我已经浏览了这个网站一段时间,试图找到这个问题的答案,但答案总是太具体到特定的程序来帮助我。我遇到麻烦的代码是当我尝试使用ArithmeticSequence类中定义的构造函数实例化一个新的ArithmeticSequence对象时。但是,Netbeans告诉我这是一个从静态上下文引用的非静态变量。我尝试将类ArithmeticSequence更改为静态,但这也不起作用,因为出现了一个新错误,只允许我使用默认构造函数。我要做的是使用从文件mySequence读取的两个值将SEQ.in实例化为新的ArithmeticSequence。我收到了mySequence = new ArithmeticSequence(FT, SI);行的错误消息。无论如何,这是代码:

import java.io.*;
import java.util.Scanner;

public class SeqTest {

    public static void main(String[] args) {  
        Sequence mySequence;
        try (Scanner sc = new Scanner(new FileReader("SEQ.in"))) {
            if (sc.hasNext()) {
                for (int i = 0; i < 3; i++) {
                    String Line = sc.next();
                    Character type = Line.charAt(0);
                    double FT = sc.nextDouble();
                    double SI = sc.nextDouble();
                    switch (type) {
                        case 'A':
                            mySequence = new ArithmeticSequence(FT, SI);


                    }
                    System.out.println("The first 10 terms in the sequence are:");
                    System.out.println(mySequence.toString());
                    System.out.println("The sum of the first 5 terms = " + mySequence.getNthSum(5));
                }
            }
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        }
    }

    interface SequenceOps {

        public double getNthTerm(int n);

        public double getNthSum(int n);
    }

    abstract static class Sequence implements SequenceOps {

        private double firstTerm;

        public void Sequence() {
            firstTerm = 1;
        }

        public void Sequence(double j) {
            firstTerm = j;
        }

        public double getFirstTerm() {
            return firstTerm;
        }

        @Override
        public String toString() {
            String q = "";
            for (int i = 10; i >= 0; i--) {
                q += getNthTerm(i) + ", ";
            }
            return q;
        }
    }

    public class ArithmeticSequence extends Sequence {

        private double comDif;
        public void ArithmeticSequence() {
            Sequence(1);
            comDif = 0;
        }

        public void ArithmeticSequence(double j, double k) {
            Sequence(j);
            comDif = k;
        }

        @Override
        public double getNthTerm(int n) {
            return getFirstTerm() + (n - 1) * comDif;
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                double j = getNthTerm(i);
                q += j;
            }
            return q;
        }
    }

    public class GeometricSequence extends Sequence {

        private double ratio;

        public void GeometricSequence() {
            Sequence(1);
            ratio = 1;
        }

        public void GeometricSequence(double i, double k) {
            Sequence(i);
            ratio = k;
        }

        @Override
        public double getNthTerm(int n) {
            return getFirstTerm() * (Math.pow(ratio, (n - 1)));
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                q += getNthTerm(i);
            }
            return q;
        }
    }

    public class FibonacciSequence extends Sequence {

        private double secondTerm;

        public void FibonacciSequence() {
            Sequence(1);
            secondTerm = 1;
        }

        public void FibonacciSequence(double j, double i) {
            Sequence(j);
            secondTerm = i;
        }

        @Override
        public double getNthTerm(int n) {
            double q = 1.6180339887;
            return secondTerm * Math.pow(q, n) + getFirstTerm() * Math.pow(q, n);
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                q += getNthTerm(i);
            }
            return q;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您不应在构造函数中使用返回类型,如果使用内部类,则必须在内部类之前实例化外部类。

public class SeqTest {

    public static void main(String[] args) {
        Sequence mySequence = null;
        try (Scanner sc = new Scanner(new FileReader("SEQ.in"))) {
            if (sc.hasNext()) {
                for (int i = 0; i < 3; i++) {
                    String Line = sc.next();
                    Character type = Line.charAt(0);
                    double FT = sc.nextDouble();
                    double SI = sc.nextDouble();
                    switch (type) {
                        case 'A':
                            mySequence = new SeqTest().new ArithmeticSequence(FT, SI);
                    }
                    System.out.println("The first 10 terms in the sequence are:");
                    System.out.println(mySequence.toString());
                    System.out.println("The sum of the first 5 terms = " + mySequence.getNthSum(5));
                }
            }
        } catch (FileNotFoundException fnfe) {
            System.out.println(fnfe);
        }
    }

    interface SequenceOps {

        public double getNthTerm(int n);

        public double getNthSum(int n);
    }

    abstract static class Sequence implements SequenceOps {

        private double firstTerm;

        public void Sequence() {
            firstTerm = 1;
        }

        public void Sequence(double j) {
            firstTerm = j;
        }

        public double getFirstTerm() {
            return firstTerm;
        }

        @Override
        public String toString() {
            String q = "";
            for (int i = 10; i >= 0; i--) {
                q += getNthTerm(i) + ", ";
            }
            return q;
        }
    }

    public class ArithmeticSequence extends Sequence {

        private double comDif;

        public ArithmeticSequence() {
            Sequence(1);
            comDif = 0;
        }

        public ArithmeticSequence(double j, double k) {
            Sequence(j);
            comDif = k;
        }

        @Override
        public double getNthTerm(int n) {
            return getFirstTerm() + (n - 1) * comDif;
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                double j = getNthTerm(i);
                q += j;
            }
            return q;
        }
    }

    public class GeometricSequence extends Sequence {

        private double ratio;

        public GeometricSequence() {
            Sequence(1);
            ratio = 1;
        }

        public GeometricSequence(double i, double k) {
            Sequence(i);
            ratio = k;
        }

        @Override
        public double getNthTerm(int n) {
            return getFirstTerm() * (Math.pow(ratio, (n - 1)));
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                q += getNthTerm(i);
            }
            return q;
        }
    }

    public class FibonacciSequence extends Sequence {

        private double secondTerm;

        public FibonacciSequence() {
            Sequence(1);
            secondTerm = 1;
        }

        public FibonacciSequence(double j, double i) {
            Sequence(j);
            secondTerm = i;
        }

        @Override
        public double getNthTerm(int n) {
            double q = 1.6180339887;
            return secondTerm * Math.pow(q, n) + getFirstTerm() * Math.pow(q, n);
        }

        @Override
        public double getNthSum(int n) {
            double q = 0;
            for (int i = n; i >= 0; i--) {
                q += getNthTerm(i);
            }
            return q;
        }
    }
}