简体   繁体   English

删除未实现的方法

[英]Removing non-implemented methods

When I implement an interface Eclipse tells me to add all implemented methods. 当我实现一个接口时,Eclipse告诉我添加所有实现的方法。

Since not all of them are usually needed, is there a way to delete methods inherited from the interface instead of having an empty body? 由于并非通常都需要它们,是否有办法删除从接口继承的方法而不是拥有空的正文? It bothers me a bit to have this useless code floating around. 让这些无用的代码四处浮动让我有些困扰。

A good example for this is the KeyListener interface. 一个很好的例子是KeyListener接口。

jTextComponent.addKeyListener(new KeyListener()
{
    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }

    @Override
    public void keyReleased(KeyEvent arg0)
    {
    }
});

I want to write the following or similar: 我要编写以下内容或类似内容:

jTextComponent.addKeyListener(new KeyListener()
{
    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }
});

Is there a way to say that all non-implemented methods are automatically empty or something instead of putting them into the code? 有没有办法说所有未实现的方法会自动为空或某种形式,而不是将其放入代码中?

There is not a way to do this with any interface. 无法通过任何接口执行此操作。

However, in this particular case (with KeyListener ) there is a class KeyAdapter which implements all the methods with empty bodies, so that you can do this: 但是,在这种特殊情况下(使用KeyListener ),存在一个KeyAdapter类, KeyAdapter使用空主体实现所有方法,因此您可以执行以下操作:

jTextComponent.addKeyListener(new KeyAdapter()
{
    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
         System.out.println("Pressed!");
    }
});

If you're curious, KeyAdapter is basically this: 如果您很好奇, KeyAdapter基本上是这样的:

public class KeyAdapter implements KeyListener
{
    @Override
    public void keyTyped(KeyEvent e)
    {
    }

    @Override
    public void keyPressed(KeyEvent pressedEvent)
    {
    }

    @Override
    public void keyReleased(KeyEvent arg0)
    {
    }
}

If you implement an interface , you have to fulfill the contract by implementing all of its methods. 如果实现接口,则必须通过实现其所有方法来履行合同。 If you do not want to implement methods then you can leave the method body blank, but still you will have to inlclude the methods in your code. 如果您不想实现方法,则可以将方法主体保留为空白,但仍然必须将这些方法包含在代码中。

Is there a way to say that all non-implemented methods are automatically empty or something instead of putting them into the code? 有没有办法说所有未实现的方法会自动为空或某种形式,而不是将其放入代码中?

No, there isn't , atleast at implementation class level. 不,在实现类级别上至少没有。

But from java 8 onwards you can define default methods in interface, which also requires making changes in interface itself rather than implementing class. 但是从Java 8开始,您可以在接口中定义默认方法,这也需要对接口本身进行更改而不是实现类。 These methods provide flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method. 这些方法提供了允许接口定义实现的灵活性,该接口定义实现将在具体类无法为该方法提供实现的情况下默认使用。

No. You could use an abstract class between your interface and concrete class and add unused methods there. 不可以。您可以在接口和具体类之间使用抽象类,并在其中添加未使用的方法 But Interface defines a contract, the set of basic properties / methods which a concrete class should have to be called as the type of the interface . 但接口定义了一个合同,该组的哪一个具体的类应该有被称为接口的类型的基本属性/方法。

In eclipse, you click on the compiler error and click add unimplemented methods to add all the unimplemented methods in the class which implements the interface . 在eclipse中,单击编译器错误,然后单击添加未实现的方法,以将所有未实现的方法添加到实现 接口的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM