Let's talk about some of the syntax in Java programming:
1. What is an array of objects?
An array of objects is the object of the class for each element in the array, which defines the object before assigning it, and assigns the object directly to the array.
Declaration of an array of objects:
class name [] Object Array name = new class name [array length];
1 classperson{2 PrivateString name;//Name Attribute3 PublicPerson (String name) {//setting content by construction method4 This. name = name;//assign a value to a name5 } 6 PublicString GetName () {7 return This. Name;//Get name8 } 9 }; Ten Public classobjectarraydemo01{ One Public Static voidMain (String args[]) { A //class name Array name [] = new class name [length] -Person per[] =Newperson[3] ;//opens up three space-sized arrays -System. out. println ("============== Array Declaration =================") ; the //Each element is a default value before an array of objects is initialized - for(intx=0; x<per.length;x++) {//Loop Output -System. out. print (Per[x] +",") ;//because it just opens up space, so it's all the default values. - } + //is initialized for each element in the array, each of which is an object, which needs to be instantiated separately -per[0] =NewPerson ("Zhang San") ;//instantiate the first element +per[1] =NewPerson ("John Doe") ;//instantiate a second element Aper[2] =NewPerson ("Harry") ;//instantiate a third element atSystem. out. println ("\n============== Object Instantiation =================") ; - for(intx=0; x<per.length;x++) {//Loop Output -System. out. Print (Per[x].getname () +",") ;//at this point, the instantiation is complete, so the name is printed directly - } - } -}
The use of object arrays enables the management of multiple objects.
2. foreach and variable parameters
1), Foreach Loop (enhanced for loop)
How to declare:
For (type variable name: Array or collection) {
Code
}
1 classperson{2 3 Public Static voidMain (string[] args) {4 5String []names={"Ma Yi","Xiao Qiang","Xiao Ming","junming"}//Defining Arrays6 7 for(String name:names) {8 //traverse names, output to name9System. out. println (name);Ten } One A } -}
2), variable parameters: automatically transmitted to any number of parameters as required.
Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array. Note: The mutable parameter must be in the last item. When the number of mutable parameters is one more, there must be one that is not the last, so only one variable parameter is supported. Because of the variable number of arguments, Java cannot differentiate whether the passed parameter belongs to the previous variable parameter or the back parameter, so only the variable parameter can be placed in the last item, when there is the same type parameter behind it.
Features of variable parameters:
(1), can only appear at the end of the parameter list;
(2) 、... Between the variable type and the variable name, there are no spaces before and after it can be;
(3), when invoking a variable parameter method, the compiler implicitly creates an array for the variable parameter, accessing the mutable parameter in the form of an array in the method body.
classdemo{ Public Static voidmain (String [] args) {Demo d=NewDemo (); D.print ("xiaoming","Wancai","Xioaqiang","Maotou"); } Public Static voidPrint (String ...params){ for(String s:params){//variable parameter paramsSystem. out. println (s); } }}
3, there are many design patterns in Java, and now we are talking about the singleton design mode.
Singleton design Pattern: guarantees that a class has only one instance, and provides a global access point to access it (guaranteeing the uniqueness of a class in-memory objects).
How to ensure the uniqueness of the object?
1), the construction method is privatized (other programs are not allowed to create such objects with new)
2), declare an object of this class (via the new keyword)
3), provide a static method to the outside to obtain an object instance.
As follows:
class testdemo{ private Testdemo () {} // Structuring method Privatization Private static testdemo test=new Testdemo (); Declares an object of this class public static Testdemo getinstance () { return test; } // provide a static method to the external }
When do I use a singleton design mode?
1), by using in the design of the tool class
2), when there is no attribute in a class (the state of the object)
3), this class is used very frequently as a tool class.
There are two implementations of the singleton design pattern:
1), Hungry sweat type
class single{ // class One load, objects already exist, development often use this private staticsingle s= New singles (); Private Single () {} public static getinstance () { return s; }}
2), Lazy-type: (will be used during the interview)
class single2{ // static Single2 S; private Single2 () {} public static Single2 getinstance () { if (S==null =new Single2 (); return S; }}
In the getinstance () method we can make some judgments to decide whether to return the object of Single2, and realize the controllability of the singleton object.
Object-oriented Java Learning (3)