一个类可以有一个接口,接口可以有一个Java类吗?
In the very first step, we will see can a class have an interface in Java?
在第一步中,我們將看到類可以在Java中具有接口嗎?
Yes, it is possible to define an interface inside the class.
是的,可以在類內部定義接口。
The interface is defined in another interface is known as nested interface, but when we define an interface inside the class then that is also known as the nested interface.
該接口是在另一個接口中定義的,稱為嵌套接口,但是當我們在類內部定義一個接口時,也稱為嵌套接口。
The objective of defining an interface inside a class is used to group related interfaces so that they can be managed easily.
在類內部定義接口的目的是對相關接口進行分組,以便可以輕松管理它們。
Once an interface is defined in a class then we are not able to access an interface directly (i.e. an interface must be referred by a class).
一旦在類中定義了接口,則我們將無法直接訪問接口(即,接口必須由類引用)。
There is a restriction on access modifiers when we define an interface in a class.
當我們在類中定義接口時,對訪問修飾符有限制。
It is not mandatory to prefix "static" keyword with the interfaces defined in a class because the interface is by default static.
不必在類中定義的接口前面加上“ static”關鍵字,因為默認情況下該接口是靜態的。
Syntax:
句法:
class MyClass{// MyClass Codeinterface MyInterface(){//MyInterface Code}}Example:
例:
// Java program to demonstrate the example of // defining an interface in a classclass MyClass {// Interface definition in a classinterface MyInterface {void display();} }public class Main implements MyClass.MyInterface {String str = "we are learning Java Programming";// override abstract method of interfacepublic void display() {System.out.print("Hi,");}public static void main(String[] args) {Main m = new Main();MyClass.MyInterface mc = new Main();// Calling Main class method of interfacemc.display();System.out.println(m.str);} }Output
輸出量
Hi, we are learning Java ProgrammingIn the second step, we will see can an interface have a class in Java?
在第二步中,我們將看到接口在Java中可以有一個類嗎?
Yes, it is possible to define a class inside the interface.
是的,可以在接口內部定義一個類。
The objective of defining a class inside an interface is used to group related interfaces so that they can be managed easily.
在接口內定義類的目的是對相關接口進行分組,以便可以輕松管理它們。
Once a class is defined in an interface then we are not able to access a class directly (i.e. a class must be referred by an interface).
一旦在接口中定義了一個類,那么我們將無法直接訪問該類(即,一個類必須由接口引用)。
There is no restriction on access modifiers when we define a class in an interface.
當我們在接口中定義一個類時,對訪問修飾符沒有任何限制。
It is not mandatory to prefix "static" keyword with the class defined in an interface because the class is by default public.
由于在默認情況下該類是公共類,因此不必在接口中定義的類前面加上“ static”關鍵字。
Syntax:
句法:
interface MyInterface{// MyInterface Codeclass MyClass(){// MyClass Code} }Example:
例:
// Java program to demonstrate the example of // defining a class in an interfaceinterface MyInterface {// MyClass definitionclass MyClass {String str = "Java support OOPS Concept";void display() {System.out.print("Hi,");}} }public class Main extends MyInterface.MyClass {public static void main(String[] args) {// Main class is instantiatedMain m = new Main();// Calling MyClass methodm.display();System.out.println(m.str);} }Output
輸出量
Hi, Java support OOPS Concept翻譯自: https://www.includehelp.com/java/can-a-class-have-an-interface-and-can-an-interface-have-a-class-in-java.aspx
總結
以上是生活随笔為你收集整理的一个类可以有一个接口,接口可以有一个Java类吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 保姆级教学:缓存穿透、缓存击穿和缓存雪崩
- 下一篇: scala 数组合并_Scala程序合并