Java动手动脑04
1.运行代码
class Grandparent
{
public Grandparent()
{
System.out.println("GrandParent Created.");
}
public Grandparent(String string)
{
System.out.println("GrandParent Created.String:" + string);
}
}
class Parent extends Grandparent
{
public Parent()
{
//super("Hello.Grandparent.");
System.out.println("Parent Created");
//super("Hello.Grandparent.");
}
}
class Child extends Parent
{
public Child()
{
System.out.println("Child Created");
}
}
public class TestInherits
{
public static void main(String args[])
{
Child c = new Child();
}
}
TestInherits.java

结论:
1、子类的构造方法在执行之前,必须先调用父类的构造方法
2、通过 super 调用父类构造方法,super必须是子类构造方法中编写的第一个语句
3、super本身就是调用父类的构造方法,而且可以执行父类被隐藏的成员变量和被覆盖的父类成员方法
4.Java构造函数的调用是按照顺序来的,如果不先构造父类,又何来子类,子类是继承父类的,首先得知道父类里面有什么,才能继承,其次,父类只有自己的东西,子类的元素父类是不知道的,所以,先构造子类是不和语法的。
思索:为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来?
答:不能,子类拥有父类的成员变量和成员方法,如果不调用父类的构造方法,则从父类继承而来的成员变量和成员方法不能进行初始化。而不能反过来调用原因是,因为父类不能知道子类有什么成员变量和成员方法并且这样做子类不能在父类帮助下完成继承来的成员变量的初始化,导致成员变量无法初始化,程序就会报错,无法执行。
2.
package Test;
public class ExplorationJDKSource {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new A());
}
}
class A{}
ExplorationJDKSource.java

总结:其实类可以做到输出,是因为我们的写每一个类,都是默认继承Object类!所以Object类里的方法名称都得到了继承!而System.out实际上是Object的一个子类PrintStream,而这个子类内部定义了一个public static void println(String ...);的方法传进来的大类参数就会调用父类的toString方法,改成String类型并输出。而该方法如下: public String toString() { return getClass().getName() +"@" + Integer.toHexString(hashCode()); } hashCode方法是本地方法,由JVM设计者实现: public native int hashCode();所以出现上述结果。
程序中,main方法实际上调用的是: public void println(Object x),这一方法内部调用了String类的valueOf方法。
valueOf方法的内部又调用Object.toString方法
而Object.toString方法的代码是:
public String toString(){
return getClass().getName() +"@" + Integer.toHexString(hashCode());
}
hashCode方法是本地方法,是JVM的设计者实现的: public native int hashCode();
3.
class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}
public class TestCast
{
public static void main(String args[])
{
Mammal m;
Dog d=new Dog();
Cat c=new Cat();
m=d;
//d=m;
d=(Dog)m;
//d=c;
c=(Cat)m;
}
}
d=m错误:不能从Mammal类转换到Dog类;
d=c错误:不能从Cat类转换到Dog类;
c=(Cat)m正确:
4.
public class ParentChildTest {
public static void main(String[] args) {
Parent parent=new Parent();
parent.printValue();
Child child=new Child();
child.printValue();
parent=child;
parent.printValue();
parent.myValue++;
parent.printValue();
((Child)parent).myValue++;
parent.printValue();
}
}
class Parent{
public int myValue=100;
public void printValue() {
System.out.println("Parent.printValue(),myValue="+myValue);
}
}
class Child extends Parent{
public int myValue=200;
public void printValue() {
System.out.println("Child.printValue(),myValue="+myValue);
}
}

原因:
前两行正常输出,父类对象调用父类的方法,子类对象调用子类的方法;
第三行,当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定;
第四行,当parent=child;仅仅是将parent中有的方法用child的方法代替,所以parent.myValue++;而输出的是child的printValue(),而printValue()方法中输出的是child.myValue,所以输出的是child.myValue;
第五行,强制类型转换,++作用在child的myValue,所以输出的也是child的myValue,而不是parent的myValue;
结论:
当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。这个特性实际上就是面向对象“多态”特性的具体表现。如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。如果子类被当作父类使用,则通过子类访问的字段是父类的!
5.在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。
class test{
void play() {
System.out.println("我是父类test");
}
}
class test11 extends test{
void play() {
super.play();
}
}
public class test1
{
public static void main(String[] args) {
test11 t=new test11();
t.play();
}
}

6.
package Test;
public class TestInstanceof
{
public static void main(String[] args)
{
//声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类
//但hello变量的实际类型是String
Object hello = "Hello";
//String是Object类的子类,所以返回true。
System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));
//返回true。
System.out.println("字符串是否是String类的实例:" + (hello instanceof String));
//返回false。
System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));
//String实现了Comparable接口,所以返回true。
System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));
String a = "Hello";
//String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过
//System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));
}
}

7.
package second;
class Parent
{
public int value=100;
public void Introduce()
{
System.out.println("I'm father");
}
}
class Son extends Parent
{
public int value=101;
public void Introduce()
{
System.out.println("I'm son");
}
}
class Daughter extends Parent
{
public int value=102;
public void Introduce()
{
System.out.println("I'm daughter");
}
}
public class TestPolymorphism
{
public static void main(String args[])
{
Parent p=new Parent();
p.Introduce();
System.out.println(p.value);
p=new Son();
p.Introduce();
System.out.println(p.value);
p=new Daughter();
p.Introduce();
System.out.println(p.value);
}
}


浙公网安备 33010602011771号