佛山信息技术网站开发,跨境电商在哪些网站上面做,松阳县建设局网站公示,微信小程序可以自己开发吗接口
1. 概念 在软件工程中#xff0c;软件与软件的交互很重要#xff0c;这就需要一个约定。每个程序员都应该能够编写实现这样的约定。接口就是对约定的描述。 In the Java programming language, an interface is a reference type, similar to a class, that can con…
接口
1. 概念 在软件工程中软件与软件的交互很重要这就需要一个约定。每个程序员都应该能够编写实现这样的约定。接口就是对约定的描述。 In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces 在 Java 编程语言中接口是类似于类的引用类型它只能包含常量方法签名默认方法静态方法和嵌套类型。 方法主体仅适用于默认方法和静态方法。 接口无法实例化- 它们只能由类实现或由其他接口扩展。 An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods. 接口声明可以包含方法签名默认方法静态方法和常量定义。 具有实现的方法是默认方法和静态方法。 从上面的描述中可以得出 接口中没有构造方法。 2. 接口定义
语法 [ public ] interface 接口名 { [ public static final ] 数据类型 变量名 变量的值 ; // 接口中定义变量该变量是静态常量在定义的时候必须赋值 返回值类型 方法名 ([ 参数列表 ]); // 定义接口方法 default 返回值类型 方法名 ([ 参数列表 ]){ // 接口中定义的默认方法必须在 JDK8 及以上版本使用 [ return 返回值 ;] } static 返回值类型 方法名 ([ 参数列表 ]){ // 接口中定义的静态方法必须在 JDK8 及以上版本使用 [ return 返回值 ;] } private 返回值类型 方法名 ([ 参数列表 ]){ // 接口中定义的私有方法必须在 JDK9 及以上版本使用 [ return 返回值 ;] } } package com . we . interfaceclass ; interface Test { public static final int number 10 ; public abstract void show (); public default int plus ( int a , int b ){ return a b ; } public static int multiply ( int a , int b ){ return a * b ; } private String getName (){ return admin ; } } 示例 使用接口描述人有名字 package com . we . interfaceclass . person ; public interface Person { String getName (); // 获取人的姓名 } 使用接口描述演员能表演 package com . we . interfaceclass . person ; public interface Actor { void performance (); // 演员表演 } 3. 接口继承 语法 [ public ] interface 接口名 extends 接口名 1 , 接口名 2 ,... 接口名 n { } 示例 使用接口继承描述演员是人 package com . we . interfaceclass . person ; public interface Actor extends Person { void performance (); // 演员表演 } 使用接口继承描述歌手是人 package com . we . interfaceclass . person ; public interface Singer extends Person { void sing (); // 唱歌 } 使用接口继承描述艺人既是演员也是歌手 package com . we . interfaceclass . person ; public interface Artist extends Actor , Singer { void endorsement (); // 代言 } 注意 接口可以多继承这是Java 中唯一可以使用多继承的地方。接口包含的变量都是静态常量接 口中包含的方法签名都是公开的抽象方法接口中的默认方法和静态方法在 JDK8 及以上版本才能定 义接口的私有方法必须在 JDK9 及以上版本才能定义。接口编译完成后也会生成相应的 class 文件。 4. 接口实现 实现接口语法 访问修饰符 class 类名 implements 接口名 1 , 接口名 2 ,... 接口名 n { } A class that implements an interface must implement all the methods declared in the interface. 实现接口的类必须实现接口中声明的所有方法。 一个类如果实现了一个接口那么就必须实现这个接口中定义的所有抽象方法包括接口通过继承关系继承过来的抽象方法这个类被称为接口的实现类或者说子类。与继承关系一样实现类与接口之间 的关系是 is-a 的关系。 示例 使用实现接口的方式描述娱乐明星是艺人 package com . we . interfaceclass . person ; public class EntertainmentStar implements Artist { private String name ; public EntertainmentStar ( String name ) { this . name name ; } Override public void endorsement () { System . out . printf ( 娱乐明星 %s 代言 \n , getName ()); } Override public void performance () { System . out . printf ( 娱乐明星 %s 表演 \n , getName ()); } Override public void sing () { System . out . printf ( 娱乐明星 %s 唱歌 \n , getName ()); } Override public String getName () { return name ; } } package com . we . interfaceclass . person ; public class PersonTest { public static void main ( String [] args ) { Person p new EntertainmentStar ( 刘德华 ); // 娱乐明星是人 System . out . println ( p . getName ()); Actor a new EntertainmentStar ( 范冰冰 ); // 娱乐明星是演员 a . performance (); Singer s new EntertainmentStar ( 张学友 ); // 娱乐明星是歌手 s . sing (); Artist artist new EntertainmentStar ( 古天乐 ); // 娱乐明星是艺人 artist . endorsement (); artist . performance (); artist . sing (); } } 5. 接口应用场景 一般来说定义规则、定义约定时使用接口。 示例 计算机对外暴露有 USB 接口 USB 接口生产商只需要按照接口的约定生产相应的设备 ( 比如 USB 键盘、USB 鼠标、优盘 ) 即可。 Java SE文章参考:Java SE入门及基础知识合集-CSDN博客