class Car
{
class Wheel
{
}
}class PlaneWheel extends Car.Wheel
{
PlaneWheel(Car car)
{
car.super(); // 这句怎么理解?调用 car 的父类构造函数? 调用Object 构造函数?!
}
public static void main(String[] args)
{
Car car=new Car();
PlaneWheel pw=new PlaneWheel(car);
}
}
{
class Wheel
{
}
}class PlaneWheel extends Car.Wheel
{
PlaneWheel(Car car)
{
car.super(); // 这句怎么理解?调用 car 的父类构造函数? 调用Object 构造函数?!
}
public static void main(String[] args)
{
Car car=new Car();
PlaneWheel pw=new PlaneWheel(car);
}
}
调用Car.Wheel的构造函数
只能且必须用car.super(),而不能用super(),或忽略不写。
greathawker() ( ) 信誉:100 Blog 加为好友 2007-5-11 16:26:07 得分: 0
调用Car.Wheel的构造函数
只能且必须用car.super(),而不能用super(),或忽略不写。**************************************************
刚做了测试,确实调用Car.Wheel的构造函数,但难以理解。
Car.Wheel
这个内部类是被继承的,他的构造器调用是肯定会在PlaneWheel构造器调用之前就开始调用.可以参考Thinking in java第7章的多态,里面有构造器的调用顺序.greathawker
调用Car.Wheel的构造函数
只能且必须用car.super(),而不能用super(),或忽略不写。我用Eclipe调式了一下,鉴定是调用的Car.Wheel
{
public Car(){
System.out.println("Car");
}
class Wheel{
public Wheel(){
System.out.println("Wheel");
}
}
}public class PlaneWheel extends Car.Wheel{
PlaneWheel(Car car){
car.super();
System.out.println("PlaneWheel");
}
public static void main(String[] args){
Car car=new Car();
PlaneWheel pw=new PlaneWheel(car);
}
}------------------------
结果:
Car
Wheel
PlaneWheel
哪个参数调用,直接找到其父类就好了
为什么这里是用super而不是用child
{
public Car(){
System.out.println("Car");
}
class Wheel{
public Wheel(){
System.out.println("Wheel");
}
}
}public class PlaneWheel extends Car.Wheel{
PlaneWheel(Car car){
car.super();
System.out.println("PlaneWheel");
}
public static void main(String[] args){
Car car=new Car();
PlaneWheel pw=new PlaneWheel(car);
}
}------------------------
结果:
Car
Wheel
PlaneWheel这样测试是不行的....没看偶说的么...构造器有个调用循序的问题....他一定会执行父类的构造器的..
enclosing class object, things are slightly complicated when you inherit
from an inner class. The problem is that the “secret” reference to the
enclosing class object must be initialized, and yet in the derived class
there’s no longer a default object to attach to. The answer is to use a
syntax provided to make the association explicit: //: c08:InheritInner.java
// Inheriting an inner class.
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~
You can see that InheritInner is extending only the inner class, not the
outer one. But when it comes time to create a constructor, the default one
is no good and you can’t just pass a reference to an enclosing object. In
addition, you must use the syntax FeedbackenclosingClassReference.super();inside the constructor. This provides the necessary reference and the
program will then compile.其实这段我自己都还没看到,我正好看到这章,看了下目录有讲这个的...就copy来咯.