• 抽象类和接口以及Comparable内部比较器


    一.抽象类

    抽象类:定义共性功能,继承抽象类,那么抽象类中的抽象方法必须全部实现。

    1.抽象类和抽象方法都需要被abstract修饰。抽象方法一定要定义在抽象类中
    2.抽象类不可以直接创建对象(原因:调用抽象方法没有意义)
    3.只有覆盖了抽象类中所有的抽象方法后,其子类才可以创建对象。不然子类也要是抽象类。

      4.抽象类一定是父类

      5.抽象类中可以不定义抽象方法(意义:静态方法可以直接让子类去使用)

      6.关键字不可以和private,final一起用。(final声明的类不允许有子类,private私有的不允许覆写)

      7.抽象类中可以没有抽象方法

     

     代码1:

    public abstract class Animal {
        public abstract void eat();
        public abstract void sleep();
    }
    
    class Dog extends Animal{
        @Override
        public void eat() {
            System.out.println("dog eat");
        }
    
        @Override
        public void sleep() {
            System.out.println("dog sleep");
        }
    }
    
    //只有覆盖了抽象类中所有的抽象方法后,其子类才可以创建对象。不然子类也要是抽象类
    abstract class Cat extends Animal{
    
        public abstract void catchMouse();
    
    }
    
    
    class LittleCat extends Cat{
    
        @Override
        public void eat() {
            System.out.println("little cat eat");
        }
    
        @Override
        public void sleep() {
            System.out.println("little cat sleep");
        }
    
        @Override
        public void catchMouse() {
            System.out.println("little cat catch");
        }
    }

    代码2:

    package com.atguigu.day10;
    
    public abstract class Animal {
        String name;
        int age;
    
        public Animal() {
        }
    
        public Animal(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public abstract void eat();
        public abstract void sleep();
    
        @Override
        public String toString() {
            return "name:"+name+" age:"+age;
        }
    }
    
    class Dog extends Animal{
    
        public Dog() {
        }
    
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("dog eat");
        }
    
        @Override
        public void sleep() {
            System.out.println("dog sleep");
        }
    }
    
    //只有覆盖了抽象类中所有的抽象方法后,其子类才可以创建对象。不然子类也要是抽象类
    abstract class Cat extends Animal{
    
        public abstract void catchMouse();
    
    }
    
    
    class LittleCat extends Cat{
    
        @Override
        public void eat() {
            System.out.println("little cat eat");
        }
    
        @Override
        public void sleep() {
            System.out.println("little cat sleep");
        }
    
        @Override
        public void catchMouse() {
            System.out.println("little cat catch");
        }
    }

    main

    package com.atguigu.day10;
    
    public class Test {
    
        public static void main(String args[]) {
    
            Dog dog1= new Dog();
            dog1.name="lin1";
            dog1.age=23;
            System.out.println(dog1);
            Dog dog2= new Dog("lin2",14);
            System.out.println(dog2);
        }
    }

    输出

    name:lin1 age:23
    name:lin2 age:14

    二.接口

    接口:用于拓展性

    1.包含了功能声明的特殊类。
    2.定义格式:public interface 接口名{}    使用格式:class A implements 接口名{}
    3.接口中可以定义成员变量,但变量只能是最终变量,并且是静态的。默认修饰符:public static final。
    4.接口中可以定义方法,但只能是抽象方法。接口中的方法默认被public abstract修饰
    5.接口中没有构造方法,不可以创建对对象。 
    6.子类必须覆盖掉接口中所有的抽象方法,子类才可以实例化。
    7.多个接口之间可以使用extends进行多继承,如果一个类继承了,实现了多个接口的接口,不仅要实现此接口的抽象方法,也要实现继承接口的抽象方法
    8.类可以实现多个接口

     代码:

    public interface Fly {
        int num=20;
        void fly();
    }
    
    interface Eats{
        void eat();
    }
    
    class Superman implements Fly{
    
        @Override
        public void fly() {
            System.out.println("super man fly");
        }
    }
    
    class Bird implements Fly,Eats{
    
        @Override
        public void fly() {
            System.out.println("bird fly");
        }
    
        @Override
        public void eat() {
            System.out.println("bird eat");
        }
    }

    main

    public class Test {
    
        public static void main(String args[]) {
    
            Superman s1 = new Superman();
            s1.fly();
            Bird b1 = new Bird();
            b1.fly();
            b1.eat();
    
            //接口的多态
            Fly f1 = new Bird();
            f1.fly();
            //接口中的成员变量被public static final修饰
            System.out.println(Fly.num);
        }
    }

    输出

    super man fly
    bird fly
    bird eat
    bird fly
    20

     代码2:

    public interface Fly {
        int num=20;
        void fly();
        //jdk8提供的,jdk8以后在接口中可以使用此方法,给继承接口的类按需选择使用
        public default void addOil(){
            System.out.println("add oil");
        }
        //也可以有静态方法
        public static void show(){
            System.out.println("fly show");
        }
    }
    
    interface Eats{
        void eat();
    }
    
    class Superman implements Fly{
    
        @Override
        public void fly() {
            System.out.println("super man fly");
        }
    
        @Override
        public void addOil() {
            System.out.println("super man add oil");
        }
    }
    
    class Bird implements Fly,Eats{
    
        @Override
        public void fly() {
            System.out.println("bird fly");
        }
    
        @Override
        public void eat() {
            System.out.println("bird eat");
        }
    }

    main:

    package com.atguigu.day10;
    
    public class Test {
    
        public static void main(String args[]) {
            Superman s1 = new Superman();
            s1.fly();
            //s1按需选择使用addOil
            s1.addOil();
            Bird b1 = new Bird();
            b1.fly();
            b1.eat();
    
    
            //接口的多态
            Fly f1 = new Bird();
            f1.fly();
            //接口中的成员变量被public static final修饰
            System.out.println(Fly.num);
            //静态方法
            Fly.show();
    
        }
    }

    输出

    super man fly
    super man add oil
    bird fly
    bird eat
    bird fly
    20
    fly show

     三.接口特殊情况

    package com.atguigu.day10;
    
     interface Study {
        public default void study(){
            System.out.println("just study");
        }
    }
    
    interface HardStudy {
        public default void study(){
            System.out.println("just sing");
        }
    }
    
    class Students implements Study,HardStudy{
    
        @Override
        public void study() {
            System.out.println("self study");
            //可以在重写的方法中通过接口名.super.方法名调用
            Study.super.study();
            HardStudy.super.study();
        }
    }

    main

    package com.atguigu.day10;
    
    public class Test {
    
        public static void main(String args[]) {
            Students st1 = new Students();
            st1.study();
    
        }
    }

    输出

    self study
    just study
    just sing

     四.comparable接口

    目前对象.compareTo(需要比较的对象)

    Comparable接口的声明以及作用,可以看到它可以使继承他的类进行比较大小,只需要调用实现类的compareTo方法即可

    实现比较大小:
    
    如果返回值等于零:o1=o2
    返回值大于零则o1>o2
    返回值小于于零则o1<o2

    代码:

    package com.atguigu.day10;
    
    class P implements Comparable<P> {
    
        String name;
        int age;
        public P(String name,int age) {
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    
        @Override
        public int compareTo(P o) {
            return this.age-o.age;
        }
    }

    main

    public class ComparableTest {
    
        public static void main(String[] args) {
            P[] personList = new P[4];
            personList[0] = (new P("lin1", 22));
            personList[1] = (new P("lin2", 21));
            personList[2] = (new P("lin3", 36));
            personList[3] = (new P("lin4", 20));
    
            System.out.println("比较大小");
            P lin1 = new P("lin1", 22);
            P lin2 = new P("lin2", 21);
            String result = lin1.compareTo(lin2) == 0 ? "一样大" : lin1.compareTo(lin2) > 0 ? "lin1大" : "lin2大";
            System.out.println(result);
    
        }
    
    }

    输出

    比较大小
    lin1大

    引用数据类型比较
    Comparable:内部比较器
    在比较对象类的内部,完成了比较规则的制定
    
    Comparabtor:外部比较器
    Comparable代码
    package com.atguigu.day10;
    
    public class FlyMan implements Comparable{
        String name;
        int age;
        int height;
    
        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 int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        public FlyMan() {
        }
    
        public FlyMan(String name, int age, int height) {
            this.name = name;
            this.age = age;
            this.height = height;
        }
    
        @Override
        public String toString() {
            return "name:"+name+" age:"+age+" height:"+height;
        }
    
        @Override
        public int compareTo(Object o) {
            FlyMan f = (FlyMan) o;
            return this.height-f.height;
        }
    }
    package com.atguigu.day10; public class WorkMan implements Comparable{ String name; int age; public WorkMan() { } public WorkMan(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; } @Override public String toString() { return "name:"+name+" age:"+age; } @Override public int compareTo(Object o) { WorkMan workMan = (WorkMan) o; return this.age-workMan.age; } }

    package com.atguigu.day10.Utils; public class SortArry { public static void sort(Comparable[] arr){ for (int i = 0; i <arr.length-1 ; i++) { for (int j = 0; j < arr.length-1-i; j++) { if (arr[j].compareTo(arr[j+1])>0) { Comparable temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } }

    package com.atguigu.day10; import com.atguigu.day10.Utils.SortArry; public class CompareTest { public static void main(String[] args) { FlyMan f1= new FlyMan("lin1",23,177); FlyMan f2= new FlyMan("lin2",28,183); FlyMan f3= new FlyMan("lin3",19,182); FlyMan f4= new FlyMan("lin4",24,181); FlyMan[] fly1= {f1,f2,f3,f4}; WorkMan w1 = new WorkMan("yang1",25); WorkMan w2 = new WorkMan("yang2",20); WorkMan w3 = new WorkMan("yang3",23); WorkMan w4 = new WorkMan("yang4",22); WorkMan[] work1 = {w1,w2,w3,w4}; System.out.println("fly1排序前-------------------"); for (int i = 0; i < fly1.length; i++) { System.out.println(fly1[i]); } System.out.println("fly1排序后-------------------"); SortArry.sort(fly1); for (int i = 0; i <fly1.length ; i++) { System.out.println(fly1[i]); } System.out.println("work1排序前-------------------"); for (int i = 0; i <work1.length ; i++) { System.out.println(work1[i]); } System.out.println("work1排序前-------------------"); SortArry.sort(work1); for (int i = 0; i <work1.length ; i++) { System.out.println(work1[i]); } } }

    输出

    fly1排序前-------------------
    name:lin1 age:23 height:177
    name:lin2 age:28 height:183
    name:lin3 age:19 height:182
    name:lin4 age:24 height:181
    fly1排序后-------------------
    name:lin1 age:23 height:177
    name:lin4 age:24 height:181
    name:lin3 age:19 height:182
    name:lin2 age:28 height:183
    work1排序前-------------------
    name:yang1 age:25
    name:yang2 age:20
    name:yang3 age:23
    name:yang4 age:22
    work1排序前-------------------
    name:yang2 age:20
    name:yang4 age:22
    name:yang3 age:23
    name:yang1 age:25
    
    
    Comparabtor
    代码1:
    package com.atguigu.day10;
    
    public class TeacherTest {
    
        public static void mineSort(Teacher[] ts,TeacherSortOfAge sortOfAge){
            for (int i = 0; i <ts.length-1 ; i++) {
                for (int j = 0; j <ts.length-i-1 ; j++) {
                    if(sortOfAge.compare(ts[j],ts[j+1])>0){
                        Teacher temp=ts[j];
                        ts[j]=ts[j+1];
                        ts[j+1]=temp;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Teacher t1 = new Teacher("lin1",18,100.0);
            Teacher t2 = new Teacher("lin2",12,101.0);
            Teacher t3 = new Teacher("lin3",19,90.0);
            Teacher t4 = new Teacher("lin4",16,103.0);
            Teacher t5 = new Teacher("lin5",22,87.0);
            Teacher[] ts={t1,t2,t3,t4,t5};
            System.out.println("排序前--------------------");
            for (int i = 0; i <ts.length ; i++) {
                System.out.println(ts[i].toString());
            }
            System.out.println("排序后--------------------");
            TeacherSortOfAge tsoa = new TeacherSortOfAge();
            mineSort(ts,tsoa);
            for (int i = 0; i <ts.length ; i++) {
                System.out.println(ts[i].toString());
            }
        }
    }
    package com.atguigu.day10;
    /*
    使用外部比较器比较:
    在比较对象的外面新建了一个类专门用于比较
    1.定义一个类,实现Comparator接口
    2.重写compare()传递连个参数
    3.定制比较规则
    4.先去创建比较规则的对象,通过对象调用compare()再传入对象
     */
    import java.util.Comparator;
    
    public class TeacherSortOfAge implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Teacher p1 =(Teacher) o1;
            Teacher p2 = (Teacher) o2;
            return p1.age-p2.age;
        }
    }

    代码2:

    package com.atguigu.day10;
    import java.util.Comparator;
    public class TeacherSortOfAgeAndSalary implements Comparator{
    
        @Override
        public int compare(Object o1, Object o2) {
            Teacher p1 =(Teacher) o1;
            Teacher p2 = (Teacher) o2;
            //当年龄相等
            if(p1.age==p2.age){
                return Double.compare(p1.salary,p2.salary);
            }
            return p1.age-p2.age;
        }
    }
    package com.atguigu.day10.Utils;
    
    import java.util.Comparator;
    
    public class SortUtils {
        public static void sort(Object[] objArr, Comparator com){
            for (int i = 0; i < objArr.length-1; i++) {
                for (int j = 0; j <objArr.length-i-1 ; j++) {
                    if(com.compare(objArr[j],objArr[j+1])>0){
                        Object temp=objArr[j];
                        objArr[j]=objArr[j+1];
                        objArr[j+1]=temp;
                    }
                }
            }
        }
    }
    package com.atguigu.day10;
    
    import com.atguigu.day10.Utils.SortUtils;
    
    public class TeacherTest {
    
        public static void mineSort(Teacher[] ts,TeacherSortOfAge sortOfAge){
            for (int i = 0; i <ts.length-1 ; i++) {
                for (int j = 0; j <ts.length-i-1 ; j++) {
                    if(sortOfAge.compare(ts[j],ts[j+1])>0){
                        Teacher temp=ts[j];
                        ts[j]=ts[j+1];
                        ts[j+1]=temp;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Teacher t1 = new Teacher("lin1",18,100.0);
            Teacher t2 = new Teacher("lin2",12,101.0);
            Teacher t3 = new Teacher("lin3",18,90.0);
            Teacher t4 = new Teacher("lin4",16,103.0);
            Teacher t5 = new Teacher("lin5",16,87.0);
            Teacher[] ts={t1,t2,t3,t4,t5};
            System.out.println("排序前--------------------");
            for (int i = 0; i <ts.length ; i++) {
                System.out.println(ts[i].toString());
            }
            System.out.println("排序后--------------------");
            TeacherSortOfAgeAndSalary tsoas = new TeacherSortOfAgeAndSalary();
            SortUtils.sort(ts,tsoas);
            for (int i = 0; i <ts.length ; i++) {
                System.out.println(ts[i].toString());
            }
        }
    }

    输出

    排序前--------------------
    name:lin1 age:18 salary:100.0
    name:lin2 age:12 salary:101.0
    name:lin3 age:18 salary:90.0
    name:lin4 age:16 salary:103.0
    name:lin5 age:16 salary:87.0
    排序后--------------------
    name:lin2 age:12 salary:101.0
    name:lin5 age:16 salary:87.0
    name:lin4 age:16 salary:103.0
    name:lin3 age:18 salary:90.0
    name:lin1 age:18 salary:100.0
     
  • 相关阅读:
    广播的最佳实践---实现强制下线功能
    广播的最佳实践---实现强制下线功能
    使用本地广播
    使用本地广播
    关于Win7固态优盘的优化方法
    关于Win7固态优盘的优化方法
    在屏幕中使用评分组件
    在屏幕中使用评分组件
    【NYOJ】[65]另一种阶乘问题
    【NYOJ】[65]另一种阶乘问题
  • 原文地址:https://www.cnblogs.com/hbxZJ/p/15520601.html
Copyright © 2020-2023  润新知