用java创建一个接口A,该接口中至少有一个方法a和一个常量,至少有两个实现该接口的类。使用接口技术输出相关信息
时间: 2024-05-14 17:14:20 浏览: 9
以下是用Java创建一个接口A,实现该接口的两个类以及输出相关信息的示例代码:
```java
// 定义接口A
public interface A {
// 声明一个方法a
void a();
// 定义一个常量
final String CONSTANT = "This is a constant in interface A";
}
// 实现接口A的第一个类
class AImpl1 implements A {
// 实现接口A中的方法a
public void a() {
System.out.println("This is method a in AImpl1");
}
}
// 实现接口A的第二个类
class AImpl2 implements A {
// 实现接口A中的方法a
public void a() {
System.out.println("This is method a in AImpl2");
}
}
// 输出相关信息
public class Main {
public static void main(String[] args) {
// 创建AImpl1对象并调用方法a
AImpl1 obj1 = new AImpl1();
obj1.a();
// 创建AImpl2对象并调用方法a
AImpl2 obj2 = new AImpl2();
obj2.a();
// 输出接口A定义的常量
System.out.println(A.CONSTANT);
}
}
```
输出结果:
```
This is method a in AImpl1
This is method a in AImpl2
This is a constant in interface A
```