[英]For some reason my java class won't inherit a public void. Can somebody help me?
I'm pretty much new to Java development and I'm trying to build a simple android game, for this, I need to inherit some methods from other classes. 我对Java开发非常陌生,因此我试图构建一个简单的android游戏,为此,我需要从其他类继承一些方法。 Only, for some reason it won't. 只是出于某种原因,它不会。 Am I missing something here? 我在这里想念什么吗?
Here is code from the public Java class "Addition" 这是来自公共Java类“ Addition”的代码
public class Addition {
Random random = new Random();
int Answer;
int PartOne;
int PartTwo;
int FalseAnswer;
//CALL IN THIS FIRST!
public void Calculate() {
Answer = random.nextInt(999) + 2;
PartTwo = random.nextInt(Answer--) + 1;
PartOne = Answer - PartTwo;
FalseAnswer = random.nextInt(Math.round(Answer * 1.2f)) + Math.round(Answer * 0.8f);
}
//some more stuff. //一些更多的东西。
Here is the class subtraction, that I want to base upon Addition. 这是我要基于加法的类减法。 Here is the code that I have there: 这是我在那里的代码:
public class Subtraction{
Addition addition = new Addition();
addition.Calculate();
}
Don't mind the formatting, it is just to demonstrate. 不要介意格式,只是为了演示。 It is giving me an error, saying Calculate does not exist or cannot be found (neither can the other methods in the class). 它给我一个错误,说Calculate不存在或找不到(类中的其他方法也不能)。
I have also already tried inherting directly: 我也已经尝试过直接继承:
public class Subtraction extends Addition{
Calculate();
}
Didn't work either. 也没用。 The only thing it let's me do is this: 它让我做的唯一一件事是:
public class Subtraction extends Addition{
@Override
public void Calculate() {
super.Calculate();
}
}
Which is not what I want. 这不是我想要的。 I'm sure I'm missing something obvious, but can someone please point it out? 我确定我缺少明显的东西,但是有人可以指出吗? Thank you in advance. 先感谢您。
public class Subtraction extends Addition{
}
is all you need. 是你所需要的全部。 Don't write Calculate();
不要写Calculate();
in that class, you've already inherited it. 在该类中,您已经继承了它。
You get the error because you can't call methods like that outside of any class method. 之所以会出现错误,是因为您无法在任何类方法之外调用此类方法。 That' why this would work 这就是为什么这行得通
public class Subtraction extends Addition{
@Override
public void Calculate() {
super.Calculate(); // calls the 'calculate' method of the 'Addition' class
}
}
& this won't: &这不会:
public class Subtraction extends Addition{
Calculate(); // Outside of any method, invalid call
}
If you aren't trying to inherit from the Addition class using: 如果您不尝试使用以下方法从Addition类继承:
public class Subtraction extends Addition
{
@Override
public void Calculate()
{
super.Calculate(); // calls the 'calculate' method of the 'Addition' class
}
}
Then you need to put 那你需要放
Addition addition = new Addition();
addition.Calculate();
into a method that can be called. 成为可以调用的方法 Do it like this: 像这样做:
public class Subtraction
{
public void Calculate()
{
Addition addition = new Addition();
addition.Calculate();
}
}
You have to do it this way because the method calls have to be in an executable context (in a method or constructor). 您必须采用这种方式,因为方法调用必须在可执行上下文中(在方法或构造函数中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.