class Base
{
String str="BaseStr";
String staticstr="Base static str";
void method()
{
System.out.print("BaseMethod");
}
static void staticMethod()
{
System.out.print("Base Static Method");
}
}
class Sub extends Base
{
String str="SubStr";
String staticstr="Sub static str";
void method()
{
System.out.print("SubMethod");
}
static void staticMethod()
{
System.out.print("Sub static Method");
}
}
public class BaseType_SubInstance
{
public static void main(String[] args)
{
Base x=new Sub();
System.out.println("x.str:"+x.str); //打印 x.str:BaseStr
System.out.println("x.staticstr:"+x.staticstr);//打印 x.staticstr:Base static str
System.out.print("x.method():");
x.method(); //打印 SubMethod
System.out.print("\nx.staticMethod():");
x.staticMethod(); //打印 Base Static Method
}
}
为什么结果打印结果会是那样?