[英]Why prefer ListAdapter over ArrayAdapter?
我看到一個視頻使用
ListAdapter theAdapter = new ArrayAdapter()
而不是ArrayAdapter theAdapter = new ArrayAdapter()
。 為什么?當構造函數未構造
ListAdapter
的實例時,為什么ListAdapter theAdapter = new ArrayAdapter()
有效?
我會給你一個簡單的例子。 它的所有繼承和父子的事情。 研究下面給出的代碼,你會得到一切。
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
class Test
{
public static void main(String[] args)
{
Animal a = new Animal();
Dog d = new Dog();
Animal a2 = d; // OK, since Dog IS-A Animal
Dog d2 = a; // not OK; what if a is a Cat?
}
}
所以父類的對象可以容納子類的對象。 反過來就不成立了。
在您的情況下, ListAdapter
是父接口, ArrayAdapter
是它的間接子類。
您可以在此處查看完整的層次結構ListAdater和ArrayAdapter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.