Object-Oriented
A man-made abstract programming model in understanding the object-oriented code to be appreciated that according to the abstract model, not just to understand the complex issues from the literal codes, learn a separate split into a small problem, solved by each of the small problem, and finally solved a big problem
class
Class is abstract things, arithmetic, logic, and other concepts, used to package this type of related data and method code, packaged as separate components, can be understood as a template class, or drawing. When creating the class object is equivalent to pressing this template to create
Objects
Specific examples from the class to create, for each separate instance of occupied memory space, storing respective attribute data, examples can be individually controlled, so that the specified instance of the code to execute the method.
Quote
Save an instance of an object reference variable memory addresses, the address values assigned to variables, variables used to call an instance, can be understood as a remote control, just use references to operate on the line.
Here an example to understand:
Project: day0101_ soldiers
Class: day0101.Test1 class and class Solder
Solder Class Code:
. 1 Package day0101; 2 . 3 . 4 . 5 Import java.util.Random; . 6 . 7 . 8 . 9 / * 10 . 11 * package 12 is 13 is * data, a logical operation method, 14 15 * packaged as a "class" component 16 . 17 * / 18 . 19 public class Solder { 20 is 21 is // member variable 22 is 23 is int ID; // default value of 0 24 25 int Blood = 100 ; 26 is 27 28 //Method members 29 30 public void Go () { 31 is 32 System.out.println ( the this .id + "number of soldiers marching" ); 33 is 34 is } 35 36 public void Attack () { 37 [ 38 is IF (Blood == 0 ) { 39 40 System.out.println ( 41 is 42 is "this is the" + id + "number of bodies of the soldiers' ); 43 is 44 is return ; // method to this end 45 46 is } 47 48 System.out.println (ID +" soldiers attack No. " ); 49 50 // analog injury, hypovolemic 51 is 52 is // generates a random blood Save 53 is 54 is int D = new new the Random () the nextInt (10. ); 55 56 is Blood - = D; // hypovolemic 57 is 58 IF (Blood <0) { // if the blood becomes negative, reset to 0 59 60 Blood = 0 ; 61 is 62 is } 63 is 64 System.out.println ( "blood:" + Blood); 65 66 IF ( == 0 Blood ) { 67 68 System.out.println (the above mentioned id + "No soldiers were killed."); 69 70 } 71 72 } 73 74 }
Test1 class code:
Package day0101; public class Test1 { public static void main (String [] args) { // create a new instance Soldier // then its memory address value stored to variable s1 / * s1 = { * ID: 0, * Blood: 100 *} * / Solder s1 = new new Solder (); / * S2 = { * ID: 0, * Blood: 100 *} * / Solder S2 = new new Solder (); // find soldiers with s1 variable memory space, access its id variable s1.id = 9527 ; s2.id= 9528 ; // find variables s1 soldier with memory, to perform it go () method code s1.go (); s2.go (); s2.attack (); s1.attack (); } }
Construction method
When creating a new instance, a special method for performing the new Solder ()
There must be a Java class constructor, if they do not define a constructor method, the compiler will add a default empty constructor
class Solder{ public Solder(){ } }
The above code is an empty constructor add their own, when you do not add is that the compiler is automatically generated empty constructor, to do nothing
The role of the constructor
After creating an instance, a particular method performed immediately constructor code can be written in any completion of any function or perform any operation, the constructor general role is to the member variable. Member variables are defined variable in the class.
this usage
this.id
id is the member variables, member variables when given an assignment by the constructor:
public class A{ int id; public A(int id){ this.id=id; } }
this.id represents the member variable id.
this(a,b)
a, b are parameters in parentheses, this (a, b) represents two call parameters as constructor
public class A{ int id; int a; public A(int id){ this(id,5) } public A(int id,int a){ this.id=id; this.a=a ; } }
this () to be in the first line of code.