Key words: Face object, class, construction method, this, static, inner class One, the concept of facing the object: The problem of the installation of a fixed set of rules divided into multiple independent objects, and then by invoking the method of the object to solve the problem. Its characteristics can be summarized as encapsulation, inheritance, polymorphism. 1, encapsulation: In the face of the core of the object, the properties and behavior of the object is encapsulated, do not need to let the outside world know the details of implementation, this is the encapsulation of thought. 2, Inheritance: The main description is the relationship between classes and classes, through inheritance, you can without rewriting the original class, the function of the original class to extend. 3, polymorphism: is allowed to appear in the program duplicate name phenomenon, it refers to a class defined in the properties and methods are inherited by other classes, they can have different data types or behave differently, which makes the same properties and methods in different classes have different semantics. Class and Object 1, a class is an abstraction of an object, which is used to describe the common characteristics and behavior of a set of objects. A class can define member variables and member methods, where member variables are used to describe the characteristics of an object, also known as an attribute, and member methods are used to describe the behavior of an object, which can be referred to simply as a method. How to create a class:
1 class person{2 //Define a variable of type int 3 int age;4 //define Speak () method 5 void speak () {6 SYSTEM.OUT.PRINTLN ("* * * }8}//person class name, age is a member variable, speak () member method
2. Creation and use of objects: Create: Class Name Object name = new class name ();//Instance Object use: Object reference. Object member; When instantiating an object, the Java virtual opportunity automatically initializes the member variable, assigning different initial values to the different types of member variables.
Table: Initial values for member variables
Member Variable Class |
Initial value |
Member Variable Class |
Initial value |
Byte |
0 |
Double |
0.0D |
Short |
0 |
Char |
Null character, ' \u0000 ' |
Int |
0 |
Boolean |
False |
Long |
0L |
Reference data type |
Null |
Float |
0.0F |
|
|
In Java, NULL is a special constant that, when the value of a variable is NULL, indicates that the variable does not point to any object and becomes garbage collected. 3, the so-called encapsulation of the class refers to the definition of a class, the property in the class is privatized, that is, using the private keyword to decorate, the private property can only be accessed in its own class. In order for the outside world to access private properties, you need to provide some common methods that use public adornments, including the GetXXX () method used to get the property and the Setxxx () method to set the property worth.
1 classstudent{2 private String name; Privatize the Name property 3 private int age; Privatize The Age attribute 4//Below is the public getxxx () and Setxxx () method 5 publiclystring GetName () {6 return name; 7 } 8 public void SetName (string stuname) {9 NA me = stuname; }11 public int getage () { n. }14 public void setage (int stuage) {1 5//The following is an inspection of the incoming parameters(stuage<=0) {System.out.println ("Age is not legal");}else {20 = stuage;//Assigning a value to a property }21 }22 public void introduce () {System.out.println ("Hello everyone, my name is" +name+ ", I Am" +age+ "year old!) ); }25 }26 public class example01{27 public static void main (string[] args) {Student Stu = n EW Student (); Stu.setage ( -30); Stu.setname ("Li Fang"); stu.introduce (); }33}
Third, construction Method 1, construction method: The constructor method is a special member of the class, it will be called automatically when the object is instantiated. 2, the structure method characteristic: The ① method name and the class name are the same;
② no declaration of the return value type before the method name;
③ cannot return a value in a method using the return statement;
1 class person{2 //Construction Method 3 Public person () {4 } 6 Public person (int. ) {7 Age = A; } 9 public void}13 public class example{14 public static void }18}
3, the construction method of overloading: As with the normal method, the method name is the same, only the parameter type or the number of parameters can be different. In general, construction methods are typically decorated with public. Four, this keyword 1, this keyword three common methods:
① The This keyword allows you to explicitly access the member variables of a class to resolve problems with local variable name collisions.
1 class person{2 int Age , 3 public person (int. ) {4 this.age = age; Access member variable 5 } 6 public int getage () {7 return this . Age; 8 } 9}
② calls the Member method through the This keyword.
The ③ constructor method is called automatically by a Java virtual machine when instantiating an object, and cannot be called in a program like any other method, but it can be called in a constructed method using the form "This (" parameter 1, Parameter 2 ... ")" to invoke the other constructor method.
Note the point:
① can only be used in constructor methods to call other constructors, and cannot be used in member methods.
② in the constructor method, the statement that uses this call to construct the method must be in the first row and only once.
③ cannot use this to call each other in the two constructor methods of a class.
Static keyword 1, use the static keyword to decorate the member variable, the variable is made static variable. Static variables are shared by all instances and can be accessed using the "class name. Variable" form. Note: The static keyword can only be used to decorate member variables and cannot be used to decorate local variables. 2. Static method: Add the Static keyword before the method defined in the class to invoke a method without creating an object. It can be accessed in the form of class name. Method Name. Note: You can access only static decorated members in a static method, and any object may be created when a static method is called. 3. Static code block: a block of code decorated with the static keyword is called a static code block, and when the class is loaded, the static code block is executed, because the class only loads exceptions, so the static code block can only be executed once. 4. Single Case mode:
1 class single{2 private static single INSTANCE = new single (), 3 private single () {} 4 public STA Tic single getinstance () { 5 return INSTANCE; 6 } 7 } 8//The above singleton can also be written in the following form 9 class single{10 private Single () {}11 public static final Single INSTANCE = New single ();
/* Variable name instance preceded by three modifiers, where public's role is to allow external direct access to the variable, and static is used to let external
"Class name. Variable name" To access the variable, the final function is to prohibit external modification of the variable. */12 }13 class Example {public static void main (string[] args) {A single s = single.getinstance (); The getinstance () method is the only way to obtain an instance object of single class, which is a singleton class of }18}
The variable with the final modifier of the keyword is a constant and its value is immutable. Inner class: The inner class is divided into member inner class, Static inner class, method inner class according to the position, modifier and definition of the internal classes. 1. Create the specific syntax format for the inner class object: The outer class name. Internal class name Variable name = new external class name (). New inner Class ();
1 class outer{2 private int num = 4; Defines a member variable of a Class 3//The following code defines a member method that accesses the inner class 4 public void Test () {5 Inner Inner = new Inner (); 6 inner.show (); 7 } 8//The following code defines a member inner Class 9 class inner{10 void Show () {11///member variable that accesses an external class in a method of a member inner class of System.out. println ("num =" + num); }14 }15 }16 public class Example16 {n public static void m Ain (string[] args) {Outer Outer = new Outer ();//Create external Class object Outer.test ();//Call Test () method }21 }22//Create an inner class object directly Example of the public class Example16 {string[] args) {Outer.Inner Inner = new Outer (). Inner (); Create an inner class object Inner.show (); Call the Show () method }28 }29//When the inner class is declared private, the outside world will not be able to access it.
2. Create a concrete syntax format for the static inner class object: The outer class name. Internal class name Variable name = new External class name. Internal class name ();
1 class outer{2 private static int num = 6; Defines a member variable for a Class 3 //The following code defines a static inner class 4 static class inner{5 void Show () {6 System.out.println ("num = "+num"); 7 } 8 } 9 }10 public class Example16 {one public static void main (string[] args) {Out.inner Inner = new Out.inner (); Create an inner class object Inner.show (); Method of calling inner class }15}
Note: ① can only access static members of external classes in a static inner class.
② can define static members in static inner classes, while static members are not allowed in non-static inner classes. 3, Method inner class: Refers to the class defined in the member method, it can only be used in the current method. The inner class of the method can access the member variables of the external class.
Getting Started with Java--(2) facing objects (top)