1. The concept of OOP
Concept: Object Oriented Programming (OOP) is a kind of computer Programming architecture, considering the specific Object as a unit, considering its attributes and methods, it can make people’s Programming and the actual world more close, all objects are given attributes and methods, This becomes a more humanized, real-world concept, an idea for designing and implementing software systems.
- Process-oriented thinking:
- Process oriented design thinking in considering the problem, is a specific process as a unit, considering its implementation method, concerned is the realization of the function, such as we complete a demand step, what to do, how to do, the first step, the second step… Get the result. Every step, every process, we participate in and get to the end result we want.
- C language is a typical process-oriented language
- Object-oriented thinking:
- Object orientation and process orientation are mutually reinforcing, not mutually exclusive. For example, a requirement is to put an elephant in the refrigerator: step 1 open the refrigerator door, step 2 put the elephant in, step 3 close the refrigerator door… For such simple, non-collaborative problems, a process-oriented approach is clearly best.
- But if the requirement is to make a refrigerator such a complex transaction, process oriented is very difficult, need object-oriented thinking to design the structure of the refrigerator, color, size, noise… And so on.
- Of course, while object-oriented design is used at the macro level, process-oriented design is still used at the micro level.
- Four characteristics of OOP:
- OOP is characterized by abstraction, encapsulation, inheritance, and polymorphism.
- OOP is characterized by encapsulation, inheritance, and polymorphism. (Because abstraction doesn’t involve code)
1.1 object
Concept: The word “Object” was proposed and defined by Husserl, a phenomenological master in the 19th century. He believed that an Object is the image of the Object in the world in the human brain, the reflection of human consciousness as consciousness, and the preconceived thing that exists as a concept. For example, when we recognize a new Object called tree, Thus, the concept of tree is formed in our consciousness. This concept will always exist in our mind and will not disappear when the tree is cut down. This concept is the image of the object in the real world in our consciousness, which is an object.
- Everything is an object: an apple, a policy, a mood, etc.
- Objects are unique, just as two identical leaves cannot appear in the world.
- Objects can have properties and behaviors (methods).
1.2 the abstract
Concept: the so-called abstraction is to extract similar parts, also known as extraction, extraction or induction summary, is to ignore those aspects in a topic irrelevant to the current goal, in order to fully pay attention to the aspects related to the current goal, and then these parts of our concern, summed up together to form a concept class.
Wolf dog guide diagram
Conclusion:
- First, we automatically ignore the animals’ height, weight, blood type, date of birth, etc., because we don’t care.
- Then, fierce eyes, downward tail, social animal, responsible, bonding, hunting, whining… These attributes and methods are extracted and summarized into a Wolf class, which will become a “standard” or “template” in your heart. If you create a real Wolf, you will give these attributes and methods to the “Wolf” you create according to the “template”.
- Finally, dotty eyes, upward-looking tails, solitary animals, irresponsible, loyal, cute, barking… These attributes and methods are extracted and summarized into dogs, which will become a “standard” or “template” in your mind. If you create a real dog, you will follow this “template” and give these attributes and methods to the “dog” you create.
- Wolf templates and dog templates are abstracted, and a real Wolf and a real dog are new templates that you create.
1.3 packaging
Concept:
- Encapsulation is a process that encapsulates properties and methods together in a program unit, hides the implementation process of the method, and implements the results of the Encapsulation process in the form of classes.
- A class is an object that abstracts the attributes and methods of interest and encapsulates these attributes and methods into a unit.
- The relation between classes and instances: in JAVA, a class is a “template”, and an instance is a “Wolf” or a “dog”. We get a template by abstracting and summarizing each instance, and we get the corresponding instance by instantiating the template.
1.3.1 install
Concept:
- For the above example, we have abstracted a “Wolf” and a “dog”, and a bunch of their corresponding properties and methods, but these things can’t just exist in our imagination and memory, we need to record them, in the case of Java, in a class.
- The process of abstracting the properties and methods you want to focus on and writing them in a class is called “loading”.
Pseudocode: Wolf class. Java
Wolf {attribute: Eyes = Fierce; Tail = downward; Habits = social; Character = [responsibility, solidarity] Method: hunting method (); Cry method (); }Copy the code
Pseudocode: canines. Java
Dog {attributes: Eyes = Douche; Tail = up; Habits = solitary; Personality = [irresponsible, loyal] method: Cute method (); Barking method (); }Copy the code
1.3.2 seal
Concept:
- “Seal” is an object in order to protect privacy, protecting the internal implementation details, object isolation of change, because the object inside is very easy to change, like the inside of the box or hardware configuration, updated in real time, but after wrapped in a case, you don’t need to keep up, no matter how it changes, to your function is not become, boot restart, usb interface, and so on.
- Encapsulation will produce isolation, in the face of different caller, is that you can have different permissions, this is referred to as encapsulation permissions, such as in real life to turn registered permanent residence, need to give his a lot of or all of the information, before parents, need a little less details, will be revealed in front of your friends less, about some of their own little secret, only oneself know…
- Java properties and methods also have corresponding permissions, identified by an obvious tag called permission modifiers.
public
: Accessible in the same project.protected
: same package, same package, different package parent and child classes are accessible.friendlly
: Same type, same package can be accessed, this keyword cannot be displayed.private
: Only the same type can be accessed.
2. Class design
Concept: We have been using classes provided by the JDK, such as String, Scanner, etc. Next we will create a class of our own.
- Class to
class
For keywords, there can be more than one parallel in a Java fileclass
, butpublic
Modification of theclass
There can only be one. - The class name is actually an abbreviation of the class. It is recommended to start with a capital hump. The full name of the class is a combination of the package name and the class name.
- Classes can be divided into five categories according to their modifications and positions:
public class A{}
: Public class: All classes are accessible.class A{}
: friend class: accessible to the same package.final class A{}
: Final class: cannot be inherited, and all properties ina final class are final.static class A{}
Static class: can only be written inside other classes.abstract class A{}
Abstract class: A class that can contain abstract methods.
Class’s single responsibility: In the system, each class has certain responsibilities and duties refers to a kind of what kind of function to complete, want to assume what kind of obligation, a class can have a variety of functions, but only a kind of responsibility, excellent class general at the time of design class, will be the responsibility of the decomposed into class attributes and methods, and attributes of a class is responsible for storing data, method of a class is responsible for the operation data.
3. Use of classes
Concept: When a software system is running, an instance is created from a class. This process is called instantiation, and the created object is called an instance of the class.
- Declarative assignment: Instantiation is the process of using the new keyword to create a space in the heap memory, and then calling the constructor of the class to construct the class and put it into space A.
- We must design a constructor when we design the template (by default, we use the implicit constructor that comes with the template).
- During instantiation, the constructor must be called.
- Member invocation: Calls in Java use the component character “.” to invoke members.
- Non-static member attributes belong to an instance, are independent of each other, and are invoked using the name of the instance they belong to.
- Static member attributes belong to the class (also known as class attributes), are shared and unique, and are called directly using the class name.
Source: / javase – oop /
- src:
c.y.start.MyClassTest
/ * * *@author yap
*/
public class MyClassTest {
public int id = 1;
String name = "Zhang";
public static int age = 23;
private String gender = "Female";
@Test
public void instance(a) {
MyClassTest zhaosi = new MyClassTest();
System.out.println(zhaosi);
}
@Test
public void changeForNoStaticField(a) {
MyClassTest zhaosi = new MyClassTest();
MyClassTest liuneng = new MyClassTest();
zhaosi.id = 50;
System.out.println(liuneng.id);
}
@Test
public void changeForStaticField(a) {
MyClassTest zhaosi = new MyClassTest();
MyClassTest liuneng = new MyClassTest();
MyClassTest.age = 50; System.out.println(MyClassTest.age); }}Copy the code
4. Class member attributes
Concept: A class can hold attribute variables, called attributes for short.
- Location:
- If a variable is defined in the method body, it is called a local variable.
- If a variable is defined outside the method body, it is called a member property of that class.
- Classification: Attributes are classified into six categories based on modifiers:
public String name
: Public property: visible across any class in a package.protected String name
: Protected property: visible across package parent classes.String name
: Default property: same package visible.private String name
: Private property: Visible to this class.static String name
Static property: Shared and unique.final String name
: Constant property: the property cannot be assigned twice. All caps are recommended.
- Default: Unlike local variables, attribute variables have default values.
byte
.short
.int
.long
The default value is 0.float
.double
The default value is 0.0.char
The default value is space,\u0000
.boolean
The default value is false.- The default values for reference data types are all NULL.
5. Class member methods
Concept: A member method, like a member attribute, is a member of a class.
- Classification: Methods are divided into 8 categories according to modifiers:
public void fun(){}
Public methods: visible across any class in a package.protected void fun(){}
: Protection method: visible across package parent classes.void fun(){}
: Default method: see the same package.private void fun(){}
: Private method: visible to this class.static void fun(){}
Static method: shared and unique.public fun(){}
Constructor: commonly known as a constructor, a method that must be called when creating a class.abstract void fun();
Abstract method: A method that has no method body.final void fun(){}
Final method: cannot be overridden.
- Four elements of the method:
- Method name: The name of the method, the naming convention, the variable name, the lowercase hump, and the verb if possible.
- Return type: what type of data the method returns to the caller: void if no data is returned.
- Parameter list: What type of data the method receives, separated by commas. If no data is passed in, the method is called no-parameter.
- Method body: The logical code for a method, noting that it is best for a method to do only one thing.
5.1 Design ideas of the method
Flow: When we want to design a method, it is recommended to follow the following order:
- Purpose: I want to design a method that can add two numbers together to get a result.
- Access: this method only allows access to other methods of the class:
private
- Static or not: I want this method to belong to each instance, not the class:
- Don’t write
static
- Don’t write
- Return value: I want this method to eventually return me one
int
Type results:private int
- Method name: the method name is called
addNum
:private int addNum()
- Parameter list: WHEN I want to call this method, I definitely want to receive two variables of type int:
private int addNum(int numA, int numB)
- Method body: Writing the logical code for a method that does only one thing:
private int addNum(int numA, int numB){... }
Source: / javase – oop /
- src:
c.y.start.MethodDesignTest
/ * * *@author yap
*/
public class MethodDesignTest {
private int addNum(int numA, int numB) {
return numA + numB;
}
@Test
public void addNum(a) {
int result = addNum(1.5); System.out.println(result); }}Copy the code
5.2 Transfer of method parameters
Concept: Method parameters have a property when passed:
- When the parameter passed is a primitive data type, it means to pass a copy.
- Pass a reference when the parameter passed is a reference data type.
Source: / javase – oop /
- src:
c.y.start.MethodParamsTest
/ * * *@author yap
*/
public class MethodParamsTest {
private void methodA(int num) {
num = 1000;
}
private void methodB(int[] arr) {
arr[0] = 1000;
}
@Test
public void passCopy(a) {
int num = 10;
methodA(num);
System.out.println(num);
}
@Test
public void passReference(a) {
int[] arr = {1.2}; methodB(arr); System.out.println(Arrays.toString(arr)); }}Copy the code
5.3 Recursion of methods
Concept:
- Recursion is all about calling yourself.
- Recursion is simple but time-consuming, and any problem you can solve with recursion can be solved with loops, so don’t use recursion until you have to.
- Recursion must have an exit! Otherwise it’s a dead recursion that causes a stack overflow: a StackOverflowError.
Source: / javase – oop /
- src:
c.y.start.DeadRecursionTest
/ * * *@author yap
*/
public class DeadRecursionTest {
private void methodA(a) {
System.out.println("methodA...");
methodA();
}
@Test
public void deadRecursion(a) { methodA(); }}Copy the code