java 如何在接口中获得非抽象方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4150706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:02:37  来源:igfitidea点击:

How do you get a non-abstract method in an interface?

javaoopclassinterfacemethods

提问by Ben

How does one make a method in an interface that's not abstract? I know that it CAN be done, I just don't know how.

如何在非抽象的接口中创建方法?我知道可以做到,只是不知道怎么做。

I'll give an example of my confusion. In Java you can implement java.awt.event.MouseListener, then you must call the method addMouseListener(Object)... passing your class as a parameter, so the MouseListener knows what object to cast from. How is the addMouseListener(Object) method possible?

我举一个例子来说明我的困惑。在 Java 中,您可以实现 java.awt.event.MouseListener,然后您必须调用方法 addMouseListener(Object)... 将您的类作为参数传递,因此 MouseListener 知道要从哪个对象进行转换。addMouseListener(Object) 方法是如何实现的?

Someone said I was confusing the way anonymous classes work with interfaces having non-abstract methods. How would you implement an anonymous class within a interface so the implementer can call its methods? I'm very new and still a 'noob' at OOP.

有人说我混淆了匿名类与具有非抽象方法的接口的工作方式。您将如何在接口中实现匿名类,以便实现者可以调用其方法?我很新,仍然是 OOP 的“菜鸟”。

回答by mgv

Interface methods are by definition public and abstract, so you cannot have non-abstract methods in your interface.

接口方法根据定义是公共和抽象的,因此您的接口中不能有非抽象方法。

回答by Todd

If you're interested in anonymous inner classes, they work as shown below.

如果您对匿名内部类感兴趣,它们的工作方式如下所示。

We'll continue with the MouseListener example.

我们将继续使用 MouseListener 示例。

The interface for java.awt.event.MouseListenerlooks something like this:

的界面java.awt.event.MouseListener看起来像这样:

public interface  MouseListener {
  void mouseClicked(MouseEvent e);
  void mouseEntered(MouseEvent e);
  void mouseExited(MouseEvent e);
  void mousePressed(MouseEvent e);
  void mouseReleased(MouseEvent e);
}

Somewhere in your app you may want to respond to mouse events, so using an anonymous inner class you could do something like this.

在你的应用程序的某个地方你可能想要响应鼠标事件,所以使用匿名内部类你可以做这样的事情。

component.addMouseListener(new MouseListener() {
  public void mouseClicked(MouseEvent e){/*implementation goes here...*/}
  public void mouseEntered(MouseEvent e){/*implementation goes here...*/}
  public void mouseExited(MouseEvent e){/*implementation goes here...*/}
  public void mousePressed(MouseEvent e){/*implementation goes here...*/}
  public void mouseReleased(MouseEvent e){/*implementation goes here...*/}
});

What you've done here is created a new class (without name, hence anonymous) that implements the MouseListener interface. You have not, as suggested above, created a non-abstract method on an interface.

您在这里所做的是创建了一个实现 MouseListener 接口的新类(没有名称,因此是匿名的)。如上所述,您还没有在接口上创建非抽象方法。

You could have also just created a new named class ("named class" means regular old class):

您还可以创建一个新的命名类(“命名类”表示常规的旧类):

class MyMouseListener implements MouseListener {
  public void mouseClicked(MouseEvent e){/*implementation goes here...*/}
  public void mouseEntered(MouseEvent e){/*implementation goes here...*/}
  public void mouseExited(MouseEvent e){/*implementation goes here...*/}
  public void mousePressed(MouseEvent e){/*implementation goes here...*/}
  public void mouseReleased(MouseEvent e){/*implementation goes here...*/}
}

Then somewhere else you would do component.addMouseListener(new MyMouseListener());

然后在其他地方你会做 component.addMouseListener(new MyMouseListener());

See the difference?

看到不同?

I hope this helps. Good luck.

我希望这有帮助。祝你好运。

