Interfaces and internal classes provide us with a more structured approach to separating interfaces from reality.
1, abstract classes and abstract methods.
The following syntax is used for abstract method declarations:
Abstract void f ();
Classes that contain abstract methods are called abstract classes. If a class contains one or more abstract methods, the class must be qualified as abstract. (otherwise, the compiler will make an error.) If an abstract class is incomplete, then we get an error message from the compiler when we try to produce an object of that class, because it is unsafe to create an object for the abstract class. In this way, the compiler ensures the purity of the abstract class, and we don't have to worry about misusing it.
If you inherit from an abstract class and want to create an object of that new class. Then you must provide a method definition for all the abstract methods in the base class. If you do not, then the exported class is also an abstract class, and the compiler will force us to qualify the class with the abstract keyword.
H is useful for creating abstract classes and abstract methods, because they enable the abstraction of classes and tell users and compilers how to use them. Abstract classes are also useful refactoring tools because they make it easy to move public methods up the inheritance hierarchy.
2, interface. Interface keyword makes the concept of abstraction a step forward. The abstract keyword allows people to create one or more methods in a class that do not have any definition---provides an interface section, but does not provide any corresponding concrete implementations. These implementations are created by inheritors of this class. Interface This keyword produces a completely abstract class that does not provide any concrete implementation at all.
You can choose to explicitly declare methods as public in the interface, but even if you do not do so, they are public. Therefore, when an interface is to be implemented, the method defined in the interface must be defined as public. Otherwise, they will only get the default package access, so that the access permissions are lowered during the method's inheritance, which is not allowed by the Java compiler.
Each method in the interface is indeed a declaration, which is the only thing that the compiler allows to exist in the interface.
3, fully decoupled. As long as a method operates on a class rather than an interface. Then you can only use this class and its subclasses. If you want to apply this method to a class that is not in this inheritance structure, then it is not. The interface can largely relax this limitation. Therefore, it allows us to write code that is more reusable.
Policy design Pattern: Creates a method that behaves differently depending on the parameter object being passed. This type of method contains the invariant parts of the algorithm to be executed. The "policy" contains the changed parts. A policy is a parameter object that is passed in, which contains the code to execute.
class processor{ public String name () { return getclass (). Getsimplename ( ); } return input;} } class extends processor{ String process (Object input) {return (String) input). toUpperCase ();} }
Class Downcase extends processor{ String process (Object input) {return (String) input). toLowerCase ();} }
public class apply{
public static void process (Processor p,object s) {
System.out.print ("Using Processor" +p.name ());
System.out.print (p.process (s));
}
String s = "ABCD";
public static void Main (string[] args) {
Process (new Upcase (), s);
Process (new Downcase (), s)
}
}/output:
Using Processor Upcase
Abcd
Using Processor Downcase
Abcd
Adapter design mode: The code in the adapter will accept the interface you have and produce the interface you need.
class filteradapter Impletemts processor{ filter Filter , public Filteradapter (filter Filter) { this. filter=filter; } }
In this way of using the adapter, the Filteradapter constructor accepts all of the interface filters you have, and then generates the objects that you need for the processor interface.
4. Multiple inheritance in Java. is to implement multiple interfaces. This is also the core reason for using interfaces, in order to be able to move up to multiple base types (and the resulting flexibility). Then, the second reason to use the interface is the same as using the abstract base class: Prevent the client programmer from creating objects of that class, and make sure that this is just an interface. But this poses a problem, should we use interfaces or abstract classes? If you want to create a base class without any methods and member variables, you should choose an interface instead of an abstract class.
Interfacecanfight{voidfight (); }Interfacecanswim{voidswim (); }classactioncharacter{ Public voidfight (); }classHeroextendsActioncharacterImplementscanfight,canswim{ Public voidswim () {}} Public classadventure{ Public Static voidt (canfight x) {x.fight ();} Public Static voidu (canswim x) {x.swim ();} Public Static voidW (actioncharacter x) {x.fight ();} Public Static voidMain (string[] args) {Hero h=NewHero (); T (h); U (h); W (h); } }
5, through inheritance to extend the interface. Inheritance makes it easy to add new/method declarations to an interface, and you can combine several interfaces in a new port by inheritance.
Name collisions when combining interfaces: You can overload the method and overwrite the method to write a @override annotation. Try to avoid the confusion of code readability using the same method name in different interfaces.
6, adapter interface. One of the most appealing reasons for interfaces is to allow multiple, different implementations of the same interface. In a simple case, its embodiment is usually a method of accepting an interface type, and the implementation of the interface and the object passed to the method depend on the consumer of the method. The main statement is: "You can use any object you want to call my method, as long as your object follows my interface."
7, the domain in the interface. Any fields that are placed in the interface are automatically static and final. Since the fields are static, they can be initialized the first time the class is loaded, which occurs when any domain is first accessed. Of course, these domains are not part of the interface and their values are stored in the static storage area of the interface.
8, nested interface. An interface can be nested within a class or other interface.
9, interface and factory. The factory method design pattern is very useful, the reuse code is very convenient, unlike the direct call constructor, we call the creation method on the factory object. The factory object will generate an implementation object for the interface.
Java Programming Idea (IX, interface)