java 接口 使用_JAVA中接口的使用

抽象类是从多个类中抽象出来的模板,如果将这种抽象进行的更彻底,那么就是接口(interface)了。什么是接口,简单的讲,接口就是抽象类的进一步抽象,这种进一步的抽象只定义了一种规范,而不需要关心具体的数据状态和方法实现细节,它只规定了一部分必须提供的方法。下面介绍接口的具体使用细节;

1.接口里不能包含构造器和初始化块定义,只能包含成员变量(静态常量)、方法(抽象实例方法、类方法或默认方法)、内部类(内部接口、枚举)这三种成员。

2.接口里的所有成员都应该定义为public权限,这个public可以省略,系统会自动补充。同理,接口里定义的静态常量会自动增加static和final,因此也可以省略。而且由于没有构造器和初始化块,接口里面的成员变量只能在定义时指定初始值。

3.接口里定义的方法只能是抽象方法、类方法或默认方法,所以系统会自动为普通方法增加public abstract修饰,同时普通方法不能有方法体(抽象方法)。但是类方法和默认方法都必须要有方法体。

4.默认方法必须要有default修饰,不能用static修饰,同理public会自动增加,默认方法需要接口实现的类的实例来调用。类方法必须要static修饰,public自动增加,类方法可以用接口来直接调用。

5.接口支持多继承,即一个接口可以有多个直接父接口。

下面是具体的例子:

packagebiology;public interfaceAnimal

{int classification = 7;

String eat();

String move();default voiddescription()

{

System.out.println("I am a kind of biology");

}staticString summary()

{return "The nature is wonderful";

}

}packagebiology;public class Dog implementsAnimal

{publicString name;publicDog(String name)

{this.name =name;

}publicString eat()

{return name + " eat meat and grass";

}publicString move()

{return name + " move with dog's legs";

}

}packagebiology;public class Goat implementsAnimal

{publicString name;publicGoat(String name)

{this.name =name;

}publicString eat()

{return name + " eat grass";

}publicString move()

{return name + " move with goat's legs";

}

}packagebiology;public class Tiger implementsAnimal

{publicString name;publicTiger(String name)

{this.name =name;

}publicString eat()

{return name + " eat meat";

}publicString move()

{return name + " move with tiger's legs";

}

}packagebiology;public classTest

{public static voidmain(String[] args)

{

Dog animal1= new Dog("Shepherd");

Tiger animal2= new Tiger("Bengal Tiger");

Goat animal3= new Goat("sheep");

System.out.println("The classification of biology is " +Animal.classification);

System.out.println(animal1.name+ ": " + animal1.eat() + ". " +animal1.move());

System.out.println(animal2.name+ ": " + animal2.eat() + ". " +animal2.move());

System.out.println(animal3.name+ ": " + animal3.eat() + ". " +animal3.move());

animal1.description();

animal2.description();

animal3.description();

System.out.println(Animal.summary());

}

}

运行结果如下:

The classification of biology is 7Shepherd: Shepherd eat meat and grass. Shepherd move with dog's legs

Bengal Tiger: Bengal Tiger eat meat. Bengal Tiger move with tiger's legs

sheep: sheep eat grass. sheep move with goat's legs

I am a kind of biology

I am a kind of biology

I am a kind of biology

The nature is wonderful

在这里例子中,我们定义了一个接口Animal, 在这个接口中定义了一个成员变量classification(自动添加public static final 修饰, 由接口调用),两个抽象方法eat()和move()(自动添加public abstract 修饰, 由接口实现类来实现), 一个默认方法(由接口实现类的实例调用),一个类方法(直接由接口调用)。

差不多就是这么多,具体的细节还要多看书和敲代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值