Process-Oriented and Object-Oriented Design Ideas
Process-oriented and object-oriented (both linguistic design ideas):
Process oriented programming (POP): Focus on specific processes to solve problems step by step.
object oriented programming (OOP): Design first, classify the objective things in reality, create classes (define functions in classes). Classes are the abstraction of real things, create specific objects, and let objects do specific things.
Case study: Put an elephant in the refrigerator
Process Oriented:Three-step implementation 1.Open the door(); 2.Play an elephant(); 3.close(); Object-oriented: Refrigerators{ Open the door() close() Play an elephant() } Elephants{ }
Object-oriented cannot replace process-oriented, they are complementary. Object-oriented focuses on understanding the relationship between things from a macro perspective, and still uses a process-oriented way of thinking when it comes to how to achieve a certain detail. Object-oriented can't really land and become passive water if it leaves process-oriented.
< java class
Concept: Class is the abstraction of real things. In real life, there are objects (concrete existence) before classes.
Class structure:
Member variables: attributes in classes
Noun: brand, color, price, model.....
Method: The behavior of things (functions, things you can do)
Verb: Drive, Accelerate, Stop......
Construction method: used to create objects;
Code block: A block of code without a name
Internal class: The class declared in the body of the class.
Definition of Java classes
Step 1: Discover Classes
Class declaration format:
[Access Permission Modifier] [Modifier,abstract,final] class Car{ }
public-decorated class, the class name must match the file name.
A non-public-decorated class whose name may not match the file name
Step 2: Define member variables (attributes) of classes: members of classes, define in classes
[Access Permission Modifier] [Modifier] type attr_name[=defaultValue] ;
Variables are grouped by location: member variables, local variables
Member variables:
(1) Defined in a class, outside the body of the method;
(2) Data types can be any type supported by java (reference type, basic type);
(3) Member variables can be initialized or uninitialized, and when uninitialized, java initializes them with default values. The default values for reference type are null integer type 0, floating point type 0.0 boolean type false, and char type''.
(4) When an object is created, a member variable is copied from the class to the object;
Member variables can be used in construction methods, member methods, and code blocks.
Step 3: Define the member method (behavior/function) of the class
[Access Permission Modifier] [Modifier]/ void Method Name(){ }
Local variables:
(1) defined in methods, construction methods, code blocks;
(2) It must be assigned before use;
(3) Data types can be any type supported in java;
(4) The parameters of the method are also local variables. Local variables are created when the method runs and destroyed after the method runs.
Member method: Defined in a class, not modified by static. Called by object, modified by static, can be called directly by class name.
Case: Car.java
//1. Discovery Class public class Car { //2. Define member variables (attributes) of classes String name; float price; String color; int speed; //3. Define member methods of classes (behavior/function) public void run(int b){ int a = 0; System.out.println(a); System.out.println(b); System.out.println(name+"Car Driving"); } public void stop(){ System.out.println(name+"Car stop"); } }
Creation and use of objects
Objects: Objects that exist in a specific way can be used and used. Everything is an object.
Car bm = new Car();
new: create objects in memory using Car class as template;
Car(): The construction method, which has a construction method by default with the same method name as the class name;
Car bm: Declares a variable of type Car;
=assign the created object to the variable on the left;
bm represents a specific object that can be used.
Each object of the same class has a different member variable storage space.
Each object of the same class shares its methods.
Object memory sketch:
Case: TextCar.java
public class TestCar { public static void main(String[] args) { Car bm = new Car(); bm.name = "BMW"; bm.color = "black"; bm.price = 300000; bm.run(100); Car bc = new Car(); bc.name = "Benz"; bc.price = 400000; bc.color = "white"; bc.run(200); Car ad = new Car(); System.out.println(ad.name);//null System.out.println(ad.price);//0.0 System.out.println(ad.color);//null System.out.println(ad.speed);//0 } }
Classes and Objects
Class is a model, an abstraction of reality and a template.
(2) Objects are a specific instance created from the class model, so the process of creating objects is also called instantiating objects.
(3) In real life, there are objects before classes, while in programming, classes are designed before objects are created.