当前位置: 技术问答>java相关
不是说一个非抽象类继承抽象类必须为抽象类中所有的抽象方法书写方法体吗?那么请进来看看?
来源: 互联网 发布时间:2015-02-27
本文导语: 实现接口其实就是继承一种以常量和抽象方法组成的特殊类。 下面这段代码实现了两个接口,但是只用到了两个事件处理方法。必须把两个接口中所有的抽象方法全部写出来,但是却没有为它们书写方法体,编译通...
实现接口其实就是继承一种以常量和抽象方法组成的特殊类。
下面这段代码实现了两个接口,但是只用到了两个事件处理方法。必须把两个接口中所有的抽象方法全部写出来,但是却没有为它们书写方法体,编译通过,为什么?
难道写个空的{}就算继承了吗?
import java.awt.*;
import java.awt.event.*;
class SL275 implements MouseMotionListener,MouseListener
{
private Frame f;
private TextField tf;
public SL275() {
f = new Frame("Two listeners example");
tf = new TextField(30);
}
public void launchFrame() {
Label label = new Label("Click and drag the mouse");
// Add components to the frame
f.add(label, BorderLayout.NORTH);
f.add(tf, BorderLayout.SOUTH);
// Add this object as a listener
f.addMouseMotionListener(this);
f.addMouseListener(this);
// Size the frame and make it visible
f.setSize(300, 200);
f.setVisible(true);
}
// These are MouseMotionListener events
public void mouseDragged(MouseEvent e) {
String s = "Mouse dragging: X = " + e.getX()
+ " Y = " + e.getY();
tf.setText(s);
}
public void mouseEntered(MouseEvent e) {
String s = "The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e) {
String s = "The mouse has left the building";
tf.setText(s);
}
// Unused MouseMotionListener method.
// All methods of a listener must be present in the
// class even if they are not used.
public void mouseMoved(MouseEvent e) { }
// Unused MouseListener methods.
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String args[]) {
TwoListener two = new TwoListener();
two.launchFrame();
}
}
下面这段代码实现了两个接口,但是只用到了两个事件处理方法。必须把两个接口中所有的抽象方法全部写出来,但是却没有为它们书写方法体,编译通过,为什么?
难道写个空的{}就算继承了吗?
import java.awt.*;
import java.awt.event.*;
class SL275 implements MouseMotionListener,MouseListener
{
private Frame f;
private TextField tf;
public SL275() {
f = new Frame("Two listeners example");
tf = new TextField(30);
}
public void launchFrame() {
Label label = new Label("Click and drag the mouse");
// Add components to the frame
f.add(label, BorderLayout.NORTH);
f.add(tf, BorderLayout.SOUTH);
// Add this object as a listener
f.addMouseMotionListener(this);
f.addMouseListener(this);
// Size the frame and make it visible
f.setSize(300, 200);
f.setVisible(true);
}
// These are MouseMotionListener events
public void mouseDragged(MouseEvent e) {
String s = "Mouse dragging: X = " + e.getX()
+ " Y = " + e.getY();
tf.setText(s);
}
public void mouseEntered(MouseEvent e) {
String s = "The mouse entered";
tf.setText(s);
}
public void mouseExited(MouseEvent e) {
String s = "The mouse has left the building";
tf.setText(s);
}
// Unused MouseMotionListener method.
// All methods of a listener must be present in the
// class even if they are not used.
public void mouseMoved(MouseEvent e) { }
// Unused MouseListener methods.
public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public static void main(String args[]) {
TwoListener two = new TwoListener();
two.launchFrame();
}
}
|
---------------难道写个空的{}就算继承了吗?
对,是这样的。。在实现接口时我们只需要实现自己关心部分的方法,而对于接口中的其它方法只要用{}就可以了,只不过实现方法为空
对,是这样的。。在实现接口时我们只需要实现自己关心部分的方法,而对于接口中的其它方法只要用{}就可以了,只不过实现方法为空
|
方法返回为void,当然只要写{}就行了。否则就return具体类型了。