Android 如果LocationListener是一个接口,为什么它有一个构造函数?
我已经看到,接口中不允许使用构造函数,但这是如何允许的 locationListener=新locationListener(){Android 如果LocationListener是一个接口,为什么它有一个构造函数?,android,constructor,interface,locationlistener,Android,Constructor,Interface,Locationlistener,我已经看到,接口中不允许使用构造函数,但这是如何允许的 locationListener=新locationListener(){ etc}是的,您是对的,接口不能有构造函数,但您描述的是匿名类。在这一行中,您正在创建扩展LocationListener(它的实现在花括号之间)的新类的对象,该对象没有名称。 如果您想了解更多关于匿名类的信息,请查看此处:是的,您是对的接口不能有构造函数,但您描述的是匿名类。在这一行中,您正在创建扩展LocationListener(它的实现在花括号之间)的新类的
etc}是的,您是对的,接口不能有构造函数,但您描述的是匿名类。在这一行中,您正在创建扩展LocationListener(它的实现在花括号之间)的新类的对象,该对象没有名称。
如果您想了解更多关于匿名类的信息,请查看此处:是的,您是对的接口不能有构造函数,但您描述的是匿名类。在这一行中,您正在创建扩展LocationListener(它的实现在花括号之间)的新类的对象,该对象没有名称。
如果您想了解更多关于匿名类的信息,请查看此处:这是匿名类方法。为了清楚地说,这里有一个例子
interface Animal {
public void cry();
}
要创建动物实例的对象,首先需要实现动物接口
class Lion implements Animal {
public void cry() {
System.out.println("Roar");
}
}
然后使用常用方法创建对象:
Animal theLion = new Lion();
另一种方法是使用匿名类创建动物对象
Animal theTiger = new Animal() {
public void cry() {
System.out.println("Grrr");
}
}
现在,两个对象都应该能够调用cry
方法,如下所示:
theLion.cry();
theTiger.cry();
干杯 这是匿名类方法。为了说明这一点,这里有一个例子
interface Animal {
public void cry();
}
要创建动物实例的对象,首先需要实现动物接口
class Lion implements Animal {
public void cry() {
System.out.println("Roar");
}
}
然后使用通常的方法创建一个对象:
Animal theLion = new Lion();
另一种方法是使用匿名类创建动物对象
Animal theTiger = new Animal() {
public void cry() {
System.out.println("Grrr");
}
}
现在,两个对象都应该能够调用cry
方法,如下所示:
theLion.cry();
theTiger.cry();
干杯 感谢您的回复,但是为什么它需要构造函数(LocationListener())?感谢您的回复,但是为什么它需要构造函数(LocationListener())?感谢您的回复,但是为什么它需要构造函数(Animal())如果接口中没有定义构造函数?因为匿名类是声明Lion类之类的类的缩写。谢谢你的回复,但为什么它需要构造函数(Animal())如果接口中没有定义构造函数?因为匿名类是声明Lion类之类的类的缩写。