java:从嵌套类声明数组

时间:2016-01-22 01:22:52

标签: java arrays

我对Java很新。假设我有以下内容:

InnerClass[] innerClassArray = new InnerClass[size] 

如果它嵌套在OuterClass中,我如何在main中声明一个类型为InnerClass的数组。这不起作用。

from sympy import *
import sympy.mpmath as const

OUT_OF_BOUNDS = "Integer out of bounds."
INVALID_INTEGER = "Invalid Integer."
INVALID_FLOAT = "Invalid Float."
CANT_SOLVE_VARIABLES = "Unable to Solve for More than One Variable."
CANT_SOLVE_DONE = "Already Solved. Nothing to do."

# time value of money equation: FV = PV(1 + i)**n
# FV = future value
# PV = present value
# i = growth rate per perioid
# n = number of periods
FV, PV, i, n = symbols('FV PV i n')
time_value_money_discrete = Eq(FV, PV*(1+i)**n)
time_value_money_continuous = Eq(FV, PV*const.e**(i*n))

def get_sym_num(prompt, fail_prompt):
    while(True):
        try:
            s = input(prompt)
            if s == "":
                return None
            f = sympify(s)
            return f
        except:
            print(fail_prompt)
            continue

equations_supported = [['Time Value of Money (discrete)', [FV, PV, i, n], time_value_money_discrete], 
                       ['Time Value of Money (continuous)',[FV, PV, i, n], time_value_money_continuous]]
EQUATION_NAME = 0
EQUATION_PARAMS = 1
EQUATION_EXPR = 2

if __name__ == "__main__":
    while(True):
        print()
        for i, v in enumerate(equations_supported):
            print("{}: {}".format(i, v[EQUATION_NAME]))
        try:
            process = input("What equation do you want to solve?  ")
            if process == "" or process == "exit":
                break
            process = int(process)
        except:
            print(INVALID_INTEGER)
            continue
        if process < 0 or process >= len(equations_supported):
            print(OUT_OF_BOUNDS)
            continue
        params = [None]*len(equations_supported[process][EQUATION_PARAMS])
        for i, p in enumerate(equations_supported[process][EQUATION_PARAMS]):
            params[i] = get_sym_num("What is {}? ".format(p), INVALID_FLOAT)
        if params.count(None) > 1:
            print(CANT_SOLVE_VARIABLES)
            continue
        if params.count(None) == 0:
            print(CANT_SOLVE_DONE)
            continue
        curr_expr = equations_supported[process][EQUATION_EXPR]
        for i, p in enumerate(params):
            if p != None:
                curr_expr = curr_expr.subs(equations_supported[process][EQUATION_PARAMS][i], params[i])
        print(solve(curr_expr,  equations_supported[process][EQUATION_PARAMS][params.index(None)]))

谢谢。

1 个答案:

答案 0 :(得分:0)

除了作为OuterClass实例的一部分之外,InnerClass的实例不能存在,并且InnerClass的类定义与OuterClass的类定义相关联。

更正确的是:

Outerclass.InnerClass[] innerClassArray = new Outerclass.InnerClass[size]; 

并填充它:

OuterClass outer = new OuterClass();
innerClassArray[0] = outer.new InnerClass();
innerClassArray[1] = outer.new InnerClass();