Explanation of Java Fundamentals Object Oriented 1 of Construction Method static this Keywords

  • 2021-07-16 02:24:28
  • OfStack

The difference between object-oriented and process-oriented. To win 5 chess games:
Process-oriented analysis:

Start the game Black chess goes first Draw a picture Judge winning or losing It's White's turn Draw a picture Judge winning or losing Go back to Step 2 Output result

Object-oriented analysis:

Black and white, the behavior of both parties is 1 model 1 Chessboard system, responsible for drawing pictures Rule system, judging foul, winning or losing

Traditional process-oriented programming is the solution step of thinking about problems, which is suitable for small problems. But when the scale of the problem is large, the program is required to have better scalability and can find errors more quickly, the object-oriented design idea can reflect its advantages. Object-oriented is closer to the natural way of thinking of human beings, and abstracts things in the real world into objects and methods of objects.

Object-oriented development process is actually a process of constantly creating objects, using objects and directing objects to do things.

Relationship between class and object.

Object represents an entity that can be explicitly identified. For example: 1 person, 1 book, 1 school or 1 computer and so on. Each object has its own unique identity, state and behavior.

The state of an object (characteristic or attribute, that is, instance variable) is represented by the data field of the object. For example, a person can have attributes such as name, age, height, weight, home address, etc. These are the "data fields of people".

The behavior of an object (the action performed by the object, that is, the function) is defined by the method. For example, define getName () to get the name, getHeight () to get the height, and setAddress (ES20addr) to modify the address.

Construction method

The constructor is called when the object is created. Each class has a constructor, and if a constructor is not explicitly defined for the class, the compiler automatically creates a default parameterless constructor for the class. The constructor must be exactly the same as the class name and have no return type.
For example:


public class Person{
	public String name;
	public int age;
	public Person(){// Parametric construction method. 
	}
	public Person(String n,int a){// Parametric construction method 
	name=n;
	age=a;
	}
}

public class PersonTest{
	public static void main(String[] args){
	Penson p = new Penson(" Zhang 3",19);		// A parameterized call to the corresponding parameterized constructor. 
	System.out.println(" Name :"+p.name+"  Age :"+p.age);
}
	
}

Run results:

Name: Zhang 3 Age: 19

static keyword

Used to modify class members, variables and methods modified by static are called class member variables and class member methods respectively. Class member variables and class member methods belong to the class itself and can be accessed directly without creating objects.
Access method:

Class name. Member variable/member method

Summary:

Load as the class loads The existence of precedence over the object means that the object can be called before it is created, so the main method of the java program must be decorated with static

Note:

Static methods can only access static members this, super keywords cannot be used in static methods Local variables in the method body cannot be modified with static

this keyword

The this keyword in Java is used to reference the member variables and methods of an object, which can be conveniently operated within a class.


public class Person{
 private String name;
 private int age;
 
 public void SetName(String name){
 this.name=name;	// Pass parameters to the current object's name . 
  }
  
}

this()
this () represents the constructor of the current class, which can only be used in the constructor, and is written in line 1 within the constructor.


Related articles: