object-oriented
object-oriented
Process oriented & object oriented
-
Process oriented thought
- The steps are clear and simple, what to do in the first step, what to do in the second step
- Facing the process, it is suitable to deal with some simple problems
-
Object oriented thought
- Birds of a feather flock together, the thinking mode of classification, thinking about problems will first solve problems
- What categories are needed, and then think about them separately.
- Finally, process oriented thinking is carried out on the details under a certain classification.
- Object oriented is suitable for dealing with complex problems,
- It is suitable for dealing with problems requiring multi person cooperation
-
For describing complex things, in order to grasp
-
On the whole, we need to use object-oriented thinking
-
To analyze the whole system. But from micro to operation
-
It still needs process oriented thinking to deal with it
What is object oriented
- Object oriented programming (OOP)
- Essence: organize code in the form of classes and organize (encapsulate) data in the form of objects.
- abstract
- Three characteristics
- inherit
- encapsulation
- polymorphic
- From the perspective of Epistemology: there are objects before classes. A class is an abstraction of an object.
- From the perspective of code operation, there are classes before objects. A class is a template for an object.
Review methods and deepening
-
Definition of method
-
Modifier
-
Return type
-
breakļ¼ Jump out of switch and end the difference between loop and return.
-
Method name: initial lowercase + hump
-
standard
1, Naming conventions
1. Project name all lowercase
2. Package name all lowercase
3. The first letter of the class name is capitalized. If the class name is composed of multiple words, the first letter of each word should be capitalized.
For example: public class MyFirstClass {}
4. Variable name and method name should be lowercase. If the name consists of multiple words, the first letter of each word should be capitalized.
For example: int index=0;
public void toString(){}
5. Constant names are all uppercase
For example: public static final String GAME_COLOR=”RED”;
6. All naming rules must follow the following rules:
1) The name can only be composed of letters, numbers, underscores and $symbols
2) , cannot start with a number
3) , names cannot use keywords in JAVA.
2, Annotation specification
1. Class annotation
Class comments must be added in front of each class. The comment template is as follows:
/**
- Copyright (C), 2006-2010, ChengDu Lovo info. Co., Ltd.
- FileName: Test.java
- Detailed description of class
- @author class creator name
- @Date creation date
- @version 1.00
*/
2. Attribute annotation
Attribute comments must be added before each attribute. The comment template is as follows:
/**Prompt information*/
private String strMsg = null;
3. Method notes
Each method must be preceded by a method note. The note template is as follows:
/**
- Detailed instructions for using class methods
- @Usage description of param parameter 1
- @return description of the returned result
- @throws exception type. The error code indicates the description of the exception thrown from this type of method
*/
4. Construction method notes
Each construction method must be preceded by a note. The note template is as follows:
/**
- Detailed instructions for the construction method
- @Usage description of param parameter 1
- @throws exception type. The error code indicates the description of the exception thrown from this type of method
*/
5. Method internal comments
Use single line or multi line comments inside the method, which are added according to the actual situation.
For example: / / background color
Color bgColor = Color.RED
-
Parameter list: (parameter type parameter name)
-
Exception throw
-
package com.guoba.Oop; import java.io.IOException; public class Demo01 { public static void main(String[] args) { } /* Modifier return value type method name (...){ //Method body; return Return value; } */ public String sayHello(){ return "HelloWorld!"; } public void print(){ return; } public int max(int a, int b){ return a>b ? a: b;//Ternary operator } public void readFile(String file)throws IOException{ } }
- Method call: recursion
- Static method
- Non static method
- Formal and argument
- Value passing and reference passing
- this keyword
Static method, non static method exercise:
package com.guoba.Oop; public class Demo02 { public static void main(String[] args) { Student.say1();//Call static method Student student = new Student();//Non static method: this class needs to be instantiated when calling. student.say();//This method is called using an instantiated object } }
package com.guoba.Oop; //Student class public class Student { //Non static method: this class needs to be instantiated when calling. Use the instantiated object to call this method public void say(){ System.out.println("The student spoke,Non static methods are used"); } //Static method static public static void say1(){ System.out.println("The students spoke and used the static method"); } }
Relationship between class and object
- Class is an abstract data type. It describes / defines a class as a whole, but it cannot represent a specific thing
- Animals, plants, mobile phones, computers
- Person class, Pet class, Car class... Are used to describe / define the characteristics and behavior of a specific thing
code:
package com.guoba.Oop.Demo07; public class Application { public static void main(String[] args) { //Class instantiation will return an object of its own! //Zhang San is a concrete example of the student class Student zhangsan = new Student(); zhangsan.age = 21; zhangsan.name = "Zhang San"; System.out.println(zhangsan.name + " " + zhangsan.age); } }
package com.guoba.Oop.Demo07; //Student class public class Student { //Properties: Fields int age; String name; //method public void study(){ System.out.println(this.name+"I'm learning"); } }
- Objects are concrete instances of abstract concepts
- Zhang San is an example of man
- It can show a feature and a specific example of the function
Creating and initializing objects
-
When using the new keyword to create
-
In addition to allocating memory, default initialization and calls to class constructors are also performed.
-
Constructors in classes, also known as construction methods, must be called when creating objects.
-
characteristic:
- Must be the same as the class name
- There must be no return type and void cannot be written
-
Constructors must master
Memory analysis:
Summary: