[英]why can't we override inside a class
I am writing a code of game and I am using KeyAdapter class for keyboard Inputs but I am encountring a problem. 我正在编写游戏代码,并且将KeyAdapter类用于键盘输入,但是遇到问题。 I am extending and using function of KeyAdapter class as follows 我正在扩展和使用KeyAdapter类的功能,如下所示
class Kadapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
//here i will write required code
}
}
When i am writing it outside all class then I am not getting any error in overriding the function key pressed but now suppose if I define it inside any other class as shown here 当我在所有类之外编写它时,在覆盖按下的功能键时不会出现任何错误,但是现在假设是否在其他任何类中定义它,如下所示
class Arena extends JPanel {
Player pl;
Arena() {
pl = new Player();
}
class Kadapter extends KeyAdapter {
@Override
public void KeyPressed(KeyEvent e) {
}
}
}
Then I am getting error in overriding the function keyPressed. 然后在覆盖功能keyPressed时出现错误。 can anyone tell me why. 谁能告诉我为什么。 I wanted to define it inside the arena class because i will require the same instance of player class in Kadapter class. 我想在arena类中定义它,因为在Kadapter类中我需要播放器类的相同实例。 well i could pass that instance through functions. 好吧,我可以通过功能通过该实例。 but my question here is that why can't we override the same function inside arena class.I will find any alternate solution about player problem that's not a problem but please tell me about this Override problem. 但是我的问题是为什么我们不能在arena类中覆盖相同的函数,我会找到关于玩家问题的其他解决方案,但这不是问题,但请告诉我这个Override问题。 I am very curious about it. 我对此很好奇。
You can define and implement inside the nested class. 您可以在嵌套类中定义和实现。 There should not be any issue. 应该没有任何问题。
So far I can see only issue is with method name as KeyPressed . 到目前为止,我只能看到方法名称为KeyPressed的问题 。
class Arena extends JPanel {
Player pl;
Arena() {
pl = new Player();
}
class Kadapter extends KeyAdapter {
@Override
public void KeyPressed(KeyEvent e) {
}
}
}
It should be keyPressed 应该是keyPressed
k as lower case! k小写!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.