• 接口(上)


    🐷1.接口的概念

    🐱‍🚀2.接口的语法规则

    💚3.接口的使用

    🚀4.接口的特性

    🎆5.实现多个接口

    🐶6.接口间的继承

    🎊7.接口使用的实例

    1.什么是接口呢???

    接口放在现实生活中来说呢,就是手机啊。电脑的插座之类的,我们会发现那些插座几乎大家的充电口都能用,那么这就是一种标准

    Java 中,接口可以看成是:多个类的公共规范,是一种引用数据类型。
    也就是对抽象类的的进一步抽象
    接口的语法规则
    接口用interface来修饰
    创建接口时, 接口的命名一般以大写字母 I 开头.
     接口的命名一般使用 "形容词" 词性的单词 .

     接口不能直接实例化,会报错

    1. interface IShape{
    2. public abstract void draw();
    3. public static final String name="一宝";
    4. default public void func(){
    5. System.out.println("默认的");
    6. }
    7. }
    8. class Rect implements IShape{
    9. @Override
    10. public void draw() {
    11. System.out.println("🟥");
    12. }
    13. }
    14. class Circle implements IShape{
    15. @Override
    16. public void draw() {
    17. System.out.println("○");
    18. }
    19. }
    20. class Heart implements IShape{
    21. @Override
    22. public void draw() {
    23. System.out.println(";❤");
    24. }
    25. }
    26. public class Test {
    27. public static void main(String[] args) {
    28. IShape shape = new Rect();
    29. IShape shape2 = new Circle();
    30. IShape shape3 = new Heart();
    31. shape.draw();
    32. shape.func();
    33. shape2.draw();
    34. shape2.func();
    35. shape3.draw();
    36. shape3.func();
    37. }
    38. }

    这个代码的结果是什么呢,shape.func()可以被调用吗???

     对于shape.func()可以这样理解,调用shape.draw()发生动态绑定,就到那个Rect重写的draw方法去了,但是调用shape.fun()就到接口中去了,因为func本身也是一个抽象方法,因为有了default的修饰,所以可以不在子类中重写,通过接口的引用,就直接到IShape当中去调了

    下面实现电脑USB接口

    1. package demo2;
    2. /**
    3. * Created with IntelliJ IDEA.
    4. * Description:
    5. * User: WHY
    6. * Date: 2022-11-16
    7. * Time: 10:52
    8. */
    9. interface IUSB{
    10. void openDevice();
    11. void closeDevice();
    12. }
    13. class KeyBoard implements IUSB {
    14. @Override
    15. public void openDevice() {
    16. System.out.println("打开键盘");
    17. }
    18. @Override
    19. public void closeDevice() {
    20. System.out.println("关闭键盘");
    21. }
    22. public void intput(){
    23. System.out.println("敲击键盘");
    24. }
    25. }
    26. class Mouse implements IUSB{
    27. @Override
    28. public void openDevice() {
    29. System.out.println("打开鼠标");
    30. }
    31. @Override
    32. public void closeDevice() {
    33. System.out.println("关闭鼠标");
    34. }
    35. public void click(){
    36. System.out.println("点击鼠标");
    37. }
    38. }
    39. class Computer{
    40. public void open(){
    41. System.out.println("开机");
    42. }
    43. public void close(){
    44. System.out.println("关机");
    45. }
    46. public void useDevice(IUSB usb){//判断一下是不是所有的USB接口都可以被使用
    47. usb.openDevice();
    48. if(usb instanceof Mouse){
    49. Mouse mouse=(Mouse)usb;//向下转型
    50. mouse.click();//执行子类中特有的方法
    51. }else if(usb instanceof KeyBoard){
    52. KeyBoard keyBoard=(KeyBoard)usb;
    53. keyBoard.intput();
    54. }
    55. usb.closeDevice();
    56. }
    57. }
    58. public class Test {
    59. public static void main(String[] args) {
    60. Computer computer=new Computer();
    61. IUSB iusb=new KeyBoard();//向上转型
    62. computer.useDevice(iusb);
    63. IUSB iusb1=new Mouse();
    64. computer.useDevice(iusb1);
    65. }
    66. }
    1. package demo3;
    2. /**
    3. * Created with IntelliJ IDEA.
    4. * Description:
    5. * User: WHY
    6. * Date: 2022-11-16
    7. * Time: 13:48
    8. */
    9. abstract class Animal{
    10. public String name;
    11. public Animal(String name) {
    12. this.name = name;
    13. }
    14. }
    15. interface IRunning{
    16. void run();
    17. }
    18. interface ISwimming{
    19. void swim();
    20. }
    21. interface IFly{
    22. void fly();
    23. }
    24. class Dog extends Animal implements IRunning{
    25. public Dog(String name) {
    26. super(name);
    27. }
    28. @Override
    29. public void run() {
    30. System.out.println(name+"正在跑");
    31. }
    32. }
    33. class Duck extends Animal implements IFly,IRunning,ISwimming{
    34. public Duck(String name) {
    35. super(name);
    36. }
    37. @Override
    38. public void run() {
    39. System.out.println(name+"正在跑");
    40. }
    41. @Override
    42. public void fly() {
    43. System.out.println(name+"正在飞");
    44. }
    45. @Override
    46. public void swim() {
    47. System.out.println(name+"正在游泳");
    48. }
    49. }
    50. class robot implements ISwimming{
    51. public String name;
    52. public robot(String name) {
    53. this.name = name;
    54. }
    55. @Override
    56. public void swim() {
    57. System.out.println(name+"机器人在游泳");
    58. }
    59. }
    60. public class TestDemo {
    61. public static void walk(IRunning iRunning){
    62. iRunning.run();
    63. }
    64. public static void swim(ISwimming iSwimming){
    65. iSwimming.swim();
    66. }
    67. public static void main(String[] args) {
    68. walk(new Dog("贝贝"));
    69. walk(new Duck("丫丫"));
    70. swim(new Duck("丫丫1"));
    71. swim(new robot("小冰"));
    72. }
    73. }

    从这两段代码可以看出,只要这个类继承了这个接口,就可以实现

     

     

    1. package demo4;
    2. /**
    3. * Created with IntelliJ IDEA.
    4. * Description:
    5. * User: WHY
    6. * Date: 2022-11-16
    7. * Time: 14:14
    8. */
    9. class Student implements Comparable{
    10. public String name;
    11. public int age;
    12. public int score;
    13. public Student(String name, int age, int score) {
    14. this.name = name;
    15. this.age = age;
    16. this.score = score;
    17. }
    18. @Override
    19. public String toString() {
    20. return "Student{" +
    21. "name='" + name + '\'' +
    22. ", age=" + age +
    23. ", score=" + score +
    24. '}';
    25. }
    26. public int compareTo(Student o){//根据年龄比较排序
    27. if(this.age>o.age){
    28. return 1;
    29. }else if(this.age
    30. return -1;
    31. }else{
    32. return 0;
    33. }
    34. }
    35. }
    36. //根据姓名字母排序
    37. /* if(this.name.compareTo(o.name) > 0) {
    38. return 1;
    39. }else if(this.name.compareTo(o.name) < 0) {
    40. return -1;
    41. }else {
    42. return 0;
    43. }*/
    44. //使用冒泡排序实现依据年龄排序
    45. public class Test {
    46. public static void sort(Comparable[] array) {
    47. for (int i = 0; i < array.length-1; i++) {
    48. for (int j = 0; j < array.length-1-i; j++) {
    49. /*if(array[j] > array[j+1]) {
    50. 交换;
    51. }*/
    52. if(array[j].compareTo(array[j+1]) > 0) {
    53. Comparable tmp = array[j];
    54. array[j] = array[j+1];
    55. array[j+1] = tmp;
    56. }
    57. }
    58. }
    59. }
    60. public static void main(String[] args) {
    61. Student[] students=new Student[3];
    62. students[0]=new Student("wangyibo",25,100);
    63. students[1]=new Student("weihongyan",19,99);
    64. students[2]=new Student("gaoyan",19,95);
    65. System.out.println(students[0].compareTo(students[1]));
    66. }
    67. }

     

     这个是调用sort函数实现排序

    这个是调用compareTo方法实现的

    当比较字符串是否相等用equals方法,当比较大小也要用compareT方法,要是想从大到小排,换个return  就好

     下面来总结一下接口的知识点

    使用interface 接口来定义接口

    接口不能被实例化

    接口中的方法默认是public abstract 修饰的

    接口中的成员默认是public static  final  修饰的

    接口当中的方法,不能有具体实现,但是从jdk8开始,可以写一个default来修饰,我已经子啊前面的代码展示并解释了

    接口当中不能有构造方法

    接口需要被类实现,使用关键字implements

    接口中可以有static修饰的方法

    接口和接口之间使用关键字extends继承,与类的单继承不一样啊,类可以同时继承多个接口,接口和接口之间也可以多继承

    1. interface A {
    2. void funcA();
    3. }
    4. interface B {
    5. void funcB();
    6. }
    7. //CC这个接口 不仅仅具备func这个功能,还具备了A和B接口的功能
    8. interface CC extends A,B {
    9. void funcC();
    10. }
    11. class C implements CC {
    12. public void funcA() {
    13. }
    14. public void funcB() {
    15. }
    16. public void funcC() {
    17. }
    18. }
    19. public class Test2 {
    20. }

    就像这个,一个接口也可以同时继承多个接口,并拥有被继承接口的功能

    类继承另一个类同时实现多个接口,代码也有体现到

    今天的内容就到此结束,我们下期再见,886!!!

    💚💚💚💚💚💚💚💚💚💚

  • 相关阅读:
    如何解决 CentOS 7 官方 yum 仓库无法使用的问题
    从零开始搭建仿抖音短视频APP--开发用户业务模块(4)
    前端面试练习24.3.5
    数据链路层具体协议
    NL2SQL技术方案系列(1):NL2API、NL2SQL技术路径选择;LLM选型与Prompt工程技巧,揭秘项目落地优化之道
    如何测试 esp-matter_example_light 例程
    算法体系-12 第 十二 二叉树的基本算法
    MySQL 操作语句大全(详细)
    计网第五章(运输层)(四)(TCP的流量控制)
    python中的单例模型:(详细的单例可以去主页c++中的单例模型)
  • 原文地址:https://blog.csdn.net/weixin_61436104/article/details/127868632