gpt4 book ai didi

java - 为什么我们在弱引用对象实例(如 TextView )之后使用 get() 方法?

转载 作者:行者123 更新时间:2023-11-29 18:35:59 25 4
gpt4 key购买 nike

为什么我们在 WeakReference Text View Instance 之后使用 get() 方法?

private WeakReference<TextView> mTitleText;
private TextView mAuthorText;

FetchBook(TextView titleText, TextView authorText) {
this.mTitleText = new WeakReference<>(titleText);
this.mAuthorText = authorText;

//in weakPreference Text View
mTitleText.get().setText("hello");

//in standard text view
authorText.setText("by by ");
}

为什么没有get()方法不能直接在弱引用中设置textView的文本?

最佳答案

因为就编译器而言,类型 WeakReference<TextView>TextView 完全无关. WeakReferenceTextView 完全不同.

考虑这段代码:

Foo<T> {
private T bar;
public T get() { return bar; }

public Foo(T bar) {
this.bar = bar;
}
}

class Bar {
public void func() {}
}
...
Foo<Bar> foo = new Foo(new Bar());

你基本上是在问

Why can't I call foo.func() directly? Why do I have to call foo.get().func()?

因为 funcBar 中声明, 不在 Foo .和 foo.get()返回 Bar 的实例,您可以使用它来调用 func .

setTextTextView 中声明, 不在 WeakReference . WeakReference.get给你一个 TextView 的实例,您可以使用它来调用 setText .

关于java - 为什么我们在弱引用对象实例(如 TextView )之后使用 get() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com