Object Oriented (a)
Object-oriented - Encapsulated
Class declaration format
public class Student { // Access Modifiers Modifier + class + class name public String name; // class attribute // member variables, global variables // access modifier + + variable name data type public void info () { / / access modifier + + return value of the method name // similar method, you can call their own attributes the this .name; // the this: current object // If not this, then follow the principle of proximity } }
In the main method call
-
new Object / Attribute
STU = Student new new Student (); // Object name = + object + object instance
-
Assignment
= stu.name "fr" ; // . exemplary object object attribute = value + attribute
Stu assignment made by the object, by '' are connected.
-
Object array assignment
Student [] stus = new Student[5]; stu[0] = new Student(); //或者 Student [] stus = {Student(),Student(),Student(),null,null}
Construction method
default:
public class name () {} // This method comes default
Features:
-
No return type
-
The same as the class name and method name
Precautions
-
In a class, java compile time compile the value of the property, the value of the constructor call, that is, if there constructor parameter assignment, and its value will override the default setting of the initial value.
-
Constructor arguments override the default constructor with no arguments, you write a constructor with parameters, it is best to write a constructor with no arguments
-
In the configuration process may be alternatively constructed by the this method name, its parameter types retrieval, number, sequentially consistent constructor (only in this class)
-
Constructors can not be rewritten, inheritance, and a final modification
-
this () and super () must be placed on the first line of the constructor
Access modifier
Modifiers | Scope | |
---|---|---|
private | private | Only in this category |
default | default | The same package analogy public, there are differences in different packages |
protected | under protection | Public access to different packages and different, inherit different packages, there are differences with the default |
public | public | Can access anywhere |
-
It is noteworthy that private, default, protected between two unrelated classes in different packages is not accessible!
final
-
Modifying a variable, a constant, must be given an initial value, generally associated with static
public static Final data type uppercase constant names
-
Modification methods, methods for the final method can not be overridden.
-
Modified class, the class is final class can not be inherited
static
-
Only a static variable value
-
Static methods can only refer to a static variable, you can not refer to non-static (reference belong to the same area in memory)
-
Static variables are placed in static storage block
-
Static Property belongs to the class, only one value (call it belongs to the class as a whole rather than individual objects)
General Dingding Yi assignment is: static class name property name
-
Class initialization, the first initial static property or method
-
If the property has been given too static value, not in the subsequent new creation (initialization process only once)
-
String = "" value stored in the static area
-
static not be used in this, super
-
Static inoperable non-static
Static block
format:
static { }
This class module loaded into the virtual machine when called before the constructor execution
Entity Block / statement block
{
}
Performed after static
javabean-
Writing data format specification
javabean private use defined attributes, which makes the barrier properties of the package can no longer be accessed.
In order to solve, using indirect methods, the definition of get, set method.
-
Only modified data set name (parameter type) {} this data parameter = name;.}
-
Get read-only data name () {return this. Data Name}