Concept of object oriented programming
- Everything is an object.
- Object oriented refers to analyzing things in real life from the perspective of attribute and behavior.
- Object oriented programming refers to the process of analyzing with object-oriented ideas and then expressing with object-oriented programming language.
- Object oriented programming is the demand of the development of software industrialization.
- Understand the essence of object-oriented ideas (encapsulation, inheritance, polymorphism), and master at least one programming language
Concepts of classes and objects
- Objects mainly refer to entities that exist objectively in real life. In Java language, objects are embodied as a storage area in memory space.
- Class is simply "classification", which is an abstract description of the commonness of multiple objects with the same characteristics and behaviors. It is embodied as a reference data type in the Java language, which contains member variables describing characteristics / attributes and member methods describing behaviors.
- Class is a template used to build an object. The data structure of an object is determined by the class that defines it.
Class definition
class Class name { class Person { Class body; } }
- **Note: * * usually, when the class name consists of multiple words, the first letter of each word is required to be capitalized.
Definition of member variables
class Class name { class Person { Data type member variable name = Initial value; String name; } }
- Note: when a member variable consists of multiple words, it is usually required to capitalize the first letter of each word from the second word
Initial value of member variable
- After an object is created, its member variables can be initialized by default. The specific rules are as follows:
Member method
Definition of member method
class Class name { class Person { Return value type member method name(parameter list ) { void show() { Member method; System.out.println("It's okay. Show me") } } } }
- When the member method name consists of multiple words, the first letter of each word is required to be capitalized from the second word.
Detailed explanation of return value type
- The return value mainly refers to the data content returned from the method body to the method body.
- Return value type mainly refers to the data type of return value, which can be basic data type or reference data type.
- When the returned data content is 66, the returned value type can be written as int
- Use the return keyword in the method body to return specific data content and end the current method.
- When the returned data content is 66, write return 66 in the method body; that will do
- When the method does not need to return any data content, write void as the return value type
Object creation
- new class name ();
- be careful:
- a. After a class is defined, you can use the new keyword to create the object of the class. This process is called class instantiation.
- b. The essence of creating an object is to apply for a storage area in the heap of memory space to store the unique characteristic information of the object.
Definition of reference
-
a. Variables defined with reference data types are called reference variables, or "reference" for short.
-
b. The reference variable is mainly used to record the memory address information of the object in the heap area for next access.
Syntax format Person p = new Person(); Class name refers to variable name; p.name = "Fei Zhang"; Reference variable name.Member variable name; System.out.println(p)
Case title
Programming the definition and use of the Person class.
/* Programming the definition of Person class */ public class Person { // Data type member variable name = initial value- Where = initial value is usually omitted String name; // Member variable describing the name int age; // Member variables used to describe age // Custom member method to print all member variables // Return value type method name (formal parameter list) {method body;} void show() { // Both member variables and member methods belong to members within the class, so you can directly access member variables without adding the prefix of reference System.out.println("I am" + name + ",this year" + age + "Years old!"); } public static void main(String[] args) { // 1. Declare that the reference of Person type points to the object of Person type // Data type (class name) reference variable name = new class name (); Person p = new Person(); // 2. Print the member variable value in the object // Reference variable name. Member variable name //System.out.println("I am" + p.name + ", and I am" + p.age + "years old this year!")// null 0 // Reference variable name. Member method name (argument list); // The essence of calling a method is to jump back to this position after executing the method body according to the method name p.show(); System.out.println("-----------------------------------------------------"); // 3. Modify the value of member variable p.name = "zhangfei"; p.age = 30; // 4. Print the modified value again //System.out.println("I am" + p.name + ", and I am" + p.age + "years old this year!")// zhangfei 30 p.show(); } }
Case title
Program to realize the definition of Point class. The features are: abscissa and ordinate (integer). It is required to declare the reference of Point type in the main method to Point to the Point object and print the features, and then modify the abscissa and ordinate to 3 and 5 and print again
/* Programming Point class definition */ public class Point { int x; // Member variable used to describe abscissa int y; // Member variable used to describe the ordinate // Custom member method to print member variable values void show() { System.out.println("Abscissa is:" + x + ",The ordinate is:" + y); } public static void main(String[] args) { // 1. Declare that the reference of Point type points to the object of Point type Point p = new Point(); // Prints the value of the member variable //System.out.println("abscissa:" + p.x + ", ordinate:" + p.y ")// 0 0 p.show(); System.out.println("-----------------------------------------------"); // 2. Print again after modifying the abscissa and ordinate to 3 and 5 p.x = 3; p.y = 5; //System.out.println("abscissa:" + p.x + ", ordinate:" + p.y ")// 3 5 p.show(); } }
Program execution flow and memory analysis