interface 接口方法实现
定义接口animal
public interface Animal {
void cry();
int num();
}
接口中的方法在类中被实现
public class Dog implements Animal
{
public void cry()
{
System.out.println("汪汪汪");
}
public int num(){
return 100;
}
public static void main(String args[])
{
Dog a = new Dog();
a.cry();
}
}
接口中的方法必须全部实现,就是Dog类必须实现num()和cry()方法。如果不全部实现num 和 cry 方法 继承接口 可以使用抽象类 在子类中实现 譬如:
定义抽象类 猫科类 Felidea 继承接口Animal 并没有实现接口中的cry()和num()方法
public abstract class Felidae implements Animal
{
}
但是 如果我定义一个猫类 Cat类 继承猫科类Felidea 那么抽象类中继承Animal方法必须被实现 like this:public class Cat extends Felidae
{
public void cry()
{
System.out.println("喵喵喵");
}
public int num ()
{
return 1;
}
}
:)这样做 就跟抽象类中的方法 在抽象子类中不完全实现一样 等待子类进一步实现
可以 但是只要继承了抽象类或者使用接口的实体类 必须要实现所有的方法