我们可以在Java的另一个接口中声明一个接口吗?
Java中的接口是方法原型的规范。每当您需要指导程序员或订立合同以指定应如何使用类型的方法和字段时,都可以定义接口。
要创建这种类型的对象,您需要实现此接口,为接口的所有抽象方法提供主体,并获取实现类的对象。
嵌套接口
Java允许在另一个接口内声明接口,这些接口称为嵌套接口。
在实现时,您需要将嵌套接口称为externalInterface.nestedInterface。
示例
在下面的Java示例中,我们有一个名为Cars4U_Services的接口,其中包含两个嵌套接口:CarRentalServices和CarSales,每个接口都有两个抽象方法。
从一个类中,我们实现了两个嵌套接口,并为所有四个抽象方法提供了主体。
interface Cars4U_Services {
interface CarRentalServices {
public abstract void lendCar();
public abstract void collectCar();
}
interface CarSales{
public abstract void buyOldCars();
public abstract void sellOldCars();
}
}
public class Cars4U implements Cars4U_Services.CarRentalServices,
Cars4U_Services.CarSales {
public void buyOldCars() {
System.out.println("We will buy old cars");
}
public void sellOldCars() {
System.out.println("We will sell old cars");
}
public void lendCar() {
System.out.println("We will lend cars for rent");
}
public void collectCar() {
System.out.println("Collect issued cars");
}
public static void main(String args[]){
Cars4U obj = new Cars4U();
obj.buyOldCars();
obj.sellOldCars();
obj.lendCar();
}
}输出结果
We will buy old cars We will sell old cars We will lend cars for rent
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短