java的继承属性存在疑问 [英] java's inheritance property doubt

查看:52
本文介绍了java的继承属性存在疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中编写了2个简单的程序。具体如下:



  public   class  simple {
public static void main( String [] args){
System。 out .println( Hello World);
}
}







 import javax.swing.JOptionPane中; 
public class simple1扩展了JOptionPane {
public static void main( String [] args){
JOptionPane.showMessageDialog( null Hello World);
}
}







是的。这些都适用于适当的输出。现在,当我在命令提示符下执行以下语句(获取类文件的内容)时,






 javap simple 
编译来自 < span class =code-string> simple.java
public class simple extends java.lang。 Object {
public simple();
public static void main (java.lang。 String []);
}





 javap simple1 
编译来自 simple1.java
public class simple1扩展javax.swing.JOptionPane {
public simple1();
public static void main (java.lang。 String []);
}





现在这些是我不明白的事情:



I)Books(Deitel和Deitel)说java.lang.Object是在每个程序中自动继承的,但是第二个类文件没有显示它?如果确实如此,那么java应该支持多重继承。有一种隐藏的机制。它是什么?



II)java中的父类是什么?

解决方案

引用:

书籍(Deitel和Deitel)说java.lang.Object在每个程序中自动继承

我会说:每个类继承来自 java.lang.Object 。这是真的。



Quote:

但是第二个类文件没有显示它?

因为它只显示 simple1 的直接超类。后者继承自 java.lang.Object 通过 JOptionPane (可能还有其他中间类,即你有一个继承链)。这种机制不是多重继承:当一个类直接继承两个(或更多)超类时,你有多重继承。





< blockquote class =FQ>

Quote:

java中的父类是什么?

父类是类的直接祖先(在继承链中)( super 提供了一种访问它的方法,在 Java 中。


I have written 2 simple programs in java. These are as follows :

public class simple{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}




import javax.swing.JOptionPane;
public class simple1 extends JOptionPane{
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null,"Hello World");
    }
}




Yes . these are working just fine with the appropriate output. Now,

when I execute the following statements in my command prompt(to get the content of class file),

javap simple
Compiled from "simple.java"
public class simple extends java.lang.Object{
    public simple();
    public static void main(java.lang.String[]);
}



javap simple1
Compiled from "simple1.java"
public class simple1 extends javax.swing.JOptionPane{
    public simple1();
    public static void main(java.lang.String[]);
}



Now these are the things which I don't understand:

I) Books (Deitel and Deitel) say that java.lang.Object is automatically inherited in EVERY program , but the second class file doesn't show it? If it does then java should support multiple inheritance. There is some kind of a hidden mechanism. What is it?

II) What is the parent class in java?

解决方案

Quote:

Books (Deitel and Deitel) say that java.lang.Object is automatically inherited in EVERY program

I would say: "every class inherits from java.lang.Object". That's true.

Quote:

but the second class file doesn't show it?

Because it shows just the immediate super class of simple1. The latter inherits from java.lang.Object via JOptionPane (and probably other intermediate classes, that is you have an inheritance chain). This mechanism is NOT multiple inheritance: you have multiple inheritance when a class directly inherits from two (or more) super classes.


Quote:

What is the parent class in java?

The parent class is the direct ancestor (in the inheritance chain) of a class (super provides a way for accessing it, in Java).


这篇关于java的继承属性存在疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