I. Overview
Object-oriented In addition to encapsulation, inheritance and polymorphism of three characteristics, there are other features, such as abstract abstraction, interface interface, and so on, abstract class mainly as a template for multiple classes, and the interface defines a number of classes should abide by the specification, the final keyword let Java implement immutable class, Make the system more secure.
Second, the text
1 , constructors, and initialization blocks
1.1 Constructors
A constructor is a special method that performs initialization when an instance is created, an important way to create an object, and if the programmer does not provide any constructors for the Java class, the system provides a parameterless constructor for the class, which is empty and does nothing. In any case, the Java class contains at least one constructor.
The specific code is as follows:
Public classa{PrivateString name; Private intAge ; A (String name) {//The This keyword represents the original reference This. Name =name; } //Custom constructors, with two parameters, must have the same name as the class nameA (String name,intAge ) { This(name); This. Age =Age ; } }classtest{ Public Static voidMain (string[] args) {//production object According to the constructora A=NewA"Zhang",5); A A1=NewA (); System. out. Print ("name"+a.name+" Age"+a.age); System. out. Print ("name"+a1.name+" Age"+a1.age); }}
From the above program we can see that the constructor is an important way to create Java objects, through the new keyword to create a constructor, once the programmer provides a custom constructor, the system no longer provides a default constructor, if you do not customize the parameterless constructor, you will not be able to create objects through the parameterless constructor, A class can have more than one constructor, the names must be the same, but the parameters cannot be the same. The This keyword is used in the program to invoke the parameterless constructor of the class, and the use of this must be placed in the first statement.
1.2 Initialize Fast
Java Initializes a single object through the constructor, similar to the initialization block, which can also be used to initialize the object. The specific syntax format is as follows:
[modifier]{ // initialization block executable code ... }
The modifier can only be static, which is called a static initialization block with static modifiers, and the code in the initialization block includes defining local variables, invoking methods of other objects, and using branches, looping statements, and so on. If the modifier is not written, it is called a normal initialization block. Here's a simple code:
class test{ // normal initialization block { // initialization block execution statement } // static initialization blocks Static { // static initialization block execution statement } Test () { // parameterless constructor execution statement }}
If the above program executes test test = new test (), then its execution order is to execute the static initialization block first, then the normal initialization block, and finally the test ().
Note: the initialization block executes as the class is loaded and executes only once, initializing all objects.
2 , abstract class
In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods, such as defining a people class, which provides a eat () method, However, the Eat () method of the different people subclasses is not the same, that is people cannot accurately describe the eat () method. So how do you get people () to include the Eat () method without having to provide another way to implement it? This is then done using an abstract method to satisfy this requirement. The specific characteristics of abstract methods and classes are as follows:
1. Abstract classes and methods must use the abstract modifier, and the abstraction method cannot have a method body.
2. Abstract classes cannot be instantiated.
3, abstract class and ordinary class is not much different, can contain member variables, methods, constructors, initialization blocks and enumeration Class 6 components. The constructor of an abstract class cannot create an instance, primarily for invocation by its subclasses.
4. Classes that contain abstract methods must be defined as abstract classes.
5, abstract class can only be a quilt class inheritance, subclasses want to set up an instance object, must replicate all the abstract methods, if only a part of the replication, the subclass must be abstract class.
Here's a simple code:
//Defining abstract ClassesAbstract classpeople{//Normal member variables PrivateString name; //constructor Function Publicpeople (String name) { This. Name =name; } //Common Methods Public voidenjoy () {System. out. println ("people"); } //declaring abstract methods Public Abstract voideat (); }//student inherits people, and eat () abstract methods are replicated//so that student can be created instance, no longer write student also must be abstractclassStudent extends people{ Public voideat () {System. out. println ("Student"); }}
Note: Abstract classes can also contain no abstract methods and can only be inherited. It is extracted from a number of classes with the same characteristics, with the same characteristics, with this abstract class as its subclass template, so as to avoid the arbitrariness of sub-class design.
3 , Interface
An interface is a special abstract class that cannot contain ordinary methods, and all methods in an interface are abstract methods. The specific syntax is as follows:
Interface interface Name [extends parent interface list]{[public] [static] [final] constant; [ Public] [abstract] method;
Modifier: optional, used to specify access permissions for the interface, and the optional value is public. If omitted, the default access rights are used.
Interface Name: A required parameter that specifies the name of the interface and the interface name must be a valid Java identifier. In general, the first letter is required to capitalize.
Extends parent interface Name list: Optional parameter that specifies which parent interface the interface to define inherits from. When you use the extends keyword, the parent interface name is a required parameter.
Method: The method in the interface is only defined and not implemented.
So when does the interface define it? For example, there are two classes with the same method, but the implementation function is not the same, you can define an interface, the method is refined, in the need to use the method of the class to implement, it is exempt from multiple classes to define the system method of trouble. The following is a simple example code:
//defining interface AInterfaceA {voida ();} //Defining interface BInterfaceB {voidb ();} /Interface C inherits A and implements B.InterfaceC extends A implements B {voidc ();}//Interface D implements C and must replicate all of the abstract methods in C, that is, a () and B (), otherwiseclassD implements c{ Public voidA () {System. out. println ("a"); } Public voidB () {System. out. println ("b"); Public voidC () {System. out. println ("C"); } } } // Public classcam2 {Static Public voidMain (String args[]) {//generates an instance of D, and calls the methodD a=NewD (); A.A (); A.B (); A.C (); } }
The above code has the interface A and b,c inherit a implementation of B, in fact, can also be implemented at the same time between Ab,ab only need a comma separated can, after D has implemented C, then D must replicate all the abstract methods of ABC, otherwise it cannot be instantiated.
Note: the interface is exposed to the rules, interfaces can inherit the interface, can be implemented more often, the general function of things to the Quilt class inheritance, extension function is implemented in the interface.
4 , Singleton and Final Key Words
4.1 Single case
A singleton is the most efficient way to solve a class that has only one object in memory, and here's a snippet of code:
//Lazy Type Public classSingleton {//Privatization Statement Variables Private StaticSingleton uniqueinstance =NULL; //privatization of constructors, guaranteed not to be instantiated by other classes PrivateSingleton () {//Exists only to defeat instantiation. } //Provides a public static method to provide its own instance Public StaticSingleton getinstance () {if(Uniqueinstance = =NULL) {uniqueinstance=NewSingleton (); } returnuniqueinstance; } //Other methods ...}
As you can see from the code, to guarantee the uniqueness of the object, the steps are:
1. To prevent other programs from establishing the object instance, you must privatize the constructor
2. Create an instance of this class in this class
3. To facilitate access to other programs, access can be provided externally
The lazy-type single-case method described above is delayed loading, and there is a a hungry man type, the specific code is as follows:
//a hungry man Singleton class. When class is initialized, it is self-instantiated Public classSingleton1 {//Private default Construction child PrivateSingleton1 () {}//already instantiated on its own Private StaticFinal Singleton1 single =NewSingleton1 (); //provide self-examples externally Public StaticSingleton1 getinstance () {returnSingle ; } }
Note: generally used in the development of a hungry man-style, lazy-type interview with more, because lazy-type has shortcomings, easy to create instances, can not guarantee the uniqueness of the instance.
4.2 Final keyword
Final can modify classes, variables, and methods, or you can modify local variables and parameters. Then he is characterized by:
1. When modifying a variable, it is stated that once the variable is assigned it cannot be changed, in general the format is public static final int MAX, this is called global constant, note that the constant name is capitalized.
2. A class that is final modified cannot be inherited, preventing the method from being overwritten after the quilt class inherits.
3. The final modified method cannot be replicated.
Iii. Summary
The above describes the constructors and initialization blocks, which are used to initialize objects, interfaces and abstract classes to improve the reuse of functions, greatly improve the development efficiency, single case is a design pattern, Java has a lot of such design patterns, for the development has brought great convenience.
Dark Horse programmer--java Base---Object oriented (bottom)