示例一、
--------------------------------------------------
class Fu
{
int num = 3;
void method1()
{
System.out.println("fu 1");
}
void method2()
{
System.out.println("fu 2");
}
static void method4()
{
System.out.println("fu 4");
}
}
class Zi extends Fu
{
int num = 5;
void method2()
{
System.out.println("zi 2");
}
void method3()
{
System.out.println("zi 3");
}
static void method4()
{
System.out.println("zi 4");
}
}
class DuoTaiDemo
{
public static void main(String[] args)
{
Fu f = new Zi();
f.method1();//fu 1
f.method2();//zi 2 多态时,方法看对象本身的方法
//f.method3();fu类中无method3
f.method4();//fu 4 静态方法与类绑定
System.out.println(f.num);//3 多态时,常量看的是对象所属类的常量
}
}
-------------------------------------------------
实例二、
--------------------------------------------------
class Super
{
int i=0;
public Super(String a)
{
System.out.println("A");
i=1;
}
public Super()
{
System.out.println("B");
i+=2;
}
}
class Demo extends Super
{
public Demo(String a)
{
//super();
System.out.println("C");
i=5;
}
public static void main(String[] args)
{
int i=4;
Super d=new Demo("A");
System.out.println(d.i);
}
}
-----------------------------------------------
示例一中f.num为3,看的是父类。而示例二中d.i为什么不看父类int i = 0?求解惑
|