Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想检查我的输出是否正确。还有,有关此“getClass()”方法的更多详细信息_Java_Inheritance_Instantiation - Fatal编程技术网

Java 我想检查我的输出是否正确。还有,有关此“getClass()”方法的更多详细信息

Java 我想检查我的输出是否正确。还有,有关此“getClass()”方法的更多详细信息,java,inheritance,instantiation,Java,Inheritance,Instantiation,我刚开始学习Java,遇到了这个问题。我不确定我的输出是否正确。我还想在此上下文中了解更多关于“getClass”方法的详细信息,因为“Orc”和“Unicorn”不是从同一个类继承的 我的输出是: 神话 嘶叫 不 Raaa! 对吗 最后一个问题: 子类的对象是父类的实例吗? 您的代码工作正常,输出也正确 最后一个问题: 子类的对象是父类的实例吗 为了弄清楚上面的问题,你需要学习继承的概念。 请考虑以下代码。我已在评论中解释 class Super { public void t

我刚开始学习Java,遇到了这个问题。我不确定我的输出是否正确。我还想在此上下文中了解更多关于“getClass”方法的详细信息,因为“Orc”和“Unicorn”不是从同一个类继承的

我的输出是:

神话 嘶叫 不 Raaa! 对吗

最后一个问题:

子类的对象是父类的实例吗?
您的代码工作正常,输出也正确

最后一个问题:

子类的对象是父类的实例吗

为了弄清楚上面的问题,你需要学习继承的概念。 请考虑以下代码。我已在评论中解释

    class Super {
    public void test() {
        System.out.println("From Super Class TestMethod");
    }
}

class Sub extends Super {
    public void subTest() {
        System.out.println("From the Sub Class Test ");
    }
}

public class SubSuperCheck {

    public static void main(String[] args) {
        Sub s = new Sub();// when JVM find this, it will create the memory for
                            // the
                            // two methods test() from Super class and subTest()
                            // from Sub Class
                            // if the reference(Sub s) is also the Sub then we
                            // can access the two methods(test() and subTest())
                            // if the reference(Super s) then we are able to
                            // access the Super class method only.
        System.out.println(s.getClass());// getClass() method of Object class
                                            // will return the the class name
                                            // with which we created the object
                                            // here we have created the the
                                            // class with Sub. So that we will
                                            // get the out put as Sub
        s.subTest();// Sub class method
        s.test();// Super class method
        // if we create in the following way
        Super s2 = new Sub();
        // then we are able to access the Super class method only
        s2.test();// Okay
        s2.subTest();// we will get compilation error--The method subTest() is
                        // undefined for the type Super
    }

}
Unicorn x = new Unicorn( ); 
if (x instanceof Myth) {
   System.out.println("myth");
}
else {
   System.out.println("not myth"); 
}
x.makeNoise( );

Orc y = new Orc( );
if ( x.getClass( ) == y.getClass( ) ) { //I need more explanation on this 'getClass()' method
   System.out.println("yes");
}
else {
   System.out.println("no");
}
y.makeNoise( );
    class Super {
    public void test() {
        System.out.println("From Super Class TestMethod");
    }
}

class Sub extends Super {
    public void subTest() {
        System.out.println("From the Sub Class Test ");
    }
}

public class SubSuperCheck {

    public static void main(String[] args) {
        Sub s = new Sub();// when JVM find this, it will create the memory for
                            // the
                            // two methods test() from Super class and subTest()
                            // from Sub Class
                            // if the reference(Sub s) is also the Sub then we
                            // can access the two methods(test() and subTest())
                            // if the reference(Super s) then we are able to
                            // access the Super class method only.
        System.out.println(s.getClass());// getClass() method of Object class
                                            // will return the the class name
                                            // with which we created the object
                                            // here we have created the the
                                            // class with Sub. So that we will
                                            // get the out put as Sub
        s.subTest();// Sub class method
        s.test();// Super class method
        // if we create in the following way
        Super s2 = new Sub();
        // then we are able to access the Super class method only
        s2.test();// Okay
        s2.subTest();// we will get compilation error--The method subTest() is
                        // undefined for the type Super
    }

}