Encapsulate private
We observe the following code
class Person{ private String name ; // Indicate name private int age ; // Indicates age void tell(){ System.out.println("full name:" + name + ";Age:" + age) ; } }; public class Demo{ public static void main(String args[]){ Person per = new Person() ; per.name = "Zhang San" ; per.age = -30 ; per.tell() ; } };
There is no syntax error in the above operation code, but there is a logic error (age - 30 years)
In development, in order to avoid logic errors, we suggest encapsulating all attributes and providing setter and getter methods for setting and obtaining them
Operation.
class Person{ private String name ; // Indicate name private int age ; // Indicates age void tell(){ System.out.println("full name:" + getName() + ";Age:" + getAge()) ; } public void setName(String str){ name = str ; } public void setAge(int a){ if(a>0&&a<150) age = a ; } public String getName(){ return name ; } public int getAge(){ return age ; } }; public class OODemo10{ public static void main(String args[]){ Person per = new Person() ; 2.2,this 2.3,static summary a key: per.setName("Zhang San") ; per.setAge(-30) ; per.tell() ; } };
this
In the foundation of Java, the this keyword is one of the most important concepts. Use this keyword to complete the following operations:
·Calling properties in a class
·Calling a method or constructor in a class
In a construction method, when calling another construction method, the code to be invoked must be written in the first line of the construction method.
·Represents the current object
Person2 p1 = new Person2("Zhang San",18); Person2 p2 = new Person2("Li Si",18); p1.say(); p2.say(); Person2 p3 =new Person2(); p3.say(); } } /** * this Refers to the current object * @author 23802 * */ class Person2{ private String name; private int age; Person2(){ this("Nobody",110); } Person2(String name,int age){ this.name = name; this.age = age; } void say() { System.out.println("full name:"+name+"Age:"+age); } }
Static static
The method or variable modified by the static keyword does not need to rely on the object for access. As long as the class is loaded, it can be accessed through the class name. And it will not create multiple copies of data in memory because of multiple creation of objects
a key:
one Static members are loaded and initialized when the class is loaded.
2. No matter how many objects exist in a class, there is always only one static attribute in memory (it can be understood that all objects are common)
3. When accessing: static cannot access non static, non static can access static!
package secondSecond; public class Demo5 { public static void main(String[] args) { Emp.region = "Beijing"; Emp E1 = new Emp("Zhang San"); Emp E2 = new Emp("Li Si"); Emp E3 = new Emp("WangTwo "); Emp E4 = new Emp("Pockmarks"); E1.say(); E2.say(); E3.say(); E4.say(); //Suppose the company moves to Tianjin Emp.region = "Tianjin"; E1.say(); E2.say(); E3.say(); E4.say(); System.out.println(Emp.count); } } class Emp{ private String name; static String region; static int count; Emp(){ count++; } Emp(String name,String region){ count++; this.name = name; this.region = region; } Emp(String name){ count++; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } void say() { System.out.println("Employee name:"+name+",Employee address:"+region); } }
/** * When a statically decorated method is called, it is possible that the object has not been created */ static void say() { System.out.println("Stay until autumn, September 8,I kill all flowers when they bloom"); } void say2() { System.out.println("The sky fragrance array penetrates Chang'an, and the city is full of golden armor"); say(); }
Code block
package secondSecond; public class Demo7 { public static void main(String[] args) { /** * Write code blocks in a sequentially executed code flow */ { //int a = 10; //System.out.println(a); } Poo p1 = new Poo(); Poo p2 = new Poo(); } } class Poo{ private String name; private String age; /** * Construct the code block, execute it once with each creation of the object, and execute it before constructing the method * Different from the construction method: * No matter which construction method the user calls to create the object, the construction code block must be executed */ { System.out.println("Execute 1 on object creation"); } /** * Static code block, with the class loading (first use), the static code block is executed only once * Because the class is loaded only once, the static code block is executed only once */ static { System.out.println("Static code block execution"); } public Poo() { System.out.println("Execute 2 on object creation"); } public Poo(String name) { System.out.println("Execute 2 on object creation"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } void say() { System.out.println("name:"+name+"Age"+age); } }
main method
·Public: represents public content, which can be called by all operations
·Static: indicates that the method is static and can be called directly by the class name. java StaticDemo09
·void: indicates that there is no return value operation
·Main: the method name specified by the system. If main is written incorrectly or not, an error will be reported: NoSuchMethodError: main
·String[] args: string array, receiving parameters