。
建议多看书先
thinking in java
建议多看书先
thinking in java
解决方案 »
- 我想学JSP不知来对地方没?是不是从JAVA 开始比较容易……我该怎么入门 我想在什么都不懂?怎么办?
- JAVA的List问题
- 利用網頁來傳送位置執行Java bean並且修改JAVA程式的檔案讀取位置
- Arrays.sort(arryOfInts);急用
- 请教:如何让窗口定位在自己想要的位置
- 求Java in Nutshell电子书,中英文都可以,多谢了
- 如何实现多行工具栏?
- 请问那里有WebLogin6.1下载
- 该怎么学习流的内容 (scjp)?
- 如果用foreach遍历一个集合,而这个集合需要实现Iteable接口,怎么重写iterator()
- 高分求nio方面的例子,想做个即时通讯通讯软件。
- 再问中文问题,java令我失望 解决立即给分
I i = new C();
}
{
void method();
}
public class Test implements Classjava
{
public void method()
{
System.out.println("接口的实现");
}
public static void main(String[] args)
{
new Test().method();
}
}
如果想实现(implement)接口(interface)一定要完全实现interface中的所有方法,如果不能完全实现接口所有方法的类,我们称之为 abstract class(抽象类).共同进步!~
This section shows you how to create and to use interfaces and talks about why you would use an interface instead of a class.
An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior. --------------------------------------------------------------------------------
Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants.
--------------------------------------------------------------------------------
Because an interface is simply a list of unimplemented, and therefore abstract, methods, you might wonder how an interface differs from an abstract class. The differences are significant.
An interface cannot implement any methods, whereas an abstract class can.
A class can implement many interfaces but can have only one superclass.
An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.
Let's set up the example we'll be using in this section. Suppose that you have written a class that can watch stock prices coming over a data feed. This class allows other classes to register to be notified when the value of a particular stock changes. First, your class, which we'll call StockMonitor, would implement a method that lets other objects register for notification:
public class StockMonitor {
public void watchStock(StockWatcher watcher,
String tickerSymbol, double delta) {
...
}
}The first argument to this method is a StockWatcher object. StockWatcher is the name of an interface whose code you will see in the next section. That interface declares one method: valueChanged. An object that wants to be notified of stock changes must be an instance of a class that implements this interface and thus implements the valueChanged method. The other two arguments provide the symbol of the stock to watch and the amount of change that the watcher considers interesting enough to be notified of. When the StockMonitor class detects an interesting change, it calls the valueChanged method of the watcher. The watchStock method ensures, through the data type of its first argument, that all registered objects implement the valueChanged method. It makes sense to use an interface data type here because it matters only that registrants implement a particular method. If StockMonitor had used a class name as the data type, that would artificially force a class relationship on its users. Because a class can have only one superclass, it would also limit what type of objects can use this service. By using an interface, the registered objects class could be anything--Applet or Thread--for instance, thus allowing any class anywhere in the class hierarchy to use this service.
——Gosling
只要类实现了接口,就可以在任何需要该接口的地方使用这个类的对象。
在Java中,接口不是类,而是一组对类的要求,这些类要与接口一致。
this.radius = radius;
} public void roll (long distance) {
System.out.println("Rolling for " + distance);
}}
//BasketBall.javapublic class BasketBall extends Ball { public BasketBall (long radius) {
super(radius);
}}因为只要是球形物体就可以滚动,篮球继承了球形物体的所有特性,当然也可以。但是和其他球形物体(如地球)不同,篮球是可以拿来玩的。我们会想到在类Basketball里面来实现这部分功能,修改Basketball为://BasketBall.javapublic class BasketBall extends Ball { public BasketBall (long radius) {
super(radius);
} public String play () {
return "Interesting...";
}}一个学生可以玩篮球,但是他/她当然不希望自己只能玩篮球,而不能玩其它的东西。如果把所有可以玩的东西都罗列一遍,那将是愚蠢的,能玩的东西太多了://Student.javapublic class Student { public void play(Basketball nikes) { } //public void play(Football nikes) { }
//public void play(Tabletennis nikes) { }
//There will be MUCH more...}但是,如果把"可以拿来玩"定义为一项抽象功能(即接口),并且让任何对象选择性的去实现它的话,我们就可以确定哪些对象是可以玩的,而哪些不是了。//Playable.javapublic interface Playable { public String play();}下面用接口的方法重新实现篮球://Basketball.javapublic class BasketBall extends Ball implements Playable { public BasketBall (long radius) {
super(radius);
} public String play () {
return "Interesting...";
}}同样的方法可以实现任何可以玩的物体,不管它是不是球类://Football.javapublic class FootBall extends Ball implements Playable { public BasketBall (long radius) {
super(radius);
} public String play () {
return "Not so interesting...";
}}//Computer.javapublic class Computer implements Playable { public Computer () { } public String play () {
return "Very interesting...";
}}这样一个学生就可以是://Student.javapublic class Student { public void play(Playable aThing) { }}JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。上面的例子也同样可以解释为什么接口方法一定是抽象的,因为不同对象有不同的实现细节,比如同样是可以玩的物体,有些很有趣,而有些就不是那么有趣。怎么样?用了接口以后,程序是不是变得合理多了?
和我抢分,呵呵。
基本正确。"JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。"不太正确。实现一个接口的类可以成为抽象类。