public interface A { void test();//抽象方法 } public class B implements A{ //实现类必须实现接口的所有抽象方法 public void test(){
} }
public interface Shape { double Getarea(double length);}public class Circle implements Shape { private final double PI = 3.14; public double Getarea(double length) { return PI * length * length / 4; }}public class Test { public static void main(String[] args) { Circle S = new Circle(); System.out.println(S.Getarea(6)); } } 定义一个接口为Shape,在Circle类中继承接口Shape。和抽象一样,子类的方法要么也为抽象方法,要么覆盖父类中的方法。 在接口中则需要覆盖在接口中定义的方法,例如Getarea方法,利用接口实现多态性比用抽象类更好。
若干抽象方法的集合。抽象方法通过实现类实现。 public interface testInterface { void test1(); void test2(); void test3(); } public testClass implements testInterface { public void test1() { System.out.println("test1"); } public void test2() { System.out.println("test2"); } public void test3() { System.out.println("test3"); }}
void test();//抽象方法
}
public class B implements A{
//实现类必须实现接口的所有抽象方法
public void test(){
}
}
public interface Shape {
double Getarea(double length);}public class Circle implements Shape {
private final double PI = 3.14; public double Getarea(double length) {
return PI * length * length / 4;
}}public class Test {
public static void main(String[] args) {
Circle S = new Circle();
System.out.println(S.Getarea(6));
}
}
定义一个接口为Shape,在Circle类中继承接口Shape。和抽象一样,子类的方法要么也为抽象方法,要么覆盖父类中的方法。
在接口中则需要覆盖在接口中定义的方法,例如Getarea方法,利用接口实现多态性比用抽象类更好。
public interface testInterface {
void test1();
void test2();
void test3();
}
public testClass implements testInterface {
public void test1() {
System.out.println("test1");
}
public void test2() {
System.out.println("test2");
} public void test3() {
System.out.println("test3");
}}