An object-oriented program is composed of objects, each object containing specific functional parts exposed to the user and hidden implementation parts. In object-oriented programming (OOP), there is no need to care about the specific implementation of the object. In traditional structured programming, algorithms are first-ranked and data structures are second-ranked, that is, first determine how to operate, and then consider how to organize data to facilitate operation. OOP reverses this order, puts the data first, and then considers the algorithm that operates the data.
I. Class
Classes are templates and blueprints for constructing objects. In layman's terms, the same is equivalent to the drawings of the building, while the object is equivalent to the building. The process of constructing an object from a class is called creating an instance of an object.
In Java, "class" is defined by the keyword class, followed by the class name. For example:
class Person{ // class body content}A Person class is defined.
1. Fields and methods
When defining a class, two types of elements can be set in the class: data members and member functions. Where the data member is an object, which can be of any type. If it is a handle pointing to an object, this handle must be initialized and connected to an actual object through the constructor. If it is a basic data type, it can be initialized directly at the class definition location.
Each object retains storage space for its own data members; data members are not shared among objects.
class Person{ String name; double salary;} Create an object with the new keyword. like:
Person p = new Person();
(1) The default value of the master member
If a certain master data type belongs to a class member, it is not clear that initialization can also ensure that they obtain 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 the class, it will definitely be initialized for the main type, and special attention should be paid to the initialized value. For local variables, some random values will be obtained, such as: int x; will not be automatically initialized to 0.
(2) Method
The basic components of a method include name , independent variable , return type , and body . The basic form of the method is:
返回类型方法名(/*自变量列表*/) {/* 方法体*/}The return type refers to the numeric type returned after the method is called. A method name is the identification and reference of a specific method. The argument list lists the types and names of the information you want to pass to the method.
In Java, the method is called as对象名.方法名
1. Use handles to operate objects
Although Java is completely object-oriented . However, the identifier of the operation actually points to an object's " handle ", also known as a " reference ". A handle can exist independently, not to say that if you have a handle, you must have an object corresponding to it. For example, int i; defines an integer variable i, which does not mean that it must have a value. For example, create a String handle:
String s;
Here, just a handle is created, and no object is created. Since s does not have an object associated with it, an exception will appear if s is called. Therefore, initialization is required when creating.
String s = "hello";
In Java, an object is created with the new keyword, which returns a reference to the object (i.e., a handle). so
String s = new String("hello");A String object with content "hello" was created, and the handle (reference) of the object was handed over to s to save.
2. Location of data storage
1. Register. The fastest saving area, located inside the processor. The size of the register is limited, and the size is allocated by the compiler.
2. Stack. Resides in the regular RAM (random access memory) area, the size can be changed through the "stack pointer". Moving the stack pointer down will create a new memory space; moving up will free up memory space. When creating a program, the Java compiler must know exactly the "length" and "time of existence" of all data stored in the stack, and the compiler generates the corresponding code to move the pointer. The handle of the Java object is stored in it, but the Java object is not stored in the stack.
3. Heap. A memory pool for regular purpose, where Java objects are saved. The compiler does not know or needs to know how much storage space is allocated from the "memory heap" or "heap" and how long the data will last.
4. Static storage. Static refers to a fixed position (in RAM). During the program's running, the statically stored data will be available for call at any time. Indicates that a specific element of an object is static by static keyword. Java itself cannot be placed in static storage space.
5. Constant storage. Constant values are usually placed inside the program code and never change.
6. Non-RAM storage. If the data is completely independent of a program, it can still exist when the program is not running and is within the control scope of the program. For example, streaming objects and fixed objects.
2. Object
Three main characteristics of an object:
•Object behavior – What operations can be applied to the object, or what methods can be applied to the object?
• Object status - How does the object respond when adding those methods?
•Object Identification – How to identify different objects with the same behavior and state?
The behavior of an object is defined by a callable method. Each object holds information describing the current feature, which is the state of the object. The state of the object does not change spontaneously. A well-designed class can only be implemented by calling methods. If the state of the object is changed without method calls, it means that the encapsulation has been destroyed.
Encapsulation: An implementation method that combines data and behavior in a package and hides data from the object's consumers. The key to encapsulation is that methods in the class must not directly access the instance domain of other classes. Programs interact only through the object's method domain object data.
The data in an object is called an instance field or attribute. The process of manipulating data is called a method. For each specific object, there is a specific set of instance domain values (attribute values), and the set of these values is the current state of the object.
Relationship between classes
Common relationships between classes:
•Dependency ("use-a"): A method of one class manipulates objects of another class.
•Aggregation ("has-a"): An object of one class contains an object of another class.
•Inheritance ("is-a"): is used to represent the relationship between special and general. If class A extends class B (A inherits B), class A not only contains methods of class B, but also methods of extension.
An object does not actually contain an object, but only refers to an object.
In Java, the value of any object variable is a reference to an object stored in another place. The return value of the new operator is also a reference.
The above article comprehensively understands Java classes and objects is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.