接口和覆盖接口的方法

编程入门 行业动态 更新时间:2024-10-24 12:27:28
本文介绍了接口和覆盖接口的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我13岁,对java很新。我似乎无法弄清楚的是如何不从接口实现类中的重写方法,因为它们是引用。我不想创建一个新的副本,我不能只是(在这里插入Class)扩展(接口获取它的一些方法的类)。所以我实现了它,我得到了什么?

I'm 13 and quite new to 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). 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*/){ ... }

我不想重新定义界面中的方法,只需使用它们。我不能让类ThreadManager扩展Debugger(.java),因为它已经扩展了一些东西。我认为接口是一种方法,你可以在另一个类中使用这些方法而不通过class foo extends bar继承它们。

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. I thought interfaces were a way you could use those methods in another class without inheriting them through "class foo extends bar"

顺便说一句,接口中引用的所有方法是我的类Debugger.java中的方法的引用,它们兼作调试器和游戏库。

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.

推荐答案

你不能使用方法来自界面。接口没有代码,只有定义。将其视为功能合同,实现它的类必须满足。

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.

例如

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

这是一个实现此接口的所有类必须满足的合约。这意味着实现的任何可实例化的类必须实现 public void method1ToImplement()和 public int method2ToImplement(String)。这是因为你说这个类实现了这个功能,所以你必须实现这个功能,因为到目前为止,你的类中没有这个功能的代码,因为接口不包含代码。例如,您不能使用 List 中的方法,实际上您甚至无法创建新的 List ,因为它是一个接口。但是你可以创建和 ArrayList 并使用它的方法,因为它是一个实现 List 接口的非抽象类。

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). 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. But you can create and ArrayList and use its methods because it's a non-abstract class implementing the List interface.

也许你很困惑,因为你看到其他地方你可以使用已经实现的方法,例如 toString()(其中已经在所有类中实现)。这是因为此方法未在接口中定义,而是由父类定义(在 toString()的情况下,它是实现它的Object)。

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). 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).

TL; DR:实现接口的类必须实现其方法,除非它是抽象的。

TL;DR: A class implementing an interface must implement its methods unless it's abstract.

更多推荐

接口和覆盖接口的方法

本文发布于:2023-11-04 05:36:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!