JAVA面向对象-----接口的概述

接口的概述

这里写图片描述

**接口(interface):**usb接口,主要是使用来拓展笔记本的功能,那么在java中的接口主要是使用来拓展定义类的功能,可以弥补java中单继承的缺点。

class Pencil {
    String name;
    Pencil() {
    }
    Pencil(String name) {
        this.name = name;
    }
    void write() {
        System.out.println("写字");
    }
}
interface Eraser {
    public static final String color = "白色";
    public abstract void clean();
}
// 1:带橡皮的铅笔类继承铅笔类实现橡皮接口
class PencilWithEraser extends Pencil implements Eraser {
    PencilWithEraser() {
    }
    PencilWithEraser(String name) {
        super(name);
    }
    void write() {
        System.out.println(name + ":考试专用");
    }
    public void clean() {
        System.out.println(super.name + ":带橡皮的铅笔,就是好用");
    }
}
class Demo6 {
    public static void main(String[] args) {
        PencilWithEraser pe = new PencilWithEraser("中华2B");
        pe.write();
        pe.clean();
        System.out.println(pe.color);
        System.out.println(PencilWithEraser.color);
    }
}

接口的定义格

interface 接口名{
属性
抽象方法
}体验
interface Inter
{
    int num = 6;  可以定义属性与方法。
    void show();
}

注意:

1.接口中的所有属性 默认的修饰符是 public static final。
2.接口中的所有方法 默认的修饰符是 public abstract。


【正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!下面有个“顶”字,你就顺手把它点了吧(要先登录CSDN账号哦 )】


—–乐于分享,共同进步!
—–更多文章请看:http://blog.csdn.net/duruiqi_fx


原文地址:https://www.cnblogs.com/hainange/p/6153854.html