first, the package
Features: Safe, isolate the changes, easy to use, provides repeatability
Example
[[email protected] java]# cat employeedemo.java class employee { private String id; private String name;    PRIVATE STRING GENDER;     //provides public set and get methods //public member modifiers, public, anyone can access //private member modifiers, private, only themselves can access public void setname (string n) { name = n; } public void setid (string i) { id = i; } public void setgender (String gen) { if ("Male". Equals (gen) | | "female". Equals (gen))  {   &Nbsp; gender =gen; } else { System.out.println ("please input \" male\ " or \" female\ ""); } } public string getname () { return name ; } public string getid () { return id; } Public string getgender () { return gender; } public void work () { System.out.println (id + ":" +name + ":" +gender + ":" + "working"); } } public class employeedemo {public static void main ( String[] args) { //Create Objects employee tom = new employee (); //calls the public method, assigns the member the variable tom.setname ("Tom"); tom.setid ("1"); Tom.setgender ("male"); //gets the value of the instance variable system.out.println (Tom.getname ()); System.out.println (Tom.getid ()); system.out.println ( Tom.getgender ()); //Call member Method tom.work (); } } //Running Results [[email protected] java]# java EmployeeDemo Tom1male1:tom:male:working
[[email protected] java]# cat demo.java class calculator { public String name = "My carculator"; public Double num1; public double num2; public char option; public void init (double a, char op, DOUBLE B) { num1 = a; num2 = b; option = op; } public void calculate () { switch (option) { case ' + ': system.out.println (name + ":" +num1 + "+" +num2 + "= " + (num1+num2)); break; case '-': system.out.println (name + ":" +num1 + "-" +num2 + "=" + (num1-num2)); break; case ' * ': system.out.println (name + ":" +num1 + "*" +num2 + "=" + (num1*num2)); break; case '% ': system.out.println (name + ":" +num1 + "%" +num2 + "=" + (num1%num2)); &NBsp; break; case '/': if (num2 != 0) { system.out.println (name + ":" +num1 + "/" +num2 + "=" + (num1/num2) "); } else { system.out.println ("num2 can not be 0 !"); } break;     DEFAULT : SYSTEM.OUT.PRINTLN ("null"); } } } class Demo { Public static void main (String[] args) { calculator cal = new calculator (); Cal.init (10 , '% ' ,3); cal.calculate (); system.out.println ("Bye"); } } //Run result [[email protected]&Nbsp;java]# java demomy carculator:10.0%3.0=1.0bye
Second, the construction method
1. Definition:
When an object is created, there is no value:
When an object is created, it has a value:
construction Method Effect: initializing an object
2. features of the constructor:
1) Called by the JVM when the object is established , initializes the object
2) Called when an object is created
3) The function name is consistent with the class name
4) No return value type
5) When a constructor is not defined in the class, the system defaults to the constructor of an empty argument, and the
6) Multiple constructors can be defined in a class for different initializations, in the form of Overloads
3. Structure the code block features:
1) Unified initialization for all objects
2) Run when object is created, take precedence over constructor
3) generally used to extract the public information from all construction methods
4) Differences from constructors: differences between all objects and parts of objects
Example
[[Email protected] java]# cat demo9.java class boy { string name; int age; String gender; // Constructing code blocks, Initialize all Objects { system.out.println ("Cry ... "); } boy () {  SYSTEM.OUT.PRINTLN ("Non-parametric construction"); } boy (string n, int a, STRING G) { name = n; age = a; gender = g; system.out.println ("With reference structure"); } void run () { system.out.println ("Run ..."); } } public class demo9 { public static&nbSp;void main (String[] args) { system.out.println (); boy b = new boy (); boy b2 = new boy ("Jack", 1, "male"); }} //run result [email protected] java]# java Demo9 cry ... Non-parametric construction cry ... With a reference structure
Third, This Key Words
1) A reference to the object to which the function belongs
2)This can be used only in non-static (no static modifier) functions
3) Calls between constructors must be placed in the first statement of a constructor
4) resolves the problem with the same name for object properties and function parameters in constructors
Class Person {String name; int age; Person () {} person (String N) {name=n; } person (String n, int a) {//constructor cannot be directly called by function name, compile Error!!! Person (n); age = A; }}
example
Print this in the constructor and print the object name p when you create the object, as the result is that this and P are the same as the memory address value class student { string name; String gender; int age; student () { } student (String name) { this (); this.name = name; } student (String name, String gender, int age) { this (name); system.out.println (This); //[email protected] this.gender = gender; this.age = age; } void speak () { run (); system.out.println ("Name:" + name + " Gender:" + gender + " Age:" + age + " hehe"); } Void run () { system.out.println ("Run ....."); } } class Demo2 { public static void Main (String[] args) { Student p = new Student ("Tom", "male", 28); system.out.println (p); // [email protected] p.speak (); }} //Running Results [[email protected] java]# java demo2[email protected][email Protected]run ..... Name:tom Gender: Male Age:28 hehe
Java encapsulation, construction method, this