java_抽象类_接口it

一朝天子一朝臣
abstract不能和final组合在一块

class AbstractDemo1{
    public static void main(String[] args){

    }
}

class Dog{
    public void cry(){
        System.out.println("汪汪~~~");
    }
}

abstract class Jing8 extends Dog{
    public abstract void cry() ;
}
以下修饰符,哪些是非法组合?
        abstract + final    = 非法
        abstract + private  = 非法
        abstract + static   = 非法,
        抽象方法没有方法体,如果是静态方法,直接通过类名访问。

        final + private     = 可以通过,修饰成员变量有意义,就是私有常量,如果修饰方法没有意义。

接口

    类似于抽象类,所有方法都是抽象。成员变量都是常量。
    类和接口之间是(多)实现关系。
    类和类之间是继承,单重继承。

    class Xxx implements White,Rich,... {

    }

    class B extends A {

    }

    1.interface
        接口。
    2.接口中的成员修饰符固定
        public static final     //成员变量
        public abstract         //成员函数
    3.通过接口间接实现了多重继承。

    4.最低标准
        耦合度很低。

这里写图片描述

这里写图片描述

这里写图片描述

class InterfaceDemo1 {
    public static void main(String[] args)  {
        PC pc = new PC();
        Mouse m = new Mouse();
        pc.insertUSB(m);
    }
}
class PC{
    public void insertUSB(USB usb){
        System.out.println("插入了usb设备");
        usb.play();
    }
}
//定义接口
interface USB{
    void play() ;
}
//类实现了接口
class Mouse implements USB{
    public void play(){
        System.out.println("鼠标滑动");
    }
}


Computer
-------------
OOP
------------
    1.封装
    2.继承
    3.多态
        多种状态。
        Animal <|-- Dog <| -- Jing8
        使用父类引用子类的对象。
        使用接口引用实现类的对象。
class InterfaceDemo1 {
    public static void main(String[] args)  {
        PC pc = new PC();
        Mouse m = new Mouse();
        pc.insertUSB(m);
    }
}
class PC{
    public void insertUSB(USB usb){
        System.out.println("插入了usb设备");
        usb.play();
    }
}
//定义接口
interface USB{
    void play() ;
}
//类实现了接口
class Mouse implements USB{
    public void play(){
        System.out.println("鼠标滑动");
    }
}

这里写图片描述

土豪征婚

class MultiStateDemo2{
    public static void main(String[] args)  {
        WomanStar fbb = new WomanStar();
        White w = fbb ;
        Rich r = fbb ;

        Beautiful b = (Beautiful)w ;
        b.beau();

        //Dog d = (Dog)b ;
        WRB wrb = (WRB)b ;

        White www = wrb ;


    }
}

interface White{
    public void veryWhite();
}
interface Rich{
    public void hasMoney();
}
interface Beautiful{
    public void beau();
}
//接口继承
interface WRB extends White,Rich,Beautiful{
}
//女明星
class WomanStar implements WRB{
    public void veryWhite(){
        System.out.println("很白~~");
    }
    public void hasMoney(){
        System.out.println("很有钱");
    }
    public void beau(){
        System.out.println("很漂亮");
    }
}

class Dog{
}

猜你喜欢

转载自blog.csdn.net/weixin_37243717/article/details/80789779
今日推荐