java实现接口的关键字_Java 接口关键字 interface

interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现,它允许创建者确定方法名.参数列表和返回类型,但没有任何方法体,接口只提供了形式,而未提供任何具体实现

一个接口表示:"所有实现了该特定接口的类看起来都像这样".接口被用来建立类与类之间的协议(某些面向对象语言用关键字protocol来完成这一功能.)

要想创建一个接口,需要用interface关键字代替class关键字,就像类一样,可以在interface前添加public关键字(但仅限于该接口在与其同名的文件中被定义),如果不加public,则只有包访问权限,这样就只能在同一个包内可用,接口也可以包含域,但是这些域都隐式地是static和final的

要让一个类遵循某个特定接口(或者是一组接口),需要使用implements关键字,它表示"interface只是它的外貌,但是现在我要声明它如何工作的."除此之外,它看起来很像继承

恰当的原则是优先选择类而不是接口,从类开始,如果接口的必要性变的非常明确,那么就进行重构.

//: interfaces/music5/Music5.java//Interfaces.

packageobject;import static net.mindview.util.Print.*;enumNote{

MIDDLE_C,MIDDLE_D,MIDDLE_F

}interfaceInstrument {//Compile-time constant:

int VALUE = 5; //static & final 可以声明域,但这些域都隐式地是static和final的//Cannot have method definitions:

void play(Note n); //Automatically public//自动的是public

voidadjust();

}class Wind implementsInstrument {public voidplay(Note n) {

print(this + ".play() " +n);

}public String toString() { return "Wind"; }public void adjust() { print(this + ".adjust()"); }

}class Percussion implementsInstrument {public voidplay(Note n) {

print(this + ".play() " +n);

}public String toString() { return "Percussion"; }public void adjust() { print(this + ".adjust()"); }

}class Stringed implementsInstrument {public voidplay(Note n) {

print(this + ".play() " +n);

}public String toString() { return "Stringed"; }public void adjust() { print(this + ".adjust()"); }

}class Brass extendsWind {public String toString() { return "Brass"; }

}class Woodwind extendsWind {public String toString() { return "Woodwind"; }

}public classMusic5 {//Doesn't care about type, so new types//added to the system still work right:

static voidtune(Instrument i) {//...

i.play(Note.MIDDLE_C);

}static voidtuneAll(Instrument[] e) {for(Instrument i : e)

tune(i);

}public static voidmain(String[] args) {//Upcasting during addition to the array:

Instrument[] orchestra ={newWind(),newPercussion(),newStringed(),newBrass(),newWoodwind()

};

tuneAll(orchestra);

}

}/*Output:

Wind.play() MIDDLE_C

Percussion.play() MIDDLE_C

Stringed.play() MIDDLE_C

Brass.play() MIDDLE_C

Woodwind.play() MIDDLE_C*///:~

继承和接口可以在同一个类同时使用

//: polymorphism/Sandwich.java//Order of constructor calls.

packagech08;interfaceFastFood{voidshow();

}classMeal {

Meal() { System.out.println("Meal()"); }

}classBread {

Bread() { System.out.println("Bread()"); }

}classCheese {

Cheese() { System.out.println("Cheese()"); }

}classLettuce {

Lettuce() { System.out.println("Lettuce()"); }

}class Lunch extendsMeal {

Lunch() { System.out.println("Lunch()"); }

}class PortableLunch extendsLunch {

PortableLunch() { System.out.println("PortableLunch()");}

}public class Sandwich extends PortableLunch implementsFastFood{ //继承和接口可以在同一个类同时使用private Bread b = newBread();private Cheese c = newCheese();private Lettuce l = newLettuce();public voidshow()

{

System.out.println("pushing your sandwich order");

}publicSandwich()

{

System.out.println("Sandwich()");

show();

}public static voidmain(String[] args) {newSandwich();

}

}/*Output:

Meal()

Lunch()

PortableLunch()

Bread()

Cheese()

Lettuce()

Sandwich()pushing your sandwich order

*///:~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java语言中的关键字是指具有特殊含义的单词,这些单词在Java程序中具有特定的用途,不能作为标识符或变量名使用。Java中共有50个关键字,其中包括48个保留关键字和2个特殊关键字。 以下是Java中的各种关键字: 1. abstract:用于定义抽象类和抽象方法。 2. assert:用于调试程序时进行断言判断,如果条件不成立将会抛出AssertionError异常。 3. boolean:用于定义布尔类型变量,只能取值true或false。 4. break:用于跳出循环语句。 5. byte:用于定义字节类型变量,取值范围为-128到127。 6. case:用于在switch语句中匹配选项。 7. catch:用于捕获异常。 8. char:用于定义字符类型变量。 9. class:用于定义类。 10. const:Java虽然保留了此关键字,但并没有使用,因此不能用于定义常量。 11. continue:用于跳过循环中的某个迭代。 12. default:用于switch语句中的默认选项。 13. do:用于定义do-while循环。 14. double:用于定义双精度浮点类型变量。 15. else:用于if语句中条件不成立时执行的代码块。 16. enum:用于定义枚举类型。 17. extends:用于继承一个类或实现一个接口。 18. final:用于定义常量或不可变的变量,或者修饰类、方法、变量等,表示其不可再被继承、重写或修改。 19. finally:用于定义无论是否有异常发生都需要执行的代码块。 20. float:用于定义单精度浮点类型变量。 21. for:用于定义for循环。 22. goto:Java虽然保留了此关键字,但并没有使用,因此不能跳转到标签。 23. if:用于定义条件语句。 24. implements:用于实现一个接口。 25. import:用于导入其他类的定义。 26. instanceof:用于判断一个对象是否属于某个类或实现了某个接口。 27. int:用于定义整型变量。 28. interface:用于定义接口。 29. long:用于定义长整型变量。 30. native:用于调用本地方法。 31. new:用于创建一个对象。 32. package:用于定义包。 33. private:用于定义私有成员,只能在当前类中访问。 34. protected:用于定义受保护的成员,只能在当前类及其子类和同一个包中访问。 35. public:用于定义公共成员,可以被任何类访问。 36. return:用于从方法中返回值。 37. short:用于定义短整型变量。 38. static:用于定义静态成员,只有一个拷贝,可以通过类名直接访问。 39. strictfp:用于声明浮点数计算具有严格的规范化行为。 40. super:用于引用父类的成员。 41. switch:用于定义switch语句。 42. synchronized:用于定义同步方法或同步代码块。 43. this:用于引用当前对象。 44. throw:用于抛出异常。 45. throws:用于声明方法可能抛出的异常。 46. transient:用于声明不需要持久化的变量。 47. try:用于定义异常处理代码块。 48. void:用于定义无返回值的方法。 49. volatile:用于声明变量是易变的,即每次访问都需要从主存中读取。 50. while:用于定义while循环。 以上就是Java中的各种关键字,这些关键字Java程序中起着非常重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值