如题。问题:接口可以继承接口吗?如果可以应该怎么做?
我有个程序如下:
interface C{
void method1();
}
interface D{
void method2();
}
public class A implements D{
void method1(){ //编译时这里不提示出错。
System.out.println("method1 of interface C ");
}
public void method2(){ //这里必须使用public修饰符
System.out.println("method2 of interface D");
}
public static void main(String args[]){
A a1=new A();
a1.method1();
a1.method2();
}
}
我有个程序如下:
interface C{
void method1();
}
interface D{
void method2();
}
public class A implements D{
void method1(){ //编译时这里不提示出错。
System.out.println("method1 of interface C ");
}
public void method2(){ //这里必须使用public修饰符
System.out.println("method2 of interface D");
}
public static void main(String args[]){
A a1=new A();
a1.method1();
a1.method2();
}
}
但你现在的这个有错:public class A implements D{}
这个是类继承接口呀,楼上要注意!!!
interface C{
void method1();
}
interface D extends C{
void method2();
}
public class A implements D{
public void method1(){ //这里方法前要加入public修饰符,不然会提示出错。
System.out.println("method1 of interface C ");
}
public void method2(){ //这里必须使用public修饰符
System.out.println("method2 of interface D");
}
public static void main(String args[]){
A a1=new A();
a1.method1();
a1.method2();
}
}
它内的方法也可以使用!