艾巴生活网

您现在的位置是:主页>数码 >内容

数码

java接口通俗理解,java中的接口实例及其讲解

2023-10-11 09:56:04数码帅气的蚂蚁
对于java接口通俗理解,java中的接口实例及其讲解,很多网友还不是很明白,今天艾巴小编收集了这方面的知识,就将其分享出来。 操作方法:

java接口通俗理解,java中的接口实例及其讲解

对于java接口通俗理解,java中的接口实例及其讲解,很多网友还不是很明白,今天艾巴小编收集了这方面的知识,就将其分享出来。

操作方法:

1.首先,我们实现一个简单的接口定义。接口USB { public void print();}你不能在接口中实现函数的方法,也就是说,你不能这样做。接口USB { public void print(){ system . out . println(' hello world ');};}

2. Next, we use a class to implement this interface. That is to say, it is a concrete process. For example, class computer implements the USB {@ override public void print () {//Todo automatic generation method stubSystem.out.println ('I am a computer'); }

3. Next, you can implement this class in the main function. The following example is public static void main (string [] args) {computercomputer=newcomputer (); Computer. Print (); }

4. Next, inherit multiple interfaces. The following example. Interface USB {public void print (); } interface call {publicovoid callyou (); }

5. The following is implemented with classes. The following example: class computer implements usb, and calls {@ override public void print () {//Todo automatic generation method stubSystem.out.println ('I am a computer'); } @ override public void callyou () {//Todo automatically generated method stubSystem.out.println ('I will call'); }}

6. Just implement it again in the main method.

以上知识分享到此为止,希望能够帮助到大家!