First acquainted with the Java language-abstract classes and interfaces

First acquainted with the Java language-abstract classes and interfaces

1. Abstract class
What is an abstract class? A class that contains abstract methods;
what is an abstract method? That is, it just declares that there is this method, and does not implement it.
Use the keyword abstract to modify it uniformly .
An abstract class is also a class;
it's just that it cannot instantiate an object separately ; it can
also be inherited;
the abstract methods in the abstract class will eventually be implemented;
Second, the interface
What is an interface? Modified with the keyword interface , introduced in order to solve the single inheritance problem of the class.
The interface can be extended, with the keyword extends .

interface A{
    
    
}
interface B{
    
    
}
interface C extends A,B{
    
    
}

Interfaces and abstract classes have similarities, that is, methods are abstract methods, and methods modified by the keyword default (default access permissions) can be implemented in jdk1.8 . In the interface, the member variables are all static constants, which are modified by static final , and they are initialized at the same time as they are defined.
The interface can be realized by the class, and the interface is realized by the keyword implements (that is, the abstract method in the interface is perfected to realize the corresponding function). At the same time, a class can implement multiple interfaces at the same time.
The interface cannot be instantiated.
The interface can also undergo upward transformation and dynamic binding.

interface A{
    
    
}
interface B{
    
    
}
class C implements A,B{
    
    
}

Note: The methods in the interface are public abstract by default , so you can directly declare the method return value type plus the method name.
(1) Two types of common interfaces
1. Sorting
(1) Comparable interface. By implementing the interface, the compareTo method is rewritten so that the Arrays.sort method can sort the objects of the custom class. The specific sorting rules are determined according to specific requirements.
example:

import java.util.Arrays;
class Student implements Comparable<Student>{
    
    
    public String name;
    public int age;
    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public Student(){
    
    

    }
    public Student(String name,int age){
    
    
        this.age=age;
        this.name=name;
    }
    @Override
    public int compareTo(Student o) {
    
    //重写compareTo方法
       return  this.age-o.age;
    }
}
public class MyComparable {
    
    
    public static void main(String[] args) {
    
    
        Student[]stu=new Student[]{
    
    new Student("zhangsan",38),new Student("lisi",20),new Student("wangmazi",30)};
        for(Student x:stu){
    
    
            System.out.println(x);
        }
        Arrays.sort(stu);
        for(Student x:stu){
    
    
            System.out.println(x);
        }
    }
}

Disadvantages: The compareTo method has been changed, so that the sorting method in the future will be hard-coded and cannot be changed.
(2) Comparator interface
By implementing the Comparator interface, rewrite the compare method to achieve flexible sorting. We only need to implement the Comparator interface with different classes to complete the corresponding sorting function.

import java.util.Comparator;
public class NameComparator implements Comparator <Student2>{
    
    
    @Override
    public int compare(Student2 o1, Student2 o2) {
    
    //重写compare方法
        return o1.name.compareTo(o2.name);
    }
}

The above code is the interface Comparator implemented by the class sorted by name.

import java.util.Arrays;
class Student2{
    
    
   public String name;
   public int age;
    @Override
    public String toString() {
    
    
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public Student2(){
    
    

    }
    public Student2(String name,int age){
    
    
        this.name=name;
        this.age=age;
    }
}
public class MyComparator {
    
    
    public static void main(String[] args) {
    
    
        Student2[]stu=new Student2[]{
    
    new Student2("zhangsan",38),new Student2("lisi",20),new Student2("wangmazi",30)};
        for(Student2 x:stu) {
    
    
            System.out.println(x);
        }
        System.out.println("===================================================");
        NameComparator nameComparator=new NameComparator();
        Arrays.sort(stu,nameComparator);//通过传入不同类型的实例化对象,那么排序方式也会有所不同
        for(Student2 x:stu) {
    
    
            System.out.println(x);
        }
    }
}

(2) Deep copy-Cloneable interface
This interface is an empty interface or a logo interface, because it has no internal content, its function is to identify the current class can be cloned. So implement this interface through implements, rewrite the method clone(), and instantiate the object to " . " our clone method, and then receive it by reference to complete the deep copy.
(If you don’t understand deep copy, you can read my previous article, which is explained.)
Rewrite the clone method:

  @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();
    }

Total code:

class Money implements Cloneable{
    
    
    public int money;
    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();
    }
}
class Person implements Cloneable{
    
    
    public  String name;
    public  int age;
    public Money money=new Money();
    public Person(String name){
    
    
        this.name=name;
    }
    public Person(int age){
    
    
        this.age=age;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + money.money +
                '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        Person person=(Person)super.clone();
        person.money=(Money) this.money.clone();
        return person;
    }
}
public class TestDemo {
    
    
    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        Person person =new Person("zhangsan");
        System.out.println(person);
        Person person2=(Person)person.clone();
        System.out.println(person2);
        System.out.println("===============================================");
        person2.age=18;
        person.money.money=18;
        System.out.println(person);
        System.out.println(person2);

    }
}

The above is the relevant knowledge of interfaces and abstract classes. If there is any incompleteness, please feel free to discuss.

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/112753539