Day 6: The first day of object-oriented, the first day of object-oriented
Day 6: The first day of object-oriented
1: What is the difference between process-oriented and object-oriented?
1.1: In process-oriented thinking, users must participate in the entire process. However, for objects, users only need to call some functions of others. users do not need to know how these functions are implemented.
Example: requirement: Put the lunch box in the refrigerator.
Process-oriented:
1: I open the refrigerator.
2: Put the lunch box in
3: I close the refrigerator.
Object-oriented:
1: analyze the number of objects in this requirement: refrigerator and lunch box.
2: What attributes or behaviors does the object have. : Refrigerator: open, close.
3: What is the relationship between objects?
1: I call the function enabled by the refrigerator.
2: I put the lunch box in.
3: Call the function of disabling the refrigerator.
My opinion: the process orientation is included in the object orientation, but the process is encapsulated into methods one by one.
2: object-oriented analysis process
2.1: First, analyze the objects in some requirements.
2.2: attributes or behaviors of an object.
2.3: What is the relationship between an object and an object.
3: What is an object? What is a class?
Class: class, which is the abstraction of multiple objects. (For example, the design drawing of a car is a class, and the physical objects are invisible)
Object: The specific thing of the class. Everything in the world is an object. A class is the abstraction of an object, and an object is a specific thing of a class. (A car designed is an object that can be viewed as a physical object)
4:
4.1: Describe a suspect.
Age: around 20, height: 170-175. Gender: male, face: Fang, ACCENT: Sichuan accent. Body: thin. Left-handed. : Features
Walking: inner 8. : Action.
Lock an object: Features and behaviors.
4.2: class definition.
4.3: how to access object attributes and object Methods
Access Object Attributes: Object Name. Attributes
Method of accessing the object: Object Name. Method Name ()
4.4: constructor.
4.1: constructor syntax.
1: The method name is the same as the class name.
2: No return value. The return value type is not required.
4.2: why is there a constructor? What is the role of constructor?
Purpose: Initialize the attributes of an object. It can be called only when an object is created.
4.3: What is the difference between a constructor and a common method in calling a constructor?
The constructor can only be called when an object is created, but cannot be called later. That is to say, it can only be called when it is new.
If no constructor is written to a class, a constructor without parameters is automatically added. If a constructor is written, the constructor will not be added for a long time. As long as it is a class, there must be a constructor.
Summary:
1: object-oriented analysis process.
1.1 analyze the objects of this requirement
1.2 analyze the attributes of each object
1.3 analyze the relationship between objects
2: Create and write a class
Class Name
3: How to create an object
Create with new Constructor
4: how to access the attributes and methods of an object.
Object. Property, object. Method
5: the definition of the constructor and the difference between the constructor and common methods: method name, return value, function, and call.
The method name and type of the constructor are the same, and no return value is returned. It is used as the initialization Object Property and can only be called with the new keyword.
6. overload of constructor Methods: overload of methods.
Different parameter lists
7: differences between member variables and local variables.
Tomorrow:
1: Memory analysis.
2: Transmission of basic data type parameters and referenced data type parameters.
3: this and encapsulation.
1/* requirement: a person's left hand is a peach, And the right hand is a square film K. The content of the two left hands and the right hand is required to be exchanged. Object-oriented thinking. */3 class Person 4 {5 String lHand; 6 String rHand; 7 8 String temp; 9 10 void change () {11 temp = lHand; 12 lHand = rHand; 13 rHand = temp; 14} 15} 16 17 18 class Test0119 {20 public static void main (String [] args) 21 {22 Person p = new Person (); 23 24 p. lHand = "A"; 25 p. rHand = "K"; 26 27 System. out. println ("Left Hand:" + p. lHand + ", right hand:" + p. rHand); 28 p. change (); 29 System. out. println ("Left Hand:" + p. lHand + ", right hand:" + p. rHand); 30} 31}View Code