First, the basic concept
Classes are designed to allow the programming language to more clearly describe things in everyday life. Is the description of a certain kind of thing +, is abstract.
Conceptual definition: Whereas an object is a specific individual that actually exists in that kind of thing, it is also called an instance (instance).
An object is an instance of a class, a real entity, and a class can have multiple objects.
The focus of object-oriented programming is the design of classes rather than the design of objects.
Classes are encapsulated by data members and function members. A data member is called a property of a class, and a function member is called the behavior of a class.
The function is also called the method, it can simplify the structure of the program, also can save the time of writing the same code, achieve the purpose of the program modularization.
Using methods to write programs can separate the program code for a particular function, which simplifies the code and streamlines the process of code duplication.
So in summary, it is the class that encapsulates the data of something and the related function to form a special data structure to express an abstraction of the real thing.
Second, define the class
1, the definition class is actually declares the class the property and the method.
struct: [class modifier] class name
{
[modifier] Data type member variable name;
[modifier] Data type member variable name;
.
.
[modifier] Returns the value of the data type method name (parameter 1, parameter 2, ...) Method can be taken without parameters
{
Statement sequence;
return [expression];//return data type can be a type void with no return value
}
.
.
}
Where [] the contents of the expression can be omitted
2. Usage of analytic class modifiers
Class modifiers a total of four are public, abstract, final, default
A class can have more than one modifier, and there are no successive points, but abstract and final are opposites, so they cannot be applied to the definition of a class at the same time.
There can be more than one class (class) in a. java file, but only one is decorated with the public modifier, and the name of the class must be the same as the. Java filename.
| modifier |
meaning |
| public |
declare a class as a public class, It can be accessed by any object |
| abstract |
declares an abstract class, no implementation method, requires a subclass to provide a method implementation, so you cannot create an instance of the class |
| final |
The declares a class as a final class, which is a non-inheriting class, indicating that it cannot be inherited by other classes |
| default |
< strong> The default modifier, it means that only objects in the same package can use such a class |
3. Member variables
A member variable of a class can be a simple variable, or it can be structured data such as an array of objects.
[modifier] Variable type variable name [= initial value];
4. Modifiers for parsing member properties
Adornment of member properties have access control, static modifier, final modifier, over modifier, volatile modifier
A member variable can be decorated at the same time by more than two modifiers, but the game King modifier cannot be defined together.
When defining the member modifiers of a class, a read member variable can be assigned an initial value, indicating the original state of the member variable, but the action on the member variable can only be placed in the method.
| Modifier |
Meaning |
| Public |
Public access control, which specifies that the variable is public, and that it can be accessed by any method of the object. |
| Private |
The private access control character. Specifies that the variable can be accessed only by methods in the same class (the current Class), and other classes, including subclasses, cannot access this variable. |
| Protected |
Protect access control characters. Specifies that the variable can be accessed only by its own class and its subclasses or other classes in the same package, which can be overridden in subclasses. |
| Default |
When the default access control is used, it means that other classes in the same package can access this member variable, and the class in the other package cannot access the member variable. |
| Final |
The final modifier. Specifies that the value of the variable cannot be changed. |
| Static |
Static modifiers. Specifies that the variable is shared by all objects, that is, all instances can use the variable. |
| Transient |
The transition modifier. Specifies that the variable is a system-reserved, temporary variable that has no special effect. |
| Volatile |
A volatile modifier that specifies that the variable can be controlled and modified by several threads at the same time. |
5. The Member method of the class.
The method of a class is used to define the operation of a member variable of a class, a mechanism for implementing the inner function of the class, and an important window for the class to interact with the outside world, the format of which declares the method as follows:
[modifier] Returns the value of the data type method name (parameter 1, parameter 2, ...) You can not add parameters
{
Statement sequence;
return [expression];//can be a void type with no return value
}
6. Modifiers for functions that parse member methods
Modifiers include access control access modifiers, static modifiers, abstract modifiers, final modifiers, synchronization modifiers, and local modifiers
Similarly, the modifier of a method can have more than one, but the mutex modifier cannot appear at the same time.
| Modifier |
Meaning |
| Public |
Public access control characters. Specifies that the method is public, and that it can be accessed by any object method |
| Private |
The private access control character. Specifies that the method only allows access to methods of its own class, and that methods in any other class (including subclasses) cannot be methods of this method. |
| Protected |
Protect access control characters. Specifies that the method can only be accessed by its class and its or other access in the same package |
| Default |
The default access control, which means that other classes in the same package can access this member method, and that the class in the other package cannot access the Idiom method |
| Final |
The final modifier. Specifies that the method cannot be overloaded |
| Static |
Static modifiers. Specifies a method that does not require an object to be instantiated to be activated |
| Abstract |
abstract modifier. Specifies that the method declares only the method header, without the method body, and the abstract method is implemented in the subclass |
| Synchronized |
The synchronization modifier. In a multithreaded program, this modifier is used to lock the method it belongs to before it runs, to prevent other threads from accessing it, and to unlock it after the run is finished |
| Native |
Local modifier. The method body that specifies this method is written outside the program in a different language, such as C. |
7. Summarize the usage of the four access modifiers
| Scope |
Current class |
Same bundle (Package) |
Descendant class |
Other Packages (package) |
| Public |
√ |
√ |
√ |
√ |
| Protected |
√ |
√ |
√ |
X |
| Friendly (default) |
√ |
√ |
X |
X |
| Private |
√ |
X |
X |
X |
Friendly is the case of not writing the access modifier, which is the default, but in Java there is no friendly in this case, it is derived from C + +.
Access modifiers can only be public when decorating class, and default. does not eng use protected and private
When you use the access modifier to decorate the properties of a class, you typically use private, which accesses the properties of the class through getter and setter methods.
8, the difference between the member variable and the local variable
By definition of classes and methods, you can define your own variables in both classes and methods. A variable defined in a class is a member variable, and a variable defined in a method is a local variable.
(1), from the grammatical form, the member variable is the attribute class, and the local variable is defined in the method of the variable or the parameter of the method, the member variable can be public, private, static and other modifiers, while the local variable cannot be modified by the access modifier and static , member variables and local variables can all be modified by the final.
(2), from the memory of the variable in the storage mode, the member variable is part of the object, and the object is present in the heap memory, and the local variable is present in the stack memory.
(3), from the time the variable is in memory, the member variable is part of the object, it exists as the object is created, and the local variable is generated as the method is called, and disappears automatically as the method ends
(4), if the member variable is not assigned, the default value of the type is automatically assigned (in one case, the member variable that is not modified by the final Xishi must be explicitly assigned), and the local variable is not automatically assigned and must be explicitly assigned before it can be used.
Third, the creation and use of objects
1, the object of the whole object-oriented programming of the theoretical basis.
2, the declaration period of an object: Create, use, destroy
3. To create a class of object steps:
(1), declaring a variable that points to ' objects created by the class '
(2), using the new operator to create an object and assigning it to the previously created variable
For example: Object obj = new object ();//From a memory point of view, a variable, obj, is defined in the stack memory to point to a heap of object objects created in heap memory by the new operator. That is, the variable obj is a reference variable to the object stored in the heap memory because it is a pointer to the object created by the object class, so it can be thought of as the name of the object, and thus simply the object;
4. Because a variable defined in a method must be initialized, it cannot be compiled, so when an object is created, the initial value of the member variable is followed by its base type.
5. The format for referencing object members through objects is as follows:
Object. Object member
6, anonymous object is, do not define the object reference variable, directly with the object created by new to invoke the method of the object and so on. such as New Object (). XXX ();
7. Two cases of using anonymous objects:
(1) If you need to make a method call to an object only once, you can use an anonymous object.
(2), when an anonymous object is passed as a parameter to a method call, it can be used.
Java Classes and objects