为什么 class 扩展 JTextField 不能实例化 JPasswordField?
Why can't a class extending JTextField instantiate a JPasswordField?
我有一个空的 class MyJTextField 扩展 JTextField,我希望将 MyJTextField 初始化为 JPasswordField,就像 JTextField 一样。
JTextField pass = new JPasswordField(); //no errors
but
MyJTextField pass = new JPasswordField();//"Type mismatch: cannot convert from JPasswordField to MyJTextField"
您的 MyJTextField 是 JTextField 的 child。而 JPasswordField 也是 JTextField 的 child。但是 MyJTextField 不是 JPasswordField 的 child,所以 MyJTextField 不是 JPasswordField,它们是兄弟。
//you can do
JTextField field1 = new JPasswordField(); //child is an instance of parent
JTextField field2 = new MyJTextField(); //child is an instance of parent
//you can't do
JPasswordField field3 = new JTextField(); // parent is not instance of child
MyJTextField field4 = new JTextField(); // parent is not instance of child
JPasswordField field5 = new MyJTextField(); // siblings are not instances of each other
MyJTextField field6 = new JPasswordField(); // siblings are not instances of each other
我有一个空的 class MyJTextField 扩展 JTextField,我希望将 MyJTextField 初始化为 JPasswordField,就像 JTextField 一样。
JTextField pass = new JPasswordField(); //no errors
but
MyJTextField pass = new JPasswordField();//"Type mismatch: cannot convert from JPasswordField to MyJTextField"
您的 MyJTextField 是 JTextField 的 child。而 JPasswordField 也是 JTextField 的 child。但是 MyJTextField 不是 JPasswordField 的 child,所以 MyJTextField 不是 JPasswordField,它们是兄弟。
//you can do
JTextField field1 = new JPasswordField(); //child is an instance of parent
JTextField field2 = new MyJTextField(); //child is an instance of parent
//you can't do
JPasswordField field3 = new JTextField(); // parent is not instance of child
MyJTextField field4 = new JTextField(); // parent is not instance of child
JPasswordField field5 = new MyJTextField(); // siblings are not instances of each other
MyJTextField field6 = new JPasswordField(); // siblings are not instances of each other