简体   繁体   English

B类扩展了A类,但A类具有B类的属性

[英]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? 会不会有一个无限循环: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. 主要问题是我想以捆绑形式传递android中B类的实例,但这是行不通的,没有错误,只是数据没有通过。 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. 在任何情况下都不会发生错误,因为B对象只是A的私有字段,并且没有初始化。 but, if you create a new B instance, you only create a new object A(). 但是,如果创建一个新的B实例,则只能创建一个新的对象A()。

if you want to use 如果你想使用
objectA.propB.propB.propB....... objectA.propB.propB.propB .......
you can get the NullPointException. 您可以获取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. 我没有在子类B上调用super(in)。因此,尽管没有错误,但是没有任何数据通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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