有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java布尔值不继承

我在一个超类中创建了一个布尔方法,但当我调用该方法在子类中使用它时,它会给我一个类型不匹配错误,表示它无法将布尔值转换为a。我可能缺少一些次要的内容,但有没有其他方法在继承类中使用该方法

public class A{
    public boolean Proper(String expression){
    //body of method that determines if an expression is proper
    return true;
    }
}



public class B extends A {

    static BalancedExpressions Proper = new BalancedExpressions();

    public static void main(String[] args) {
                try {
                Scanner scanner = new Scanner(new File("src/Expressions.txt"));
                FileWriter output = new FileWriter(new File("Output.txt"));

                while (scanner.hasNextLine()) {
                    String nextLine = scanner.nextLine();

                    output.write(nextLine + "\t");

                    //following line is giving the error
                    if(Proper = true) {
                    output.write("proper" + "\n");
                    } else { 
                    output.write("improper" + "\n");
                }
                scanner.close();
                output.close();
                    }
            } 
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }


        }//end main

    }

共 (2) 个答案

  1. # 1 楼答案

    你必须写Proper("some string"),而不是Proper。如果你用偏执狂写它,它是一个函数调用;如果您编写它时没有使用,那么它就是您的BalancedExpressions变量。您还需要使用双等号==。当前使用的行尝试将true赋值给Proper成员变量。另外,您不需要写if (something == true),只需写if (something)

  2. # 2 楼答案

    好的,是的,你刚刚有一个打字错误,哈哈

                    //Proper is already a boolean, so this would work perfectly,
    
                    if(Proper(nextLine)) {
                    output.write("proper" + "\n");
                    } else { 
                    output.write("improper" + "\n");
    

    您将property设置为true,而没有进行方法调用