【问题标题】:How can I return the right data type of a instance when this instance might have different data type?当该实例可能具有不同的数据类型时,如何返回该实例的正确数据类型?
【发布时间】: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


    【解决方案1】:

    我不太了解 Modula-2,但可能是这样的:

    public TypeExp Prune(TypeExp typeExp) {
        if (typeExp instanceof VarType) {
            if (typeExp.instance == null) {
                return typeExp;
            }
            else {
                typeExp.instance = Prune(typeExp.instance);
                return typeExp.instance;
            }
        } else if (typeExp instanceof OperType) {
            return typeExp;
        }
        //if typeExp is not an instance of VarType or OperType
        return null;
    }
    

    Modula 代码并非在所有代码路径中都返回。这在 Java 中是不可能的。在这些情况下,我插入了 return null 。不过,这对您的应用程序来说可能是错误的。

    【讨论】:

    • 对不起,我忘记在原始 Modula 代码中添加 return。能问一下为什么第7行有一个新的之前的Prune吗?
    • 哦,对不起,我以为这是一个新对象。但它是一个递归调用。我在答案中更改了它。
    【解决方案2】:

    以下示例与您的功能不同,但我认为您可以根据需要进行修改。它将您的返回类型隐藏在 Type 1234562 后面 => 您可以返回两个类的对象。

    主要

    package com.type;
    
    public class Main {
        public static void main(String[] args) {
            Type first = new FirstType();
            Type second = new SecondType();
    
            System.out.println(func(first).getTypeName());
            System.out.println(func(first).getTypeName());
            System.out.println(func(second).getTypeName());
        }
    
        public static Type func(Type type) {
            if(type instanceof FirstType) {
                type.setTypeName("First");
            } else {
                type.setTypeName("Second");
                // something here
            }
            return type;
        }
    }
    

    类型

    package com.type;
    
    public class Type {
        private String typeName;
    
        public Type() {}
    
        public String getTypeName() {
            return typeName;
        }
    
        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }
    }
    

    第一类

    package com.type;
    
    public class FirstType extends Type {
    }
    

    第二类

    package com.type;
    
    public class SecondType extends Type {
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 1970-01-01
      • 2020-03-21
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 2015-05-31
      • 2021-08-13
      相关资源
      最近更新 更多