Structuring method PrivatizationThe encapsulation of classes is not only in the encapsulation of attributes today. In fact, the method can be encapsulated, of course, in the method package also includes the encapsulation of the construction method. For example, the following code encapsulates the construction method.
Class Singleton{private Singleton () {//encapsulates the construction method, privatized}public void print () {System.out.println ("Hello world!!!");}};
Use
Class Singleton{private Singleton () {//encapsulates the construction method, privatized}public void print () {System.out.println ("Hello world!!!");}}; public class Singletondemo02{public static void Main (String args[]) {Singleton S1 = null;//declaration Object S1 = new Singleton ();//Wrong Cannot instantiate the object}};
At this time The Singleton class, which is a privatized constructor method, cannot be instantiated externally. However, it is possible to instantiate internally, as seen in the following:
Class Singleton{singleton instance = new Singleton ();//internally generated instantiation object of this class private Singleton () {//The construction method is encapsulated. Privatisation}public void print () {System.out.println ("Hello world!!!");}}; public class Singletondemo03{public static void Main (String args[]) {Singleton S1 = null;//Declaration Object}};
Once a class's construction method is privatized, it is only possible to get instantiated objects from within its class, so the question at this point is how to take the internally generated instance object to the external class so that the external can be instantiated directly from this object.Under normal circumstances, the instance property can only be called through the instantiation of the object in the Singleton class. Assuming that the instance object is still available when the object is not instantiated, it is necessary to declare the instance as a static access type because of the variable declared with the static. Be able to access the class name directly.
For example, see the following:
Class Singleton{static Singleton instance = new Singleton ()//internally produces instantiated objects of this class private Singleton () {//encapsulates the construction method. Privatisation}public void print () {System.out.println ("Hello world!!!");}}; public class Singletondemo04{public static void Main (String args[]) {Singleton S1 = null;//declaration object S1 = singleton.instance; /Get instantiated object S1.print ();//Call Method}};
Under normal circumstances, these attributes should be encapsulated. So the above code is best changed to the following form:
Class Singleton{private static Singleton instance = new Singleton ();//internally generated instantiated objects of this class public static Singleton getinstance () {//Get instance object return instance by static method;} Private Singleton () {//The construction method is encapsulated, privatized}public void print () {System.out.println ("Hello world!!!");}}; public class Singletondemo05{public static void Main (String args[]) {Singleton S1 = null;//declaration Object S1 = singleton.getinstance ();//Get instantiated Object S1.print ();//Call Method}};
After the construction method is privatized, the instantiated object can be obtained in the form above.The implication of the program is that privatization of the construction method prevents externally generated objects, only the first initialized and unique singleton object can be obtained by static methods.
Object Array an array of so-called objects. Refers to the inclusion of a set of related objects. However, in the use of object arrays, it is important to note that arrays must first open up space, but because they are reference data types. So each object in the array is a null value, then each object in the array must be instantiated separately when used. Object Array declaration Class Object array name [] = new class [array length];
class Person{private string name;//Name Property public person (String name) {//the content is set by construction method this.name = name;//The name is assigned a value}public string Getnam E () {return this.name;//Get Name}};p ublic class Objectarraydemo01{public static void Main (String args[]) {//class name Array name [] = new class name [length]person per[] = new person[3];//Open up three space-sized arrays System.out.println ("============== array declaration =================");// Before an object array is initialized, each element is the default value for (int x=0;x<per.length;x++) {///cyclic output System.out.print (Per[x] + ",");//Because it is only open space, it is the default value}// Initialized for each element in the array, respectively. Each object must be instantiated separately per[0] = new Person ("Zhang San");//Instantiate the first element per[1] = new Person ("John Doe");//Instantiate the second element per[2] = new Man ("Harry");//Real Instantiate a third element System.out.println ("\n============== Object Instantiation ================="); for (int x=0;x<per.length;x++) {// Loop Output System.out.print (Per[x].getname () + ",");//At this time, the instantiation is complete, so the name will be printed directly}};
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
Arrays are also classified as static initialization and dynamic initialization when used.
For example, see the following:
Class Person{private string name;//Name Property public person (String name) {//the content is set by construction method this.name = name;//name Assignment}public String GetName () {return this.name;//Get Name}};p ublic class Objectarraydemo02{public static void Main (String args[]) {//declares an array of objects. There are three objects in it, using static initialization to complete the person per[] = {The new person ("Zhang San"), the new person ("John Doe"), the new person ("Harry")}; System.out.println ("\n============== array output ================="); for (int x=0;x<per.length;x++) {// Loop Output System.out.print (Per[x].getname () + ",");//At this time, the instantiation is complete, so the name will be printed directly}};
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
Note: Each element in an object array must be instantiated separately, otherwise the content is null.
Inner class There is another class inside a class. is called an inner class. You can also define another class within a class.
Suppose you redefine a class inner inside a class outer. At this point inner is called an inner class. The class outer is called an external class.
Inner classes can be declared as public or private.
When an inner class is declared public or private. The restrictions on their access are the same as member variables and member methods.
The definition format for the inner class is as follows:
Class outer{//defines the outer class private String info = "Hello world";//defines the private property of the external class class inner{//defines the inner class public void print () {//method s for defining the inner class YSTEM.OUT.PRINTLN (info);//direct access to the private property of the External class}};p ublic void Fun () {//Defines the method of the external class new Inner (). print ();//Call method by instantiating an object in an inner class}}; public class Innerclassdemo01{public static void Main (String args[]) {new Outer (). Fun ();//Call the Fun () method of the External class}};
The above procedure. The inner class exists as an inner class of the outer class, and the object of the inner class is instantiated directly within the outer class fun () method and the print () method is called.the characteristics of the inner class exist:Disadvantage: When working in a class, it is best to define only properties and methods in a class. Assuming that another class is defined, the structure of the class must be broken.
Pros: The ability to access data between two classes enables an internal class to access the properties of an external class. Split classification validation such as the following:
Class outer{//defines the outer class private String info = "Hello world";//defines the private property of the external class public void fun () {//Defines the method of the external class new Inner (This). Print () }public String GetInfo () {///Adds a getter method to get the info content return this.info;}; Class inner{//defines inner class private Outer out = NULL,//Declaration Outer Object Public Inner (Outer out) {this.out = out;} public void print () {//Method System.out.println (This.out.getInfo ()) defining the inner class;//direct access to the private property of the External class}};p Ublic class Innerclassdemo02{public static void Main (String args[]) {new Outer (). Fun ();//Call the Fun () method of the External class}};
Assuming that the inner class is taken outside, the code is added, and the complexity is added. Use the maximum strength of the inner class. Provides a convenient way to access private properties in an external class. However, the above inner class. is not directly externally callable. cannot be used in the form of an external class.
declaring an inner class with static Suppose an inner class uses the Statickeyword declaration. Then this inner class is called an external class. Be able to access directly through the external class. Internal classes.
Class outer{//defines the outer class private static String info = "Hello world";//defines the private property of the external class static class inner{//uses static to define the inner class as the external class public void print () {///method System.out.println (info) for defining internal classes;//direct access to private properties of external classes}};p ublic void Fun () {//Defines methods for external classes new Inner (). print (); /Call method by instantiating an object from an inner class}};p ublic class Innerclassdemo03{public static void Main (String args[]) {new Outer.Inner (). print ();// Call the Fun () method of the External class}};
Using static, you can declare an inner class, which is called an external class and can be called outside the class, but a false idea is to access the properties of the external class, which must be static.in the external class, visit the inner class:an inner class can be called directly in other classes in addition to being able to access it through an external class, but the calling format must be:external class. Inner class Inner Class object = External class instance. New inner Class ();
Class outer{//defines the outer class private String info = "Hello world";//defines the private property of the external class class inner{//defines the inner class public void print () {//method s for defining the inner class YSTEM.OUT.PRINTLN (info);//direct access to the private property of the External class}};p ublic void Fun () {//Defines the method of the external class new Inner (). print ();//Call method by instantiating an object in an inner class}}; public class Innerclassdemo04{public static void Main (String args[]) {Outer out = new Outer ();//External Class instantiation object Outer.Inner in = O Ut.new Inner ();//Instantiate inner class object In.print ();//Call inner class method}};
You can also define an inner class in a method
A class can be defined in any location, such as what is seen below to define an inner class within a method.
However, the parameters of the method in which the inner class is located must be defined as the final type.
Class outer{//defines the outer class private String info = "Hello world";//defines the private property of the external class public void fun (final int temp) {//method for defining external classes class Inn er{//internal class public void print () {//define inner class in method System.out.println ("Properties in class:" + info);//Direct access to private properties of external classes System.out.println ( "Number of references in the method:" + temp);}; New Inner (). Print ()//Call method by instantiating an object in an inner class}};p ublic class Innerclassdemo05{public static void Main (String args[]) {new Outer (). Fun (30);//Call the method of the external class}};
Summary: The inner class is very practical in the actual development. Suppose you use static to declare an inner class. Remember that an inner class that uses static declarations is an external class that uses the form of an "external class. Inner class" to access the operation.
Object-oriented foundation--Privatization of construction method, object array, inner class