简体   繁体   中英

Class B extends Class A but class A has a property of class B

So I know this is confusing but why does this work? If I have

public class A implements Parcelable{
  private B propB;
}

public class B extends A implements Parcelable{
  private int stuff;
}

so if I ever created a

A objectA = new A(); 

wouldn't there be an infinite loop of this: objectA.propB.propB.propB?

the main issue is that I want to pass an instance of class B in android as a bundle, but this is not working, no error, just data doesn't get passed through. All the properties and data gets passed through except for that one property that seems to be a circular dependency..

is there a reason why this is good practice? or is it just not a good idea?

there is no error occurs in any case, because the B object only a private field of A, and is not initialization. but, if you create a new B instance, you only create a new object A().

if you want to use
objectA.propB.propB.propB.......
you can get the NullPointException.

The reason why data wasn't being passed through is because of parcels. I wasn't calling super(in) on the sub class B. so although there was no error there wasn't any data passing through either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM