如何在Java中实现不同的类型
我有以下Python代码,我只是想知道是否有一种方法可以在Java中实现类似的功能:如何在Java中实现不同的类型,java,python,types,casting,Java,Python,Types,Casting,我有以下Python代码,我只是想知道是否有一种方法可以在Java中实现类似的功能: class TypeOperator(object): """An n-ary type constructor which builds a new type from old""" def __init__(self, name, types): self.name = name self.types = types def __str__(self):
class TypeOperator(object):
"""An n-ary type constructor which builds a new type from old"""
def __init__(self, name, types):
self.name = name
self.types = types
def __str__(self):
num_types = len(self.types)
if num_types == 0:
return self.name
elif num_types == 2:
return "({0} {1} {2})".format(str(self.types[0]), self.name, str(self.types[1]))
else:
return "{0} {1}" .format(self.name, ' '.join(self.types))
class Function(TypeOperator):
"""A binary type constructor which builds function types"""
def __init__(self, from_type, to_type):
super(Function, self).__init__("->", [from_type, to_type])
my_env = {"pair": Function(var1, Function(var2, pair_type)),
"true": Bool,
"cond": Function(Bool, Function(var3, Function(var3, var3))),
"zero": Function(Integer, Bool),
"pred": Function(Integer, Integer),
"times": Function(Integer, Function(Integer, Integer))}
我现在在Java中所做的是:
class TypeOperator extends TypeExp{
private String operator;
private TypeList types;
public TypeOperator(String operator, TypeList types){
this.operator = operator;
this.types = types;
}
public static TypeExp newTypeOperator(String operator, TypeList types) {
return new TypeOperator(operator, types);
}
}
class Function extends TypeOperator{
private static final String x = "->";
private TypeList listOfTypes;
public Function(String x, TypeList listOfTypes){
super(x, listOfTypes);
}
public static TypeExp newFunction(TypeList listOfTypes){
return new Function("->", listOfTypes);
}
}
public class TypeExp{
}
public class TypeList extends LinkedList<Object>{
}
class TypeOperator扩展了TypeExp{
私有字符串运算符;
私有类型列表类型;
公共类型运算符(字符串运算符、类型列表类型){
this.operator=操作员;
this.types=类型;
}
公共静态TypeExp newTypeOperator(字符串运算符、类型列表类型){
返回新的类型运算符(运算符,类型);
}
}
类函数扩展了类型运算符{
私有静态最终字符串x=“->”;
私有类型列表类型列表;
公共函数(字符串x,类型列表){
super(x,类型列表);
}
公共静态TypeExp newFunction(类型列表类型列表){
返回新函数(“->”,类型列表);
}
}
公共类typexp{
}
公共类类型列表扩展了LinkedList{
}
但是我找不到处理类函数的方法,因为它将TypeList作为第二个参数,但在python代码中,它可以将函数作为第二个参数,这就是为什么我觉得很难处理的原因。您遇到了什么错误?在java中,您不能将函数作为参数。然而,你可以有一些像。。。。打印(getInt());不兼容类型:TypeExp无法转换为TypeList当我将Java代码放在IDE中时,我没有得到那个错误。它建造得很好。尝试完全清理,然后从头开始构建完整的项目。可能是一些工件混淆了IDE语法检查器。