【发布时间】:2019-12-03 23:27:27
【问题描述】:
我在超类中创建了一个布尔方法,但是当我调用该方法在子类中使用它时,它给了我一个类型不匹配的错误,说它不能将布尔值转换为 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
}
【问题讨论】:
-
“错误说它不能将布尔值转换为 A”。我认为它抱怨它无法将其转换为 BalancedExpressions,但是,嘿,你能发布整个错误吗?
-
- 类型不匹配:无法从 BalancedExpressions 转换为布尔值 - 类型不匹配:无法从布尔值转换为
标签: java inheritance methods