您将引用的类型与被引用对象的类型混淆了。
发生了什么
将一个类实例化为一个对象和拥有一个给定类型的引用是两件不同的事情:
另一种看待方式:
在代码中这意味着:
MyInterface i; // This is valid, only says that the type of i is MyInterface
i = new MyInterface(); // This is not valid, cannot instantiate the interface
您可以阅读有关引用类型和对象类型之间的区别here。
示例
举个例子,Integer 类扩展了Number 类并实现了Serializable 类:
Integer i = new Integer(1); // The object referenced by i is of type Integer, forever
// i is a reference to that object,
// its type is a reference to Integer
Number n = i; // Now n is also referencing the same object.
// The type of n is a reference to a Number.
// The referenced object hasn't changed, its type is still Integer
// This is possible because Number is a supertype of Integer
Serializable s = i; // Same, s is now referencing the same object.
// The object is still the same, its type hasn't changed
// The type of s is a reference to a Serializable.
// This is possible because Serializable is a supertype of Integer
适用于您的案例
构造函数定义
public LocationProvider(Context context, LocationCallback callback)
要求第二个参数是对LocationCallback 的引用。
这并不意味着被引用的对象应该是那种类型,事实上这是不可能的。这只意味着传递的引用应该是LocationCallback的子类型,最终引用的对象的类型是实现LocationCallback的类。