P.S. - Read up on inheritance, interfaces, inner classes and anonymous inner classes in Java for a deeper understanding.

PS - 阅读 Java 中的继承、接口、内部类和匿名内部类以获得更深入的理解。

回答by Marko Koji?

In Java, interface methods are public and abstract by default.

在 Java 中,接口方法默认是公共的和抽象的。

For example:

例如:

public interface IPrint{
    public abstract void print();
}

And that is same as:

这与:

public interface IPrint{
    void print();
}

So first option is bad practice. Point is that you can'tuse non-abstract methods inside of interface, because they are abstract by default. But in abstract class you can use non-abstract or abstract methods.

所以第一个选择是不好的做法。重点是你不能在接口内部使用非抽象方法,因为它们默认是抽象的。但是在抽象类中,您可以使用非抽象或抽象方法。

回答by Manoj Lakshan

After Java 8, you can define non-abstract methods inside an interface using "default" keyword as fallows.

在 Java 8 之后,您可以使用“default”关键字作为空闲时间在接口内定义非抽象方法。

public interface MyIntface {

    void abstract_method(int a, String b);

    default void nonabstract_method(){
        System.out.println("do something");
    }
}

回答by Matt McHenry

No, it can't be done in Java 6 or 7. It will become possible, in a round-about way, as part of project lambda, which is slated for Java 8. The proposed mechanism is called extension methods.

不,它不能在 Java 6 或 7 中完成。它会以一种迂回的方式成为可能,作为计划用于 Java 8 的lambda 项目的一部分。建议的机制称为扩展方法。

回答by Miguel Silva

In Java you just can't. Perhaps what you are thinking about is an abstract class.

在 Java 中你不能。也许您正在考虑的是一个抽象类。

An abstract class in Java may implement interfaces and define some method signatures while keeping other methods abstract with the "abstract" keyword -- Wikipedia

Java 中的抽象类可以实现接口并定义一些方法签名,同时使用“抽象”关键字使其他方法保持抽象--维基百科

So, like in a regular class, you can provide implementation to some concrete methods. And, like in an interface, you can declare the signature of abstract methods. Theses methods will be implemented by the concrete classes that extend the abstract one. Like an interface an abstract class cannot be instantiated.

因此,就像在常规类中一样,您可以提供一些具体方法的实现。而且,就像在接口中一样,您可以声明抽象方法的签名。这些方法将由扩展抽象类的具体类实现。像接口一样,抽象类不能被实例化。

The main practical difference is that concrete classes are created by sub-classing the abstract one. So one concrete class can only extend one abstract class while it could implement many interfaces.

主要的实际区别是具体类是通过对抽象类进行子类化来创建的。所以一个具体类只能扩展一个抽象类,而它可以实现很多接口。

回答by vanza

You're confusing "non-abstract method in interface" with "class implementing an interface". Your question:

您将“接口中的非抽象方法”与“实现接口的类”混淆了。你的问题:

How is the addMouseListener(Object) method possible?

addMouseListener(Object) 方法是如何实现的?

It's possible because the object you pass to addMouseListener(MouseListener) (which doesn't take an Object, btw, but an instance of the MouseListener interface) is an instance of a class that implements that interface:

这是可能的,因为您传递给 addMouseListener(MouseListener) 的对象(不接受对象,顺便说一句,而是 MouseListener 接口的实例)是实现该接口的类的实例:

public MyMouseListener implements MouseListener { ... }
addMouseListener(new MyMouseListener());

I'd recommend you read a tutorial on Java (or OO) basics, such as this sectionof the Java Tutorial.

我建议您阅读有关 Java(或 OO)基础知识的教程,例如Java 教程的这一部分

回答by user989383

With Java 8 you can have default implementation for methods inside a interface please visit this link. http://www.lambdafaq.org/what-are-default-methods/

使用 Java 8,您可以为接口内的方法默认实现,请访问此链接。 http://www.lambdafaq.org/what-are-default-methods/