【发布时间】:2016-07-04 09:11:34
【问题描述】:
我在 Modula-2 中有这段代码,
PROCEDURE Prune(typeExp: TypeExp): TypeExp;
BEGIN
CASE typeExp.^class OF
| VarType:
IF typeExp^.instance = NIL THEN
RETURN typeExp;
ELSE
typeExp^.instance = Prune(typeExp^.instance);
RETURN typeExp^.instance;
END;
| OperType: RETURN typeExp;
END;
END Prune;
当我尝试将此代码转换为 java 时,我遇到了几个问题。我可以创建一个实例并判断它的实例是否为空,然后选择返回什么。但我真的不知道如何处理案例 2,即实例可能是一个新的 Opentype();因为在这种情况下只能返回一个值。
public TypeExp Prune(TypeExp typeExp){
TypeExp r = new VarType();
if (r.instance == null) {
return r;
}
else {
r.instance = Prune(r.instance);
return r.instance;
}
}
第二个问题是我不认为我可以在内部调用函数 Prune(),那我该怎么办?提前致谢。
【问题讨论】:
标签: java function types instance