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