Object-oriented programs are composed of objects, each containing a specific part of the functionality exposed to the user and a hidden implementation part. In object-oriented programming (OOP), it is not necessary to care about the concrete implementation of the object. In the traditional structural programming, the algorithm is the first, the data structure is the second, that is, first determine how to operate the number, and then consider how to organize data to facilitate operation. OOP, in turn, reverses the order, puts the data first, and then considers the algorithm for manipulating the data.
First, the class
A class is a template and blueprint for constructing an object. In layman's parlance, the class is equivalent to a building's drawing, and the object is equivalent to a building. The process of constructing an object from a class is called an instance of creating an object.
In Java, class is defined by keyword class, followed by the class name. For example:
Class person{
//body content
}
Defines a person class.
1. Fields and Methods
When defining a class, you can set two types of elements in a class: data members and member functions. Where the data member is an object and can be of any type. If it is a handle to an object, you must initialize the handle and connect it to an actual object through the constructor. If it is a basic data type, it can be initialized directly at the location defined by the class.
Each object retains storage space for its own data member, and data members are not shared among objects.
Class person{
String name;
double salary;
}
newcreates an object from a keyword. Such as:
Person p = new person ();
(1) Default values for primary members
A primary data type belongs to a class member, so it is not explicitly initialized, or you can guarantee that they get a default value.
| Main type |
Default value |
| Boolean |
False |
| Char |
' \u0000 ' (null) |
| Byte |
(byte) 0 |
| Short |
(short) 0 |
| Int |
0 |
| Long |
0L |
| Float |
0.0f |
| Double |
0.0d |
If a variable is a member variable of a class, it must be initialized for the primary type, with particular attention to the initialized value. For local variables, you get some random values, such as: int x; do not automatically initialize to 0.
(2) method
The basic components of a method include name , argument , return type , and body . The basic form of the method is:
返回类型 方法名(/*自变量列表*/) {/* 方法体 */}
The return type refers to the numeric type returned after the method is invoked. a method name is an identity and reference to a specific method. the argument list lists the type and name of the information you want to pass to the method.
In Java, a method is invoked in the form of对象名.方法名
1. Manipulate objects with handles
Although Java is fully object-oriented . However, the identifier of the operation actually points to the "handle" of an object , also known as " reference ." A handle can exist independently, not to say that having a handle requires an object to correspond to it. For example, int i; defining an integer variable i does not mean that it must have a value. For example, create a handle of type string:
This just creates the handle and does not create the object. Because S does not have an object associated with it, an exception will occur if you call S. Therefore, initialization is required when creating.
In Java, an object is created with the new keyword, which returns a reference to the object (that is, a handle). So
string s = new string ("Hello");
A string object with the content "Hello" is created and the handle (reference) of the object is handed to s for save.
2. Location of data storage
1. Registers. The fastest save area, located inside the processor. Registers are limited in size and are allocated by the compiler.
2. Stack. resides in the regular RAM (random access Storage) area, which can be resized by the stack pointer. The stack pointer moves down, creates a new memory space, and moves up to free up memory space. When you create a program, the Java compiler must accurately know the "length" and "time of existence" of all the data stored in the stack, and the compiler generates the appropriate code to move the pointer. The handle of the Java object is saved in it, but the Java object is not saved on the stack.
3. Heap. A general-purpose memory pool in which Java objects are saved. The compiler does not know and does not need to know how much storage space is allocated from the "Memory heap" or "heap", and how long the data persists.
4. Static storage. Static refers to a fixed position (in RAM). When the program is running, the data that is stored statically is ready for invocation. Indicates that a specific element of an object is static through the static keyword. Java itself could not have been placed in a static storage space.
5. Constant storage. Constant values are usually placed inside the program code and will never change.
6. Non-RAM storage. If the data is completely independent of a program, the program can still exist when it is not running, and within the control of the program. For example, streaming objects and pinned objects.
Second, the object
Three main characteristics of the object:
• Object behavior-What actions can be applied to an object, or what methods can be applied to an object?
• The state of the object-how does the object respond when you add those methods?
• Object identification-How do I identify different objects with the same behavior and state?
The behavior of an object is defined by a callable method. Each object holds information that describes the current feature, which is the state of the object. The state of the object does not change spontaneously. A well-designed class whose objects can be implemented only by calling methods, which can change the state of the object without a method call, indicating that the encapsulation is corrupted.
Encapsulation: Combining data and behavior in a package and hiding the implementation of the data for the object's consumers. The key to encapsulation is that it is absolutely impossible to have methods in the class directly access the instance fields of other classes. Programs interact only through the object's method domain object data.
The data in an object is called an instance field (instance field) or a property. The process of manipulating data is called a method. For each particular object, there is a specific set of instance field values (property values), and the collection of those values is the current state of the object.
The relationship between classes
Common relationships between classes:
• Dependency ("Use-a"): A class's method manipulates objects of another class.
• Aggregation ("Has-a"): an object of a class containing an object of another class.
• Inheritance ("Is-a"): used to represent special and general relationships. If Class A extends Class B (a inherits B), Class A contains not only the method of Class B, but also the method of extension.
An object does not actually contain an object, but simply refers to an object.
In Java, the value of any object variable is a reference to an object that is stored in another place. The return value of the new operator is also a reference.
The above comprehensive understanding of Java classes and objects is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.