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("篮球");
    }
}

接口中的方法

默认方法

静态方法

私有方法

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

相关推荐
m0_748254881 小时前
芋道源码(无遮羞布版)Spring Boot 全景指南
java·spring boot·后端
在下小孙1 小时前
初始C#.
开发语言·c#
m0_748239831 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
freexyn1 小时前
Matlab自学笔记四十五:日期时间型和字符、字符串以及double型的相互转换方法
开发语言·笔记·matlab
kiiila2 小时前
【Qt 常用控件】按钮类(QPushButton、QRadioButton、QCheckBox)
开发语言·qt
江木1233 小时前
Python Numba多流和共享内存CUDA优化技术学习记录
开发语言·python·学习
zhangphil3 小时前
Android BitmapShader实现狙击瞄具十字交叉线准星,Kotlin
android·kotlin
千里马学框架3 小时前
安卓java端service如何在native进程进行访问-跨进程通讯高端知识
android·java·开发语言·安卓framework开发·车机·跨进程·安卓窗口系统
xuexizhe773 小时前
从 Android 进行永久删除照片恢复的 5 种方法
android·智能手机·电脑·手机·笔记本电脑
程序研3 小时前
适配器模式
java·设计模式