java--接口(interface\implements)

/**
 * 接口:
 * 特殊的抽象类,只包含抽象方法
 * 由interface定义
 * 只能包含常量和抽象方法
 *接口不能被实例化
 *接口需要被实现/继承的,实现类/子类;必须重写接口里面的所有抽象方法、
 *一个类可以实现多个接口,但是必须重写所有抽象方法
 *若又继承又实现,必须先继承后实现
 *接口可以继承接口,抽象类可以实现接口
 *
 *接口可以作为类型声明变量,一个接口类型的变量可以引用实现了该接口的实现类的对象,变量可以调用该接口中的方法(实现类提供方法的具体实现)
 *extends---同类型的                              类和类  接口和接口
 *implements---不同类型的                    类和接口
 *
 */
public class interfanceDemo {
    public static void main(String[] args) {
             Inter2 ob=new Boo();//向上造型
    }
}

interface Inter1{
    public static final int NULL=34;//常量
    public abstract void test();//抽象方法
    double d=1.2;//默认添加public static final
//  double dd;//错误,常量声明时同时初始化
    void tess();//默认添加public abstract
}
interface Inter2 {
    void tess();
}
class Foo {

}
class Ao extends Foo implements Inter1,Inter2{
    public void test() {//必须用public 修饰

    }
    public void tess(){//必须用public 修饰

    }
}
class Boo implements Inter2 {

    @Override
    public void tess() {
        // TODO Auto-generated method stub

    }

}

子接口与父接口:

public class test1 {
    public static void main(String[] args) {
            Inter5 aa=new AAo();//向上造型 子接口
            aa.tee();//访问父接口、子接口里面抽象方法
            aa.test();
            aa.tee(10);
            aa.show();
            Inter3 aa1=new AAo();//向上造型 父接口
            aa1.test();//只能访问父接口里面的抽象方法
            aa1.show();
    }
}
interface Inter3 {//父接口
    public static final int NULL = 12;
    public void show();
    int test();
}
interface Inter5 extends Inter3 {//子接口
    int test();
    void tee(int a);
    void tee();
}
class AAo implements Inter5 {//实现子接口
    public void show() {}
    public int test() {return 0;}
    public void tee(int a) {}
    public void tee() {}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰河家园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值