开发者

Inheritance between packages does not exist?

开发者 https://www.devze.com 2023-04-12 03:52 出处:网络
Pardon me for a stupid question but It recently dawned on me that I\'ve been doing things like this import java.awt.*;

Pardon me for a stupid question but It recently dawned on me that I've been doing things like this

import java.awt.*;
import java.awt.event.*;
                  ^
                  |

for quite some time and I've been thinking "is this really necessary?"

The code below won't compile. My question is why?

import开发者_Go百科 javax.swing.*;
import java.awt.*;

public class Calculator extends JFrame implements ItemListener, ActionListener {
    ...

More specifically, since I'm importing java.awt.*;, shouldn't I be able to implement the listeners without doing import java.awt.event.*;?


Inheritance between packages does not exist?

Correct. A package is just a namespace, which (in Java, at least) are not nested.

More specifically, since I'm importing java.awt.*;, shouldn't I be able to implement the listeners without doing import java.awt.event.*;?

The compile error you see clearly refutes this. Importing a package does not import its subpackages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号