我想在类中实现延迟加载的属性。我认为这很容易并且像这样实现:
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="nowrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:text="niledfdfdfhfgfdgdfgdfgfgfggdfgdfggggggggggggggggggggggggggggggggggggggggggggggfhssdsdsdsfgdfgfgfgfgfggfgdgdfgfgfgdsddjfhdkjfhjshdf" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:text="nilxcvxceshdfhd"
android:background="@color/colorAccent"
android:layout_height="wrap_content"
app:layout_minWidth="50dp"
app:layout_minHeight="20dp"
app:layout_alignSelf="center"
/>
</com.google.android.flexbox.FlexboxLayout>
但是现在我很惊讶,在 init 之后_bar变量包含了longTimeLoadedData甚至Foo.bar从未被调用过......有人可以向我解释这个行为吗?提前谢谢!
答案 0 :(得分:0)
看起来它已经像你想要的那样工作了:
class Foo:
def __init__(self):
self._bar = None
@property
def bar(self):
if not self._bar:
self._bar = 1
return self._bar
f = Foo()
print(f._bar) # None
f.bar
print(f._bar) # 1