为什么方法 addMouseListener 不需要 super?
Why method addMouseListener doesn't need a super?
我的问题是,当我创建一个从 JPanel 继承的 class 时,为什么不使用 super.addMouseListener() 添加监听器?我认为这个方法在超级 class 中,也就是 JPanel。
这是代码:
private class DrawPanel extends JPanel
{
private int prefwid, prefht;
// Initialize the DrawPanel by creating a new ArrayList for the images
// and creating a MouseListener to respond to clicks in the panel.
public DrawPanel(int wid, int ht)
{
prefwid = wid;
prefht = ht;
chunks = new ArrayList<Mosaic>();
// Add MouseListener to this JPanel to respond to the user
// pressing the mouse. In your assignment you will also need a
// MouseMotionListener to respond to the user dragging the mouse.
addMouseListener(new MListen());
}
因为没有必要。
您没有在 DrawPanel
class 中声明方法 addMouseListener
,因此编译器会检查 superclasses 中是否存在这样的方法,并在java.awt.Component
。因为这个方法是被DrawPanel
class继承的,所以在这里调用就可以了。
如果您想深入了解原因,您需要阅读 JLS Sec 15.12, "Method Invocation Expressions"。然而,这并不完全是轻松阅读。
我认为关键的句子是:
For the class or interface to search, there are six cases to consider, depending on the form that precedes the left parenthesis of the MethodInvocation:
If the form is MethodName, that is, just an Identifier, then:
- If the Identifier appears in the scope of a visible method declaration with that name (§6.3, §6.4.1), then:
- If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. The class or interface to search is T.
- ...
所以 T
是 DrawPanel
。
The class or interface determined by compile-time step 1 (§15.12.1) is searched for all member methods that are potentially applicable to this method invocation; members inherited from superclasses and superinterfaces are included in this search.
因此在 DrawPanel
及其所有超classes 中搜索名为 addMouseListener
的方法。
我的问题是,当我创建一个从 JPanel 继承的 class 时,为什么不使用 super.addMouseListener() 添加监听器?我认为这个方法在超级 class 中,也就是 JPanel。 这是代码:
private class DrawPanel extends JPanel
{
private int prefwid, prefht;
// Initialize the DrawPanel by creating a new ArrayList for the images
// and creating a MouseListener to respond to clicks in the panel.
public DrawPanel(int wid, int ht)
{
prefwid = wid;
prefht = ht;
chunks = new ArrayList<Mosaic>();
// Add MouseListener to this JPanel to respond to the user
// pressing the mouse. In your assignment you will also need a
// MouseMotionListener to respond to the user dragging the mouse.
addMouseListener(new MListen());
}
因为没有必要。
您没有在 DrawPanel
class 中声明方法 addMouseListener
,因此编译器会检查 superclasses 中是否存在这样的方法,并在java.awt.Component
。因为这个方法是被DrawPanel
class继承的,所以在这里调用就可以了。
如果您想深入了解原因,您需要阅读 JLS Sec 15.12, "Method Invocation Expressions"。然而,这并不完全是轻松阅读。
我认为关键的句子是:
For the class or interface to search, there are six cases to consider, depending on the form that precedes the left parenthesis of the MethodInvocation:
If the form is MethodName, that is, just an Identifier, then:
- If the Identifier appears in the scope of a visible method declaration with that name (§6.3, §6.4.1), then:
- If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. The class or interface to search is T.
- ...
所以 T
是 DrawPanel
。
The class or interface determined by compile-time step 1 (§15.12.1) is searched for all member methods that are potentially applicable to this method invocation; members inherited from superclasses and superinterfaces are included in this search.
因此在 DrawPanel
及其所有超classes 中搜索名为 addMouseListener
的方法。