当前位置: 技术问答>java相关
下面是一道Q&A,大家看一下
来源: 互联网 发布时间:2015-03-04
本文导语: 下面是一道Q&A,大家看一下 Q. When an instance of subclass is created, is an instance of base class created also? How many objects are created during the process? A: A short lessen on Inheritance: ~~~~~~~~~~~~~~~~~~~~~ Truth: FordCar ISA or...
下面是一道Q&A,大家看一下
Q. When an instance of subclass is created, is an instance of base class created also? How many objects are created during the process?
A:
A short lessen on Inheritance:
~~~~~~~~~~~~~~~~~~~~~
Truth: FordCar ISA or "is a" Car.
Think:
Which one is generalization/specialization? (Answer: Car is generalization)
Which one should be the base/sub class? (Answer: Car should be the base class)
Fact: A FordCar was just made.
Questions:
true/false? A Car was just made. (Answer: true)
How many objects (Cars) were created during this process? (Answer: one)
~~~~~~~~~~~~~~~~~~~~~
There is only one object, but it contains all of the variables for the entire hierarchy. The reason being that the execution of a constructor causes calls to the constructors all the way back up the hierarchy to java.lang.Object - as each constructor executes, it gets the memory and sets the variables as required for that class.
既然说,There is only one object,但是大家运行一下下面的程序,是编译通过的,注意一下带有注释的那句
public class SL275 extends SL110
{
SL275()
{
System.out.println("SL275's constructor executed");
}
public static void main(String[] args)
{
SL275 s=new SL275();
SL110.i=1;//这一句调用SL110编译通过,运行也通过
}
}
class SL110
{
static int i=110;
SL110()
{
System.out.println("SL110's constructor executed");
}
}
既然说只产生一个对象,那么为什么这个对象的父类对象也可以访问呢?(SL110.i=1;编译通过)。
我知道当一个构造器执行一次的时候,它的父类的构造器,父类的父类的构造器,。。。直到Object类的构造器都会被执行一次。由于构造器的作用大家都清楚,那么既然整个继承关系中的每个类的构造器都执行了一次,那么为什么说There is only one Object呢?
不理解,请高手解释
Q. When an instance of subclass is created, is an instance of base class created also? How many objects are created during the process?
A:
A short lessen on Inheritance:
~~~~~~~~~~~~~~~~~~~~~
Truth: FordCar ISA or "is a" Car.
Think:
Which one is generalization/specialization? (Answer: Car is generalization)
Which one should be the base/sub class? (Answer: Car should be the base class)
Fact: A FordCar was just made.
Questions:
true/false? A Car was just made. (Answer: true)
How many objects (Cars) were created during this process? (Answer: one)
~~~~~~~~~~~~~~~~~~~~~
There is only one object, but it contains all of the variables for the entire hierarchy. The reason being that the execution of a constructor causes calls to the constructors all the way back up the hierarchy to java.lang.Object - as each constructor executes, it gets the memory and sets the variables as required for that class.
既然说,There is only one object,但是大家运行一下下面的程序,是编译通过的,注意一下带有注释的那句
public class SL275 extends SL110
{
SL275()
{
System.out.println("SL275's constructor executed");
}
public static void main(String[] args)
{
SL275 s=new SL275();
SL110.i=1;//这一句调用SL110编译通过,运行也通过
}
}
class SL110
{
static int i=110;
SL110()
{
System.out.println("SL110's constructor executed");
}
}
既然说只产生一个对象,那么为什么这个对象的父类对象也可以访问呢?(SL110.i=1;编译通过)。
我知道当一个构造器执行一次的时候,它的父类的构造器,父类的父类的构造器,。。。直到Object类的构造器都会被执行一次。由于构造器的作用大家都清楚,那么既然整个继承关系中的每个类的构造器都执行了一次,那么为什么说There is only one Object呢?
不理解,请高手解释
|
bookbobby说的不错.
至于对象是只有一个.只是我认为.在子对象中包含一个它的父对象.所以地创建它时,必须调用它的父对象的构建语句来创建它所包含的它的父对象.
我对继承的理解是.第当从一个类继承时,就是在这个类上再包裹一层它自已的东西.如果说在创建它时不对它的父对象调用构建语句,哪么它所包的东西就不能用了.是空的了.所以实际上还是一个对象.只是在它里面还有它的祖先们.
至于对象是只有一个.只是我认为.在子对象中包含一个它的父对象.所以地创建它时,必须调用它的父对象的构建语句来创建它所包含的它的父对象.
我对继承的理解是.第当从一个类继承时,就是在这个类上再包裹一层它自已的东西.如果说在创建它时不对它的父对象调用构建语句,哪么它所包的东西就不能用了.是空的了.所以实际上还是一个对象.只是在它里面还有它的祖先们.
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。