接口

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...
白色
白色


  1. 接口中的所有属性 默认的修饰符是  public static final。
  2. 接口中的所有方法 默认的修饰符是  public abstract。
  3. 类实现接口可以通过implements实现,实现接口的时候必须把接口中的所有方法实现,一个类可以实现多个接口。
  4. 接口中定义的所有的属性默认是public static final的,即静态常量既然是常量,那么定义的时候必须赋值。
  5. 接口中定义的方法不能有方法体。接口中定义的方法默认添加public abstract
  6. 有抽象函数的不一定是抽象类,也可以是接口类。
  7. 由于接口中的方法默认都是抽象的,所以不能被实例化。
  8. 对于接口而言,可以使用子类来实现接口中未被实现的功能函数。
  9. 如果实现类中要访问接口中的成员,不能使用super关键字。因为两者之间没有显示的继承关系,况且接口中的成员成员属性是静态的。可以使用接口名直接访问。
  10. 接口没有构造方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值