Home > Java > javaTutorial > Comprehensive understanding of Java classes and objects

Comprehensive understanding of Java classes and objects

高洛峰
Release: 2017-01-20 17:47:10
Original
1187 people have browsed it

Object-oriented programs are composed of objects, and each object contains specific functional parts exposed to users and hidden implementation parts. In object-oriented programming (OOP), you don't have to worry about the specific implementation of the object. In traditional structured programming, the algorithm comes first, and the data structure comes second. That is, first determine how to operate the numbers, and then consider how to organize the data to facilitate operations. OOP reverses this order and puts the data first, and then considers the algorithm for operating the data.

1. Class

Class is the template and blueprint for constructing objects. In layman's terms, classes are equivalent to architectural drawings, and objects are equivalent to buildings. The process of constructing an object from a class is called creating an instance of the object.

In Java, "class" is defined through the keyword class, followed by the class name. For example:

class Person{
  // 类的主体内容
}
Copy after login

defines a Person class.

1. Fields and methods

When defining a class, you can set two types of elements in the class: data members and member functions. The data member is an object and can be of any type. If it is a handle pointing to an object, the 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 where the class is defined.

Each object retains storage space for its own data members; data members are not shared between objects.

class Person{
  String name;
  double salary;
}
Copy after login


#Create an object through the new keyword. For example:

Person p = new Person();
Copy after login


(1) The default value of the main member

If a main data type belongs to a class member, then it is not explicitly initialized. It is also guaranteed that they get a default value.

Comprehensive understanding of Java classes and objects

If a variable is used as a member variable of a class, then the main type will definitely be initialized. Pay special attention 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 variables, return type and body. The basic form of the method is:

Return type method name (/*argument list*/) {/* method body*/}

The return type refers to the return after calling the method numeric type. The method name is an identification and reference to a specific method. The argument list lists the type and name of the information you want to pass to the method.

The calling form of a method in Java is the object name. Method name

1. Use handles to operate objects

Although Java is completely object-oriented. However, the identifier of the operation actually points to a "handle" of an object, also known as a "reference". Handles can exist independently. It does not mean that having a handle must have an object corresponding to it. For example, int i; defines an integer variable i, but it does not mean that it must have a value. For example, create a handle of type String:

String s;
Copy after login


Here, only the handle is created, but no object is created. Since s has no object associated with it, an exception will occur if s is called. Therefore, initialization is required when creating.

String s = "hello";
Copy after login


In Java, when you create an object through the new keyword, it will return a reference (i.e. handle) to the object. So

String s = new String("hello");
Copy after login


# created a String object with the content "hello" and gave the object's handle (reference) to s for storage.

2. Data storage location

1. Register. The fastest save area, located inside the processor. Registers are limited in size, allocated by the compiler.

2. Stack. Resides in a regular RAM (random access memory) area and can be changed in size via the "stack pointer". Moving the stack pointer downward creates new memory space; moving it upward releases memory space. When creating a program, the Java compiler must know exactly the "length" and "age" of all data saved on the stack, from which 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 that holds Java objects. The compiler does not know or need to know how much storage space is allocated from the "memory heap" or "heap" and how long the data is retained.

4. Static storage. Static means located in a fixed location (in RAM). While the program is running, the statically stored data will be available for recall at any time. The static keyword indicates that a specific element of an object is static. Java itself cannot be placed in static storage space.

5. Constant storage. Constant values ​​are usually placed inside program code and never change.

6. Non-RAM storage. If the data is completely independent of a program, it can exist when the program is not running and is within the control of the program. For example, streaming objects and fixed objects.

2. Objects

Three main characteristics of objects:

•Object behavior—What operations can be applied to the object, or what methods can be applied to the object?

•The state of the object - how did the object respond when those methods were added?

•Object Identification - How to identify different objects with the same behavior and status?

The behavior of an object is defined by callable methods. Each object stores information that describes its current characteristics. This is the state of the object. The object's state does not change spontaneously. For a well-designed class, its objects can only be implemented by calling methods. If the state of the object can be changed without calling methods, it means that the encapsulation is damaged.

Encapsulation: combines data and behavior in a package and hides the data implementation method from users of the object. The key to encapsulation is that methods in a class must not directly access instance fields of other classes. Programs interact only with object data through the object's method field.

The data in the object is called instance field (instance field) or attribute. The process of manipulating data is called a method. For each specific object, there is a specific set of instance field values ​​(property values), and the set of these values ​​is the current state of the object.

Relationships between classes

Common relationships between classes:

•Dependency (“use-a”): A method of one class manipulates an object of another class .

•Aggregation (“has-a”): Objects of one class contain objects of another class.

•Inheritance (“is-a”): used to express the relationship between special and general. If class A extends class B (A inherits B), class A not only contains the methods of class B, but also extends methods.

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 comprehensive understanding of Java classes and objects is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more articles related to a comprehensive understanding of Java classes and objects, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template