class Fu
{
int num = 5;
void method1()
{
System.out.println("zi method_1");
}
void method2()
{
System.out.println("zi method_2");
}
static void method4()
{
System.out.println("fu method_4");
}
}
class Zi extends Fu
{
int num = 8;
void method1()
{
System.out.println("zi method_1");
}
void method3()
{
System.out.println("zi method_3");
}
static void method4()
{
System.out.println("zi method_4");
}
}
class DuoTaiDemo4
{
public static void main(String[] args)
{
Fu f = new Zi();
f.method4();
//在多态中,静态成员函数的特点:无论编译和运行,都参考左边。静态不进对象区Zi(),直接照引用f。
Zi z = new Zi();
z.method4();
/*
Fu f = new Zi();
System.out.println(f.num);
//打印结果为 5,在多态中,成员变量的特点:无论编译和运行,都参考左边(引用型变量所属的类)。
//静态方法一进内存就被绑定在了其所在的类中。
Zi z = new Zi();
System.out.println(z.num);
//打印结果8.
*/
/*
public static void main(String[] args)
{
Fu f = new Zi();
f.method1();
f.method2();
f.method3();
}
编译失败。把f.method3();注释掉就可以成功。
在多态中(非静态)成员函数的特点:
在编译时期:参阅引用型变量所属的类中是否有调用的方法。如果有,编译通过,没有则失败。
在运行时期:参阅对象所属的类中是否有调用的方法。
简单总结;成员函数在多态调用时,编译看左边,运行看右边。
*/
/*
Zi z = new Zi();
z.method1();
z.method2();
z.method3();
*/
}
}
{
int num = 5;
void method1()
{
System.out.println("zi method_1");
}
void method2()
{
System.out.println("zi method_2");
}
static void method4()
{
System.out.println("fu method_4");
}
}
class Zi extends Fu
{
int num = 8;
void method1()
{
System.out.println("zi method_1");
}
void method3()
{
System.out.println("zi method_3");
}
static void method4()
{
System.out.println("zi method_4");
}
}
class DuoTaiDemo4
{
public static void main(String[] args)
{
Fu f = new Zi();
f.method4();
//在多态中,静态成员函数的特点:无论编译和运行,都参考左边。静态不进对象区Zi(),直接照引用f。
Zi z = new Zi();
z.method4();
/*
Fu f = new Zi();
System.out.println(f.num);
//打印结果为 5,在多态中,成员变量的特点:无论编译和运行,都参考左边(引用型变量所属的类)。
//静态方法一进内存就被绑定在了其所在的类中。
Zi z = new Zi();
System.out.println(z.num);
//打印结果8.
*/
/*
public static void main(String[] args)
{
Fu f = new Zi();
f.method1();
f.method2();
f.method3();
}
编译失败。把f.method3();注释掉就可以成功。
在多态中(非静态)成员函数的特点:
在编译时期:参阅引用型变量所属的类中是否有调用的方法。如果有,编译通过,没有则失败。
在运行时期:参阅对象所属的类中是否有调用的方法。
简单总结;成员函数在多态调用时,编译看左边,运行看右边。
*/
/*
Zi z = new Zi();
z.method1();
z.method2();
z.method3();
*/
}
}