当前位置: 首页 > article >正文

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);
}
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);
    }
}
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);
    }
}
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);
    }
}
package day10.接口练习;

public interface swing {
    public abstract void youyong(String fangfa);
}
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("胡萝卜");
    }
}

 

 接口中成员的特点

 

接口和类之间的关系

练习 

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;
    }
}
package day10.pingpang;

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

public abstract  class xueyuan extends  people{
    public xueyuan(String name,int age){
        super(name,age);
    }
    public abstract void xue(String qiu);
}
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);
    }
}
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);
    }
}
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);
    }
}
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);
    }
}
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("篮球");
    }
}

  

 接口中的方法

默认方法

静态方法

私有方法

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

 

 

 

 

 


http://www.kler.cn/a/510222.html

相关文章:

  • 利用 LNMP 实现 WordPress 站点搭建
  • 编程工具箱(免费,离线可用)
  • MQ消息队列
  • 【tailscale 和 ssh】当服务器建立好节点,但通过客户端无法通过 ssh 连接
  • CSS:语法、样式表、选择器
  • VSCode 的部署
  • springboot基于微信小程序的停车场预订系统
  • 移远通信多模卫星通信模组BG95-S5获得Skylo网络认证,进一步拓展全球卫星物联网市场
  • 基于Java+MySQL实现在线作业提交系统
  • Android 上删除的短信存储在哪里 - 如何恢复
  • dl学习笔记:(4)简单神经网络
  • Java杂记:@TableField注解用法详解
  • HTTPS与HTTP:区别及安全性对比
  • Linux:指令大全(一)
  • Ubuntu把应用程序放到桌面
  • FastAPI 应用的容器化与 Docker 部署:提升性能与可扩展性
  • Java 静态导入:规则、实践与应用场景
  • 2,Linux文件基本属性(基于Ubuntu示例进行讲解)
  • 802.1协议讲解
  • leetcode刷题记录(六十八)——2. 两数相加
  • OpenAI第一个真正意义上的AI Agent:ChatGPT Tasks,使用指南1.0
  • latin1_swedish_ci(latin1 不支持存储中文、日文、韩文等多字节字符)
  • Shell控监Kafka积压
  • 210. 课程表 II【 力扣(LeetCode) 】
  • 【git】如何删除本地分支和远程分支?
  • Gateway与WebFlux