当前位置: 技术问答>java相关
这个程序运行的结果是什么?
来源: 互联网 发布时间:2015-02-10
本文导语: 请你不要编译运行,看看运行结果是什么,并说明理由? class vehicle { public int speed; public vehicle () { System.out.println("in the vehicle constructor"); speed = 10; } public void run() { System.out.println("The speed is : " +...
请你不要编译运行,看看运行结果是什么,并说明理由?
class vehicle
{
public int speed;
public vehicle ()
{
System.out.println("in the vehicle constructor");
speed = 10;
}
public void run()
{
System.out.println("The speed is : " + speed);
}
}
public class Car extends vehicle
{
public int speed;
public Car()
{
System.out.println("in the Car constructor");
speed = 100;
}
public static void main( String[] args)
{
Car a = new Car();
a.run();
}
};
class vehicle
{
public int speed;
public vehicle ()
{
System.out.println("in the vehicle constructor");
speed = 10;
}
public void run()
{
System.out.println("The speed is : " + speed);
}
}
public class Car extends vehicle
{
public int speed;
public Car()
{
System.out.println("in the Car constructor");
speed = 100;
}
public static void main( String[] args)
{
Car a = new Car();
a.run();
}
};
|
我看得不太明白,高手看明白了给小弟解释一下
Variables can also be overridden, it’s known as shadowing or hiding. But, member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since it’s already resolved at compile time based on the reference variable type. Only methods are resolved at run-time.
Also, methods access variables only in context of the class of the object they belong to. If a sub-class method calls explicitly a super class method, the super class method always will access the super-class variable. Super class methods will not access the shadowing variables declared in subclasses because they don’t know about them. (When an object is created, instances of all its super-classes are also created.) But the method accessed will be again subject to dynamic lookup. It is always decided at runtime which implementation is called. (Only static methods are resolved at compile-time)
Variables can also be overridden, it’s known as shadowing or hiding. But, member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since it’s already resolved at compile time based on the reference variable type. Only methods are resolved at run-time.
Also, methods access variables only in context of the class of the object they belong to. If a sub-class method calls explicitly a super class method, the super class method always will access the super-class variable. Super class methods will not access the shadowing variables declared in subclasses because they don’t know about them. (When an object is created, instances of all its super-classes are also created.) But the method accessed will be again subject to dynamic lookup. It is always decided at runtime which implementation is called. (Only static methods are resolved at compile-time)
|
in the vehicle constructor
in the Car constructor
The speed is : 10
in the Car constructor
The speed is : 10
|
in the vehicle constructor
in the Car constructor
The speed is : 10
|
in the vehicle constructor 在子类构造的时候先调用父类的构造器
in the car constructor 子类构造器中的打印
The speed is :10 run()来自vehicle,speed初始为10
如果你在Car类中覆盖来自vehicle的run()方法,那么打印speed的结果就是100了,比如在Car类中加入:
public void run()
{
System.out.println("The speed is:"+speed);
}
in the car constructor 子类构造器中的打印
The speed is :10 run()来自vehicle,speed初始为10
如果你在Car类中覆盖来自vehicle的run()方法,那么打印speed的结果就是100了,比如在Car类中加入:
public void run()
{
System.out.println("The speed is:"+speed);
}