简体   繁体   English

为什么我们不能在类中覆盖

[英]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.

相关问题 为什么我们不能用私有扩展类方法覆盖基类方法? - Why can’t we override a base class method with private extended class method? 为什么我们不能在子 class 中覆盖超级 class 的私有成员? - Why can we not override private members of a super class in a sub class? 为什么我们不能在 java 的子 class 的方法之外访问 class 内部的父实例变量 class? - why can't we access parent class instance variables inside the class outside the method in child class in java? 为什么我们不能在泛型类中创建一个“Concrete”类数组? - Why can't we create an array of “Concrete” class inside a generic class? 为什么我们不能在Spring中将抽象类自动连接到具体类中? - Why can't we autowire an abstract class inside a concrete class in Spring? 为什么我们不能在C ++中的同一个类中声明一个类的对象但是在Java中是允许的? - why can't we declare object of a class inside the same class in C++ but it is allowed in Java? 为什么我们不能嘲笑最后一堂课? - Why can't we mock a final class? 为什么我们不能在方法中声明私有本地内部类? - Why can't we declare private local inner class inside a method? 为什么 Hybris 提供不安全的 http maven 存储库 URL? 为什么我们不能覆盖它? - Why does Hybris ship insecure http maven repository URL? And why can't we override it? 为什么我们不能重写方法并定义它以返回原始方法的超类? - why we can't override a method and define it to return a superclass of the original method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM