Upward transformation
The pointer of the parent class can point to the object of the child class
Objects of subclasses can be labeled as parent classes
Only methods defined in pointer types can be called
abstract class
Using abatrace to decorate classes -- abstract classes
Abstract class function: used to be inherited
abstract class Animal{ public void eat(){ } }
Abstract methods can be defined in abstract classes
There is a definition of this method, but it is not implemented, and there is no method body
Entity classes that inherit abstract classes must implement abstract methods in abstract classes
public abstract class Dragon extends Animal{ // public void rain(){ System.out.println("Ventilation and rain distribution"); } //Abstract methods can be defined in abstract classes //There is a definition of this method, but it is not implemented, and there is no method body //Entity classes that inherit abstract classes must implement abstract methods in abstract classes public abstract void fly(); public static void main(String[] args){ // new Dragon; } } class WaterDragon extends Dragon{ public void fly(){ } } class FireDragon extends Dragon{ public void fly(){ } }
Keyword final
1. The class decorated by final cannot be inherited
2. The quantity (attribute) modified with final cannot be re assigned, but can be modified
final String str; final int[] arr={1,2,3}; public Easy3(String code){ str=code; //Examples that can be modified: arr[0]=23; }
3. Methods modified by final cannot be rewritten (abstract methods cannot be modified)
4. The main purpose of declaring the final method is to prevent the content of the method from being modified
Keyword static
Abstract classes cannot be used to instantiate objects. The only purpose of declaring abstract classes is to expand the class in the future.
A class cannot be modified by abstract and final at the same time. If a class contains abstract methods, the class must be declared as an abstract class, otherwise compilation errors will occur.
Abstract classes can contain abstract and non abstract methods.
//Use static to define the properties and methods of a class public static final int MAX=12; public static int count=12; public static void test(){ System.out.println("Static method"); //Member variables and methods cannot be called directly in static methods (new objects are required) } //Can be called directly in this class public void demo(){ count=34; test(); } class AA{ public void a(){ //Call static methods and properties by class name Easy4.test(); System.out.println(ICar.MAX); } }
Interface
Interfaces are not classes. The way of writing interfaces is very similar to classes, but they belong to different concepts. Class describes the properties and methods of an object. Interface contains the methods to be implemented by the class.
Unless the class that implements the interface is an abstract class, the class should define all methods in the interface.
###Constants in the interface
public interface ICar { //The attributes in the interface are constants //The quantity in the interface is constant by default, and it is decorated with public static final by default //Constant names should be all capitalized public static final int MAX=12; }
Abstract methods in interfaces
public interface ICar { //All methods in the interface are abstract methods //You can define abstract methods in the interface without writing abstract //The methods defined in the interface are all decorated with public abstract void run(); }
Entity methods in interfaces
public interface ICar { //Interface can have entity methods decorated with default //Methods that instances (objects) can call default void test(){} }
Class implements the interface through implements
//Class implements the interface through implements //The class that implements the interface must implement the methods defined in the interface class Truck1 implements ICar,ITest{ int a=12;//global variable @Override public void run() { test(); } }
Interfaces can inherit more than one interface
//Interfaces can inherit more than one interface interface IEasy extends ICar,ITest{ }
Design mode factory mode (creation mode)
introduce
This type of design pattern is a creation pattern, which provides the best way to create objects.
It mainly solves the problem of interface selection.
###Advantages
1. A caller who wants to create an object only needs to know its name.
2. High scalability. If you want to add a product, you can only extend a factory class.
3. Shield the specific implementation of the product, and the caller only cares about the interface of the product.
###Shortcomings
Each time you add a product, you need to add a specific class and object implementation factory, which multiplies the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the dependence of the system on specific classes.
Usage scenarios
1. Loggers: records may be recorded to local hard disks, system events, remote servers, etc. users can choose where to record logs.
2. Database access, when the user does not know what kind of database the system will use and the database may change.
3. Designing a framework to connect to the server requires three protocols, "POP3", "IMAP" and "HTTP", which can be used as product classes to jointly implement an interface.
Implementation steps
1. Create an interface;
2. Create entity classes that implement interfaces;
3. Create a factory to generate objects of entity classes based on the given information;
4. Use the factory to obtain the object of entity class by passing type information;
5. Execute the program and output the results.