一个类可以有一个接口,接口可以有一个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 Code
        interface MyInterface(){
            //MyInterface Code
        }
    }

Example:

例:

// Java program to demonstrate the example of
// defining an interface in a class

class MyClass {
    // Interface definition in a class
    interface MyInterface {
        void display();
    }
}

public class Main implements MyClass.MyInterface {
    String str = "we are learning Java Programming";
    // override abstract method of interface
    public 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 interface
        mc.display();
        System.out.println(m.str);
    }
}

Output

输出量

Hi, we are learning Java Programming

In 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 Code
        class MyClass(){
            // MyClass Code
        } 
    } 

Example:

例:

// Java program to demonstrate the example of
// defining a class in an interface

interface MyInterface {
    // MyClass definition
    class 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 instantiated
        Main m = new Main();
        // Calling MyClass method
        m.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值