java object oriented

1, Why use object orientation

Programming idea:
Process oriented: do something by yourself and follow the steps
Object oriented: you don't need to do something yourself. You just need to specify a specific object to complete it

advantage:

1. More fluent development and communication

2. Improve development efficiency

3. Conform to human thinking

2. What is the object?

The object is the actual individual of the thing, which is concrete and realistic. Everything is an object

What does the object contain?

  Attributes: some data composed of nouns are called attributes, such as name, height, etc

  Methods: some functions composed of verbs are called methods   For example, singing, dancing and so on

Attributes in class -- member variables and local variables

Member variable: defined in a class and has an initial value
Local variable: defined in the method and has no initial value

Methods in classes -- member methods

3. What is class?

A class is a description of something. It is abstract. A conceptual definition is the abstract feature of multiple objects.

Things → classes
Attribute → member variable
Behavior → member method

4. Create object

 Person  p = new Person();

5. What is the construction method

1. The construction method and class name are the same

2. The constructor has no return value

3. The constructor is called when creating an object

Classification of construction methods: 3

  1. Implicit parameterless   When creating a class, it takes a constructor without parameters by default
  2. Show no parameters   Constructors can be written in classes
  3. Show parameters   The construction method can be seen and there are parameters

For example:

public Demo() {  
         
}   
public Demo(String name) {
    this.name=name;
}
public Demo(String name,int age){
     this.name=name;
     this.age=age;
}

6. Overloading of construction method

Method overloading is a means for classes to handle different types of data in a unified way. Java method overloading means that multiple methods can be created in a class. They have the same name, but have different parameters and different definitions. When calling methods, it determines which method to use by passing them different numbers and types of parameters, which is polymorphism. example:

class MethodOverloading { 
viod receive(int i) { 
System.out.println("Received one int data"); 
System.out.println("i="+i); 
} 
viod receive(float f) { 
System.out.println("Received one float data"); 
System.out.println("f="+f); 
} 
viod receive(String s) { 
System.out.println("Received a String"); 
System.out.println("s="+s); 
} 
} 

7.this keyword

this: represents the object of the current class

Purpose:
1. It is used to distinguish the conflict between member variables and local variables with the same name
2. You can call the constructor of the current class. Syntax: this([parameter list]); Instead of passing parameters, you call the parameterless constructor of the current class. Instead, you call the parameterless constructor whose parameter list matches. Note: this call statement must be the first statement in the method body.
3. Member attribute can be called: this. Member attribute name
4. Member method can be called: this. Member method name ([parameter list])
 

//Application of this keyword:
//1. Call the properties of the current class (distinguish the assignment of member variables and local variables with the same name)
//2. Call the method of the current class
//3. Call other construction methods of the current class
//
//
//Note: this keyword is not controlled by the access permission when accessing the member method of a class. You can access all member variables and member methods in this class, including private member variables and member methods
public class Person
String name;
int age;
//Show parameterless construction method
public Person(){
this("tom",20);
}
//Define parametric construction
public Person(String name){
this.name = name;
public Person(String name,int age){
this(name);
this.age = age;
}
public void  setName(String name){
this.name = name;
}
public void testMethod(String name){
this.setName(name);//Other methods in the current class are called through this
System.out.println(this.name);
}
public static void main(String[] args) {
Person person = new Person();

System.out.println("___________this Call this class property________________");
person.setName("tom");
System.out.println("name1"+person.name);
System.out.println("___________this Call this class method________________");
person.testMethod("lili");
System.out.println("___________this Call this class constructor_____________");
Person person1 = new Person("tom",30);
System.out.println(person.name);
System.out.println(person.age);
}
}

8. Packaging

Benefits of encapsulation
1. It improves the reusability of the code.
2. It hides the implementation details and provides external access. It is convenient for the caller to use. This is one of the core, but also
To understand is the concept of encapsulation.
3. Improved security.
4. It is also one of the characteristics of object-oriented thought. Object oriented has three characteristics: encapsulation, inheritance and polymorphism

Variables can be privatized. private modifies variables and provides methods to access variables

The name of the method can set the variable and get set|get so that other classes can access the variable

For example: private String name

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

Public -- access modifier    You can visit anywhere

private access modifier   Can only be accessed in this class

9. There can be the following variables in a class


Class variable: it exists in the class and is outside the method. The static keyword must be added before the Declaration: for example, static int flag; Generated with class loading;
Member variables:
Local variables:
Differences between local variables and member variables:
1. The position of the declaration is different
Member variable (instance variable): in the class, the method is external
Local variable: in the method body or on the method declaration
2. Different memory locations
Member variable: in heap memory
Local variable: located in the stack layer
3. Different life cycles
Member variable: generated with the creation of the object and died with the death of the object
Local variable: it is generated with the method call and ends with the method call
4. Different initial states
Member variable: has default initialization value
Local variable: there is no default initialization value. It can only be used after manual initialization.
5. Execution priority
When the member variable and local variable have the same name, the "proximity principle" shall be adopted when calling;
--------

Tags: Java Back-end

Posted on Thu, 28 Oct 2021 05:54:14 -0400 by lookee