简体   繁体   English

接口和覆盖接口的方法

[英]interface and overriding the methods of the interface

I'm 13 and quite new to java. 我13岁,对java很新。 What I can't seem to figure out is how NOT to implement overriding methods in a class from an interface because they are references. 我似乎无法弄清楚的是如何不从接口实现类中的重写方法,因为它们是引用。 I don't want to make a new copy, and I can't just make (insert Class here) extend (the class the interface gets some of its methods from). 我不想创建一个新的副本,我不能只是make(在这里插入Class)扩展(接口获取它的一些方法的类)。 So I implement it and what do i get? 所以我实现了它,我得到了什么?

err: The type Threadmanager must implement the inherited abstract method (the method)

and then it has a list, one of which says "implement uninherited methods". 然后它有一个列表,其中一个表示“实现未经加工的方法”。

But I dont want to implement any methods! 但我不想实施任何方法! I want to use them! 我想用它们!

Threadmanager tm;
AwtUtils manager = tm;
manager.drawImage(/*params*/)

The above is what i want, the following is what i don't want: 以上是我想要的,以下是我不想要的:

@override
public void drawImage(/*params*/){
...
}

I don't want to redefine the methods in the interface, simply just use them. 我不想重新定义界面中的方法,只需使用它们即可。 and I cant have class ThreadManager extends Debugger(.java) because it already extends something. 我不能让类ThreadManager扩展Debugger(.java),因为它已经扩展了一些东西。 I thought interfaces were a way you could use those methods in another class without inheriting them through "class foo extends bar" 我认为接口是一种你可以在另一个类中使用这些方法而不通过“class foo extends bar”继承它们的方法

By the way, all the methods referenced in the interface are references to methods in my class Debugger.java which doubles up as a debugger and the game library. 顺便说一下,接口中引用的所有方法都是对我的类Debugger.java中的方法的引用,它们兼作调试器和游戏库。

You cannot use methods from an interface. 您不能使用界面中的方法。 An interface has no code, only definitions. 接口没有代码,只有定义。 Think of it as a functionality contract that classes implementing it have to fulfill. 可以将其视为实现它的类必须实现的功能契约

For example 例如

public interface Example {
    public void method1ToImplement();
    public int method2ToImplement(final String input);
}

This is a contract that all classes implementing this interface must fulfill. 这是一个实现此接口的所有类必须满足的合同。 This means any instantiable class that implements Example has to implement public void method1ToImplement() and public int method2ToImplement(String) . 这意味着任何implements Example实例化类都必须实现public void method1ToImplement()public int method2ToImplement(String) This is because you're stating this class fulfills this functionality, so you must implement this funcionality because as of now there's no code for this functionality in your class since the interface contains no code. 这是因为您声明此类实现了此功能,因此您必须实现此功能,因为到目前为止,您的类中没有此功能的代码,因为该接口不包含代码。 For example, you cannot use the methods in List , in fact you cannot even create a new List because it's an interface. 例如,您不能使用List的方法,实际上您甚至无法创建新的List因为它是一个接口。 But you can create and ArrayList and use its methods because it's a non-abstract class implementing the List interface. 但是你可以创建和ArrayList并使用它的方法,因为它是一个实现List接口的非抽象类。

Maybe you're confused because you saw somewhere else you can use already implemented methods, for example toString() (which is already implemented in all classes). 也许你很困惑,因为你看到其他地方你可以使用已经实现的方法,例如toString() (已经在所有类中实现)。 This is because this method is not defined in an interface but by a parent class (in case of toString() it's Object that implements it). 这是因为此方法未在接口中定义,而是由父类定义(在toString()情况下,它是实现它的Object)。

TL;DR: A class implementing an interface must implement its methods unless it's abstract. TL; DR:实现接口的类必须实现其方法,除非它是抽象的。

If I'm understanding you right, you want a class to implement an interface, but don't implement its methods. 如果我正确理解你,你想要一个类来实现一个接口,但是不要实现它的方法。 If that's so, you cannot. 如果是这样,你就不能。 Implementation of interface methods is mandatory, unless you're writing an abstract class . 除非您正在编写抽象类 ,否则必须实现接口方法。

I'm guessing there's something missing on your question, so please, provide some code of your Interface and Class so that we could give you a better answer. 我猜你的问题有些缺失,所以请提供一些InterfaceClass代码,以便我们给你一个更好的答案。

I think you're confused about what an interface does. 我觉得你对界面的作用感到困惑。 An interface simply defines a contract such that any object which implements the interface must define the methods in the interface. 接口只是定义一个契约,这样实现接口的任何对象都必须在接口中定义方法。 If you have an abstract class, then you must implement the abstract methods of said class for any class that extends the abstract class. 如果您有一个抽象类,那么您必须为扩展抽象类的任何类实现所述类的抽象方法。 The only exception to this is when you extend from a class that has already implemented the abstract methods or interface and you don't want/need to redefine them for subclasses. 唯一的例外是当你从已经实现了抽象方法或接口的类extend而你不想/需要为子类重新定义它们时。

You say that you don't want to implement the methods, you just want to use them, but you can't use methods that don't exist. 你说你不想实现这些方法,你只想使用它们,但你不能使用不存在的方法。 Implementing an interface does not magically define the logic in the methods in the interface--that is your job. 实现一个接口并没有神奇地定义接口中方法中的逻辑 - 这是你的工作。 Again, it simply states that any objects that implement the interface will have the interfaces' methods defined. 同样,它只是声明实现接口的任何对象都将定义接口的方法。

One of the nice things about interfaces is the following: Let's assume that we have a collection of objects that all implement a particular interface, then we can call any method from the interface on all those objects. 关于接口的一个好处是:假设我们有一个对象集合,它们都实现了特定的接口,然后我们就可以从所有这些对象的接口调用任何方法。 NB: we can group said objects together by having an array, ArrayList, or what have you that take the interface as the type parameter, ie ArrayList<MyInterface> 注意:我们可以通过使用数组,ArrayList或者将接口作为类型参数的东西将所述对象组合在一起,即ArrayList<MyInterface>

More specific example: 更具体的例子:
Let's consider a Shape interface that solely includes the header for an area method. 让我们考虑一个Shape接口,它只包含一个区域方法的头。 We can have a bunch of difference types of shapes that implement the Shape interface (circles, squares, etc). 我们可以有一堆不同类型的形状来实现Shape界面(圆形,正方形等)。 In each shape class, we define a method to get the area for said shape. 在每个形状类中,我们定义一个方法来获得所述形状的区域。 Now, if we have an ArrayList<Shape> shapes =... we can put different types of shapes into that list and do the following: 现在,如果我们有一个ArrayList<Shape> shapes =...我们可以将不同类型的形状放入该列表中并执行以下操作:

for (Shape s : shapes)
{
     System.out.println(s.area());
}

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

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