class Pencil
{
String name;
Pencil(){}
Pencil(String name)
{
this.name = name;
}
public void write()
{
System.out.println("write quickly...");
}
}
interface Eraser
{
public static final String color = "白色";
public abstract void clean();
}
//()
class PencilWithEraser extends Pencil implements Eraser
{
PencilWithEraser(){}
PencilWithEraser(String name)
{
super(name);
}
//public
public void write()
{
System.out.println("super "+ super.name+ "PencilWithEraser's name...");
System.out.println("this "+ this.name+ "PencilWithEraser's name...");
}
//void clean()
public void clean()
{
System.out.println("ERROR, use Eraser...");
}
}
public class jiekou
{
public static void main(String[] args)
{
PencilWithEraser p = new PencilWithEraser("中华");
p.write();
p.clean();
System.out.println(p.color);
System.out.println(PencilWithEraser.color);
}
}
运行结果:
super 中华PencilWithEraser's name...
this 中华PencilWithEraser's name...
ERROR, use Eraser...
白色
白色
- 接口中的所有属性 默认的修饰符是 public static final。
- 接口中的所有方法 默认的修饰符是 public abstract。
- 类实现接口可以通过implements实现,实现接口的时候必须把接口中的所有方法实现,一个类可以实现多个接口。
- 接口中定义的所有的属性默认是public static final的,即静态常量既然是常量,那么定义的时候必须赋值。
- 接口中定义的方法不能有方法体。接口中定义的方法默认添加public abstract
- 有抽象函数的不一定是抽象类,也可以是接口类。
- 由于接口中的方法默认都是抽象的,所以不能被实例化。
- 对于接口而言,可以使用子类来实现接口中未被实现的功能函数。
- 如果实现类中要访问接口中的成员,不能使用super关键字。因为两者之间没有显示的继承关系,况且接口中的成员成员属性是静态的。可以使用接口名直接访问。
- 接口没有构造方法。