Java(面向对象进阶——接口)

接口的定义和使用

练习

java 复制代码
package day10.接口练习;

public  abstract class animals {
    private String name;
    private int age;
    public animals(){

    }
    public animals(String name,int age){
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public abstract void eat(String something);
}
java 复制代码
package day10.接口练习;

public class dog extends animals implements swing{
    public dog(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(String something) {
        System.out.print(getAge()+"岁的"+"小狗吃"+something);
    }

    @Override
    public void youyong(String fangfa) {
        System.out.println(fangfa);
    }
}
java 复制代码
package day10.接口练习;

public class frog extends animals implements swing{
    public frog(String name,int age){
        super(name,age);
    }
    @Override
    public void eat(String something) {
        System.out.print(getAge()+"岁的"+"青蛙吃"+something);
    }

    @Override
    public void youyong(String fangfa) {
        System.out.print(fangfa);
    }
}
java 复制代码
package day10.接口练习;

public class tuzi extends animals{
    public tuzi (String name,int age){
        super(name,age);
    }
    public void eat(String something){
        System.out.println();
        System.out.println(getAge()+"岁的"+"兔子吃"+something);
    }
}
java 复制代码
package day10.接口练习;

public interface swing {
    public abstract void youyong(String fangfa);
}
java 复制代码
package day10.接口练习;

public class test {
    public static void main(String[] args) {
        dog d=new dog("小黄",2);
        frog f=new frog("小青",6);
        tuzi t=new tuzi("突牙",3);
        d.eat("骨头");
        d.youyong("狗刨");
        f.eat("虫子");
        f.youyong("蛙泳");
        t.eat("胡萝卜");
    }
}

接口中成员的特点

接口和类之间的关系

练习

java 复制代码
package day10.pingpang;

public class people {
    private String name;
    private int age;
    public people(){

    }
    public people (String name,int age){
        this.age=age;
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
java 复制代码
package day10.pingpang;

public abstract  class jiaolian extends  people{
    public jiaolian(String name,int age){
        super(name,age);
    }
    public abstract void jiao(String qiu);
}
java 复制代码
package day10.pingpang;

public abstract  class xueyuan extends  people{
    public xueyuan(String name,int age){
        super(name,age);
    }
    public abstract void xue(String qiu);
}
java 复制代码
package day10.pingpang;

public class lanqiu_jiaolian extends jiaolian{
    public lanqiu_jiaolian(String name, int age) {
        super(name, age);
    }

    @Override
    public void jiao(String qiu) {
        System.out.println(getName()+"年龄为"+getAge()+"教打"+qiu);
    }
}
java 复制代码
package day10.pingpang;

public class pingpang_xueyuan extends people implements English{
    public pingpang_xueyuan(String name,int age){
        super(name,age);
    }
    @Override
    public void speak(String langer) {
        System.out.print(getName()+"年龄为"+getAge()+"会讲"+langer);
    }
    //@Override
    public void xue(String qiu){
        System.out.println("学习打"+qiu);
    }
}
java 复制代码
package day10.pingpang;

public class lanqiu_xueyuan extends xueyuan{
    public lanqiu_xueyuan(String name,int age){
        super(name,age);
    }
    //@Override
    public void xue(String qiu){
        System.out.println(getName()+"年龄为"+getAge()+"学习打"+qiu);
    }
}
java 复制代码
package day10.pingpang;

public class pingpang_jiaolian extends jiaolian implements English{
    public pingpang_jiaolian(String name, int age) {
        super(name, age);
    }

    @Override
    public void jiao(String qiu) {
        System.out.print(getName()+"教打"+qiu);
    }

    @Override
    public void speak(String langer) {
        System.out.println("会讲"+langer);
    }
}
java 复制代码
package day10.pingpang;

public class test {
    public static void main(String[] args) {
        pingpang_jiaolian p1=new pingpang_jiaolian("李教练",38);
        pingpang_xueyuan p2=new pingpang_xueyuan("王小生",18);
        p1.jiao("乒乓球");
        p1.speak("英语");
        p2.speak("英语");
        p2.xue("乒乓球");
        System.out.println("----------------");
        lanqiu_jiaolian p3=new lanqiu_jiaolian("姚明",43);
        lanqiu_xueyuan p4=new lanqiu_xueyuan("姚小明",16);
        p3.jiao("篮球");
        p4.xue("篮球");
    }
}

接口中的方法

默认方法

静态方法

私有方法

适配器设计模式 (设置中间人)

相关推荐
巷北夜未央2 分钟前
数据结构之二叉树Python版
开发语言·数据结构·python
wapicn993 分钟前
手机归属地查询Api接口,数据准确可靠
java·python·智能手机·php
Thread.sleep(0)5 分钟前
WebRTC源码解析:Android如何渲染画面
android·webrtc
旧识君25 分钟前
移动端1px终极解决方案:Sass混合宏工程化实践
开发语言·前端·javascript·前端框架·less·sass·scss
hycccccch30 分钟前
Springcache+xxljob实现定时刷新缓存
java·后端·spring·缓存
郝YH是人间理想41 分钟前
OpenCV基础——傅里叶变换、角点检测
开发语言·图像处理·人工智能·python·opencv·计算机视觉
wisdom_zhe43 分钟前
Spring Boot 日志 配置 SLF4J 和 Logback
java·spring boot·logback
Tiger Z44 分钟前
R 语言科研绘图第 36 期 --- 饼状图-基础
开发语言·程序人生·r语言·贴图
揣晓丹1 小时前
JAVA实战开源项目:校园失物招领系统(Vue+SpringBoot) 附源码
java·开发语言·vue.js·spring boot·开源
Android 小码峰啊1 小时前
Android Dagger 2 框架的注解模块深入剖析 (一)
android·adb·android studio·android-studio·androidx·android runtime