- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 250字
- 2021-06-11 13:05:23
Interfaces
Interfaces are reference types in Java. As such, they define the skeleton of classes and objects but without including the actual functionality of methods. Classes implement interfaces but do not extend them. Let's look at an example of a simple interface, further developing the idea of building classes to represent different types of computers.
interface Computer {
public String getDeviceType();
public String getSpeed();
}
class Tablet implements Computer {
public String getDeviceType() {
return "it is a tablet";
}
public String getSpeed() {
return "1GHz";
}
}
class Example15 {
public static void main(String[] args) {
Tablet myTab = new Tablet();
System.out.println( myTab.getDeviceType() );
System.out.println( myTab.getSpeed() );
}
}
As you might have guessed, the output for this example is:
it is a tablet
1GHz
Process finished with exit code 0
Some relevant notes on interfaces follow:
- Interfaces can extend other interfaces.
- Unlike classes, which can only extend one class at a time, interfaces can extend multiple interfaces at once. You do so by adding the different interfaces separated by commas.
- Interfaces have no constructors.
- 編程的修煉
- Delphi程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- Web Development with Django Cookbook
- 單片機(jī)應(yīng)用技術(shù)
- Podman實(shí)戰(zhàn)
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- 高級語言程序設(shè)計(jì)(C語言版):基于計(jì)算思維能力培養(yǎng)
- Selenium Testing Tools Cookbook(Second Edition)
- C語言程序設(shè)計(jì)
- 寫給程序員的Python教程
- 算法圖解
- 深入淺出Python數(shù)據(jù)分析
- Python 3快速入門與實(shí)戰(zhàn)
- Node.js 6.x Blueprints