Java Interface 接口

 

 

 

 

 

 

 

 

UsbInterface.java

1
2
3
4
5
6
package com;
 
public interface UsbInterface {
  abstract public void start();
  abstract public void stop();
}

  

Camera.java

1
2
3
4
5
6
7
8
9
10
11
12
package com;
 
public class Camera implements UsbInterface{
  @Override
  public void start(){
    System.out.println("camera start");
  }
  @Override
  public void stop(){
    System.out.println("camera stop");
  }
}

  

Phone.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com;
 
public class Phone implements UsbInterface{
  @Override
  public void start() {
    System.out.println("phone start");
  }
 
  @Override
  public void stop() {
    System.out.println("phone stop");
  }
}

  

Computer.java

1
2
3
4
5
6
7
8
package com;
 
public class Computer {
  public void work(UsbInterface usbInterface){
    usbInterface.start();
    usbInterface.stop();
  }
}

  

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com;
 
import org.jetbrains.annotations.NotNull;
 
 
public class Test {
  public static void main(String @NotNull [] args) {
    Camera camera = new Camera();
    Phone phone = new Phone();
    Computer computer = new Computer();
    computer.work(camera);
    computer.work(phone);
  }
}

  

DBInterface.java

 

1
2
3
4
5
6
7
package com;
 
public interface DBInterface {
  public void connect();
 
  public void close();
}

 

  

MySQL.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com;
 
public class MySQL implements DBInterface {
  @Override
  public void connect() {
    System.out.println("connect MySQL");
  }
 
  @Override
  public void close() {
    System.out.println("stop MySQL");
  }
}

  

Oracle.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com;
 
public class Oracle implements DBInterface {
  @Override
  public void connect() {
    System.out.println("connect oracle");
  }
 
  @Override
  public void close() {
    System.out.println(("stop oracle"));
  }
}

  

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com;
 
import org.jetbrains.annotations.NotNull;
 
 
public class Test {
  public static void main(String @NotNull [] args) {
    MySQL mySQL = new MySQL();
    Oracle oracle = new Oracle();
    test(mySQL);
    test(oracle);
  }
 
  public static void test(DBInterface dbInterface) {
    dbInterface.connect();
    dbInterface.close();
  }
}

  

 

posted @   ascertain  阅读(62)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示