java接口用处以及常见接口

java接口用处以及常见接口,第1张

java接口用处以及常见接口

目录

文章目录

前言

一、接口是什么?

二、接口需要注意的地方

 三、接口的具体实现以及代码

四、常用的三个接口

总结



前言

       java是面向对象的,而类与类之间存在继承,但父类里面总是存在着一些不需要具体的实现的方法,从而java推出了抽象方法,而抽象方法更抽象的表现就是接口了,接下来让我们一起走进java接口的世界!!!


一、接口是什么?

  接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现。

二、接口需要注意的地方

使用java接口需要注意的:

1.使用interface关键字来修饰 ;

2.接口当中的方法不能有具体的实现 要实现一定得加default(默认);

3.接口当中可以有static的方法 ;

4.里面所有的方法都是public的 ;

5.抽象方法默认是public abstract的;

6.接口是不可以被new来实例化的 ;

7.类和接口之间的关系是通过关键字implement来实现的 ;

8.当一个类实现了接口类就必须重写接口当中的抽象方法 ;

9.接口当中的成员变量默认是public static final修饰的 ;

10.当一个类实现一个接口之后重写这个方法的时候 方法前得加public ;

11.一个类可以通过extends来继承普通类或抽象类但只能继承一个类,同时也可以通过implements来实现多个接口接口之间用逗号隔开 ;

12.接口和接口之间可以用extends来 *** 作他们之间的关系 意为:拓展 一个接口通过extends来拓展自己的功能,此时当一个类实现 拓展后的接口时,不仅仅是拓展前的抽象方法,还要写另一个接口的抽象方法;

 三、接口的具体实现以及代码

1.接口的简单实现

package demo3;
interface IShape {//接口
    public abstract void draw();
    default  void func(){
        System.out.println(" 1");
    }
    public static  void funStatic(){

    }
}
class Rect implements IShape{//也就是实现 
    @Override
    public void draw() {
        System.out.println("rect");
    }

 //   @Override
//    public void func() {
//        System.out.println("默认方法的重写");
//    }
}
public class Test {
    public static void drawMap(IShape iShape){//这里会完成动态绑定  接口也是可以的
        iShape.draw();
    }
    public static void main(String[] args) {
        IShape iShape = new Rect();
        iShape.draw();
        iShape.func();
        drawMap(iShape);
    }
}

2.接口里面的属性及方法

interface IA{
    public int a = 10;//默认是static final的 是常量  接口当中的成员变量默认是public static final修饰的
    void func();//public abstract
}
interface  IB{
    void funcb();
}
class Bc{

}
class C extends Bc implements IA,IB{
    //一个类可以实现多个接口 里面的方法都得重写  一个类可以继承普通类或抽象类来实现多个接口(如果是抽象类得重写抽象类的抽象方法)
    public  void func(){//必须是public的
        System.out.println("a");
    }
    public void funcb(){
        System.out.println("b");
    }
}

3.接口之间的拓展

interface IA1{
    void funcIA1();
}
interface IB1 extends IA1{//拓展b的功能
    void funcIB1();
}
class H implements IB1{
    @Override
    public void funcIA1() {
        System.out.println("a");
    }

    @Override
    public void funcIB1() {
        System.out.println("b");
    }
}

4.接口面向对象

class Animal{
    protected String name;

    public Animal(String name) {
        this.name = name;
    }
}
interface IFlying{
    void fly();
}
interface IRunning{//接口表示具有某种特性
    void run();
}
interface ISwimming{
    void swim();
}
class Brid extends Animal implements IFlying{

    public Brid(String name) {
        super(name);
    }

    @Override
    public void fly() {
        System.out.println(this.name + "正在飞");
    }
}
class Duck extends Animal implements IRunning,ISwimming,IFlying{

    public Duck(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println(this.name + "正在跑");
    }

    @Override
    public void swim() {
        System.out.println(this.name + "正在游泳");
    }

    @Override
    public void fly() {
        System.out.println(this.name + "正在飞");
    }
}
class Drog extends Animal implements IRunning,ISwimming{

