Day08 tube 7 abstract interface

Posted by tomlei on Fri, 21 Jan 2022 03:35:17 +0100

abstract class

The keyword Abstract cannot be instantiated. You can define abstract methods to be overridden

Allows common methods, properties, and constructors

When a class inherits an abstract class, it needs to override all abstract methods in the class unless it is also an abstract class Only single inheritance is supported exceptional case

(when class A is an abstract class, B is an abstract class that inherits from a and rewrites some methods, and C is an ordinary class that inherits from B, only the remaining methods need to be rewritten.)

(when class A is an abstract class, B is an abstract class that inherits from a and overrides some methods, and C is an ordinary class that inherits from a, it is necessary to override all methods in a.)

Interface

Keyword interface cannot be instantiated, and abstract methods can be defined to be overridden It's also compiled clss file Support multiple inheritance

Construction is not allowed. The attribute must be a public static constant attribute. The basic method can be defined with the default modifier

Interface classification

Constant interface: it only has constant attributes and is responsible for managing constants of the whole project. It can be replaced by enumeration type

public interface IStaticDemo {
    int A = 10;
    String LXP = "111111111111111111";
}
public class StaticDemo {
    public static void main(String[] args) {
        System.out.println(IStaticDemo.A);
        System.out.println(IStaticDemo.LXP.length());
    }
}

Business interface: a common interface, in which business methods are implemented by classes

Tag interface: used to tag classes, which can be detected by instaceof

public interface ICheck {
}
class TT implements ICheck{

}
class TestTT{
    public static void main(String[] args) {
        ICheck c = new TT();
        System.out.println(c instanceof ICheck);//true
    }
}

Functional interface: there is only one function in the interface, which is decorated with @ FunctionalInterface annotation,

1. Cooperate with anonymous internal classes (both interface and abstraction support),

Function interface (same abstract method):

@FunctionalInterface
public interface FunInter {
    int test(int a,int b);
}

Anonymous inner class:

     FunInter funInter = new FunInter() {
         @Override
         public int test(int a, int b) {
             return a+b;
         }
     }   ;

Lambda expression (interface only)

The function interface is as above, and the implementation is as follows:

     FunInter funInter1 =(i,j)->{
         return i*j;
     };

Relationship between interface and implementation class

1. Keyword implements

2. When an ordinary class implements an interface, it needs to implement all abstract methods in the interface (including the parent interface), unless the class is an abstract class

        3. The correspondence is many to many. An interface can have multiple implementation classes, and an implementation class can implement multiple interfaces

Polymorphic application - Interface oriented programming

1. The compile time type is interface and the runtime type is implementation class

2. The method return type is interface, which can be received by implementation class object

3. The method parameter list is the interface, which can be passed to the implementation class object

Static

Static import constant interface

import static tech.aistar.day07.demo.ITransportConsts.*;

Classic example:

class Base{
     
    static{
        System.out.println("base static");//①
    }
     
    public Base(){
        System.out.println("base constructor");//③
    }
}

public class Test extends Base{
 
    static{
        System.out.println("test static");//②
    }
     
    public Test(){
	     super();
        System.out.println("test constructor");//④
    }
     
    public static void main(String[] args) {
        new Test();
    }
}
public class B{
    public static B t1 = new B();
    public static B t2 = new B();
    
    {
        System.out.println("Tectonic block");
    }
   
    static{
        System.out.println("Static block");
    }

    public static void main(String[] args){
        B t = new B();
    }
}

 

Self understanding: first run the static code in the parent class, then the static code of the child class, and then the general code of the parent class, the construction of the parent class, the general code of the child class, and the construction of the child class Remember that if you perform a new operation on an object in, treat it as an object, drop the class loading first and load the object immediately, while static code loads the class, and ordinary code and constructor load the object

Topics: Java