接口(interface)是抽像类的变体。 可以定义,也不仅限于抽象类。只是接口本身不能被实例化,接口内部的类是可以实例化的。 调用接口的内部类有两种方法: 1.接口名.类名 2.创建实现类implements接口,再直接实例化内部类 示例代码: public interface Test{
class A{ public int a=1; }}--public class Program { public static void main(String[] args) { Test.A a = new Test.A(); System.out.println(a.a); new B().method(); } } class B implements Test{
可以定义,也不仅限于抽象类。只是接口本身不能被实例化,接口内部的类是可以实例化的。
调用接口的内部类有两种方法:
1.接口名.类名
2.创建实现类implements接口,再直接实例化内部类
示例代码:
public interface Test{
class A{
public int a=1;
}}--public class Program {
public static void main(String[] args) {
Test.A a = new Test.A();
System.out.println(a.a);
new B().method();
}
}
class B implements Test{
void method(){
System.out.println(new A().a);
}
}
PS:来自于百度知道
可以用来归并类,不过接口是方法的集合,类还是少定义吧
一开始我和你的想法一样,知道我接触了新公司的代码,我才发现有这么个东西,而且我要改造,所以就有了上面的问题。