Java 从超类获取返回整数值

Java 从超类获取返回整数值,java,return-value,Java,Return Value,这是我第一节课上的代码: public class Factorial { public void Sequence(){ String value = JOptionPane.showInputDialog("Please enter the number of which you want your shit."); System.out.println("Factorial:"); System.out.println(fact(Integer.valueOf(va

这是我第一节课上的代码:

public class Factorial {
public void Sequence(){
    String value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
这是阶乘的一个子类:

public class Fibonacci extends Factorial {
    public void Sequence(){
        Factorial get = new Factorial();
                //not working EDIT: Just realized this obv wont work, but still need help to get it to work
        get.getValues(n);
但是,我无法从Factorial类中获取getValues中返回的值,这里(下面)
Value
用于存储用户输入

public class Factorial {
public String value;
public void sequence(){
    value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
public String getValue(){return value;}
}

public class Fibonacci extends Factorial {
    //Method sequence is overridden here
    public void sequence(){
//can use getValue() here or anywhere...there is no need to use a separate object due to inheritance
}
}
此处(下方)
用于存储用户输入

public class Factorial {
public String value;
public void sequence(){
    value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
public String getValue(){return value;}
}

public class Fibonacci extends Factorial {
    //Method sequence is overridden here
    public void sequence(){
//can use getValue() here or anywhere...there is no need to use a separate object due to inheritance
}
}
此处(下方)
用于存储用户输入

public class Factorial {
public String value;
public void sequence(){
    value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
public String getValue(){return value;}
}

public class Fibonacci extends Factorial {
    //Method sequence is overridden here
    public void sequence(){
//can use getValue() here or anywhere...there is no need to use a separate object due to inheritance
}
}
此处(下方)
用于存储用户输入

public class Factorial {
public String value;
public void sequence(){
    value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
public String getValue(){return value;}
}

public class Fibonacci extends Factorial {
    //Method sequence is overridden here
    public void sequence(){
//can use getValue() here or anywhere...there is no need to use a separate object due to inheritance
}
}

多态性在这里并不是一个很糟糕的抽象概念,但实际上并没有一种方法可以“帮助您修复您正在做的事情”。有很多明显的原因,多态性不能像您尝试使用它的方式那样工作。正如我提到的,其中之一是静态方法不能被重写,因此您的
getValue
只能返回输入数字。另一个原因是,您正在从子类中创建一个超类的实例,并且没有必要这样做。您还可以继续对所述超类实例调用静态方法,这也是不必要的

因此,简短的一点是,当您来到这里询问与变量
n
相关的编译器错误时,您的代码表明您远远领先于您自己。如果你想学习多态性,你应该。你在评论中所说的--“希望在多个类中使用n(用户输入)的值”--你在OP中的代码不会帮助你实现这一点,因为这是一个根本错误的多态设计。我不是说这是对你的侮辱,你应该重新开始

以下是一个更典型的设计,可作为指导:

public class Main {
    public static void main(String[] args) {
        String input = JOptionPane.showInputDialog("Enter a number:");
        int n = Integer.parseInt(input);

        System.out.println(new Factorial().getResult(n));
    }
}

public interface Sequence {
    public int getResult(int n);
}

public class Factorial
implements Sequence {
    @Override
    public int getResult(int n) {
        int result = n;
        while(n > 1) {
            n--;
            result *= n;
        }

        return result;
    }
}
斐波那契在这里扩展阶乘也不是真正的面向对象的原因

如果您真的想在Sequence类中包含检索输入的功能,您可以这样做:

public abstract class Sequence {
    private static int getInput() { // static because it relies on no state
        return Integer.parseInt(
            JOptionPane.showInputDialog("Enter a number:")
        );
    }

    public final int getResultFromInput() { // simply calls getInput and getResult
        return getResult(getInput());       // sub classes will not need to do
    }                                       // anything with this method

    public abstract int getResult(int n);   // sub classes override this method
}                                           // to provide their functionality

public class Factorial
extends Sequence {
    @Override
    public int getResult(int n) {

        // same as above factorial

    }
}
然后你可以这样做:

System.out.println(new Factorial().getResultFromInput());
我想说的是,数字序列对象执行诸如显示对话框之类的操作并不是真正正确的OOP,但没有理由不能这样做


另外,作为一个次要的补充,您应该使用long而不是int。阶乘将很快导致int溢出(在12之后!)。不确定什么时候斐波那契序列会溢出,当然不会像阶乘那样快。

多态性在这里并不是一个很好的抽象,但也没有真正的方法“帮助您修复所做的事情”。有很多明显的原因,多态性不能像您尝试使用它的方式那样工作。正如我提到的,其中之一是静态方法不能被重写,因此您的
getValue
只能返回输入数字。另一个原因是,您正在从子类中创建一个超类的实例,并且没有必要这样做。您还可以继续对所述超类实例调用静态方法,这也是不必要的

因此,简短的一点是,当您来到这里询问与变量
n
相关的编译器错误时,您的代码表明您远远领先于您自己。如果你想学习多态性,你应该。你在评论中所说的--“希望在多个类中使用n(用户输入)的值”--你在OP中的代码不会帮助你实现这一点,因为这是一个根本错误的多态设计。我不是说这是对你的侮辱,你应该重新开始

以下是一个更典型的设计,可作为指导:

public class Main {
    public static void main(String[] args) {
        String input = JOptionPane.showInputDialog("Enter a number:");
        int n = Integer.parseInt(input);

        System.out.println(new Factorial().getResult(n));
    }
}

public interface Sequence {
    public int getResult(int n);
}

public class Factorial
implements Sequence {
    @Override
    public int getResult(int n) {
        int result = n;
        while(n > 1) {
            n--;
            result *= n;
        }

        return result;
    }
}
斐波那契在这里扩展阶乘也不是真正的面向对象的原因

如果您真的想在Sequence类中包含检索输入的功能,您可以这样做:

public abstract class Sequence {
    private static int getInput() { // static because it relies on no state
        return Integer.parseInt(
            JOptionPane.showInputDialog("Enter a number:")
        );
    }

    public final int getResultFromInput() { // simply calls getInput and getResult
        return getResult(getInput());       // sub classes will not need to do
    }                                       // anything with this method

    public abstract int getResult(int n);   // sub classes override this method
}                                           // to provide their functionality

public class Factorial
extends Sequence {
    @Override
    public int getResult(int n) {

        // same as above factorial

    }
}
然后你可以这样做:

System.out.println(new Factorial().getResultFromInput());
我想说的是,数字序列对象执行诸如显示对话框之类的操作并不是真正正确的OOP,但没有理由不能这样做


另外,作为一个次要的补充,您应该使用long而不是int。阶乘将很快导致int溢出(在12之后!)。不确定什么时候斐波那契序列会溢出,当然不会像阶乘那样快。

多态性在这里并不是一个很好的抽象,但也没有真正的方法“帮助您修复所做的事情”。有很多明显的原因,多态性不能像您尝试使用它的方式那样工作。正如我提到的,其中之一是静态方法不能被重写,因此您的
getValue
只能返回输入数字。另一个原因是,您正在从子类中创建一个超类的实例,并且没有必要这样做。您还可以继续对所述超类实例调用静态方法,这也是不必要的

因此,简短的一点是,当您来到这里询问与变量
n
相关的编译器错误时,您的代码表明您远远领先于您自己。如果你想学习多态性,你应该。你在评论中所说的--“希望在多个类中使用n(用户输入)的值”--你在OP中的代码不会帮助你实现这一点,因为这是一个根本错误的多态设计。我不是说这是对你的侮辱,你应该重新开始

以下是一个更典型的设计,可作为指导:

public class Main {
    public static void main(String[] args) {
        String input = JOptionPane.showInputDialog("Enter a number:");
        int n = Integer.parseInt(input);

        System.out.println(new Factorial().getResult(n));
    }
}

public interface Sequence {
    public int getResult(int n);
}

public class Factorial
implements Sequence {
    @Override
    public int getResult(int n) {
        int result = n;
        while(n > 1) {
            n--;
            result *= n;
        }

        return result;
    }
}
斐波那契在这里扩展阶乘也不是真正的面向对象的原因

如果您真的想在Sequence类中包含检索输入的功能,您可以这样做:

public abstract class Sequence {
    private static int getInput() { // static because it relies on no state
        return Integer.parseInt(
            JOptionPane.showInputDialog("Enter a number:")
        );
    }

    public final int getResultFromInput() { // simply calls getInput and getResult
        return getResult(getInput());       // sub classes will not need to do
    }                                       // anything with this method

    public abstract int getResult(int n);   // sub classes override this method
}                                           // to provide their functionality

public class Factorial
extends Sequence {
    @Override
    public int getResult(int n) {

        // same as above factorial

    }
}
然后你可以这样做:

System.out.println(new Factorial().getResultFromInput());
我想说的是,数字序列对象执行诸如显示对话框之类的操作并不是真正正确的OOP,但没有理由不能这样做

另外,作为一个次要的补充,您应该使用long而不是int。阶乘将很快导致int溢出(在12之后!)。不确定什么时候斐波那契序列会溢出,当然不会像阶乘那样快。

Poly