    public Drog(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println(this.name + "正在跑");
    }

    @Override
    public void swim() {
        System.out.println(this.name + "正在游泳");
    }
}
class Robot implements IRunning{
    public Robot() {
    }

    @Override
    public void run() {
        System.out.println( "机器人正在跑");
    }
}
public class Test4 {
    public static void running(IRunning iRunning){
        iRunning.run();
    }
    public static void swimming(ISwimming iSwimming){
        iSwimming.swim();
    }
    public static void flying(IFlying iFlying){
        iFlying.fly();
    }
    public static void main(String[] args) {
        running(new Drog("青蛙"));
        swimming(new Duck("鸭子"));
        flying(new Brid("小鸟"));
        running(new Robot());
    }
}

 

四、常用的三个接口
用哪个接口取决于 业务 推荐Comparator(比较器)
三个常用接口:
Comparable:这个借口有很大的缺点  对类的侵入性非常强  一旦写好了 不能轻易改动
Comparator:比较器  比较灵活
Cloneable:类之间的拷贝

1.Comparable的具体实现

​
class Student implements Comparable{//<>泛型  里面放类
    public int age;
    public String name;
    public double score;
    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //普通的比较方法
        //return this.age - o.age;//从小到大排序
        return o.age - this.age;//从大到小排序
        //return this.name.CompareTo(o.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12,"h",98);
        students[1] = new Student(19,"hh",97.5);
        students[2] = new Student(17,"hhh",60);
        System.out.println(students[1].compareTo(students[0]));//谁调用这个方法谁就是this
        Arrays.sort(students);//这里比较的就是compareTo里面的东西了 默认从小到大
        System.out.println(Arrays.toString(students));
    }
}

​

2.Comparator的具体实现

public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12,"h",98);
        students[1] = new Student(19,"hh",97.5);
        students[2] = new Student(17,"hhh",60);
        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students,ageComparator);
        Arrays.sort(students,scoreComparator);
        System.out.println(ageComparator.compare(students[0],students[1]));
        System.out.println(Arrays.toString(students));
    }
}
class ScoreComparator implements  Comparator{//分数比较器  比较灵活 对类的侵入性非常弱

    @Override
    public int compare(Student o1, Student o2) {

        return (int)(o1.score - o2.score);
    }
}
class NameComparator implements Comparator{//姓名比较器

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class AgeComparator implements Comparator{//年龄比较器

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class Student  {//<>泛型  里面放类
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                ", score=" + score +
                '}';
    }


}

3.Cloneable的具体实现

//创建对象的方法  new 或者克隆接口
class Money implements Cloneable{
    double a = 13.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{//这个接口是个空接口  作用就是表示这个类是可以克隆的

    public int age;
    public Money money = new Money();
    public void eat(){
        System.out.println("吃");
    }
    public Object clone() throws CloneNotSupportedException {//此时必须要重写克隆方法 比较特殊
        //return super.clone();//在这里需要看拷贝方法 才知道是浅拷贝还是深拷贝
        Person h = (Person) super.clone();
        h.money = (Money)this.money.clone();//这里就将引用也深拷贝了
        return h;
    }

    @Override
    public String toString() {
        return "Person{" +
                " age=" + age +
                '}';
    }
}
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person) person.clone();//clone 类型是object的  要实现clone方法其类一定要实现克隆接口
        System.out.println(person1.age);
        person1.age = 20;
        System.out.println(person);
        System.out.println(person1);
        System.out.println("=======");
        Person person2 = (Person)person.clone();
        System.out.println(person.money.a);
        System.out.println(person2.money.a);
        System.out.println("=====");
        person2.money.a = 85.5;
        System.out.println(person.money.a);//这里就是浅拷贝
        System.out.println(person2.money.a);//改变之后就是深拷贝了
    }
}

总结

      这里仅仅简单介绍了接口以及几个常用的接口,接口在java里面还是比较常用的,所以更具体的接口各位也可以更深入的学习!!!

 



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5681129.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存