Java object oriented Foundation

Personal homepage: Hello Code.
This column: Java Basics
For more information, please click to Summary of Java basics [table of contents] see
If you have any questions, please correct them and study together~~

Comparison of object-oriented and process oriented ideas

Procedure oriented programmingObject oriented programming
It is a process centered programming idea. Each step of realizing the function is realized by itselfIt is an object-centered programming idea, which realizes specific functions by commanding objects

Object: refers to the objective existence of things (all things are objects)

Classes and objects

  • Class is an abstraction of a class of things with common attributes and behaviors in real life
  • Class is a description of things, that is, objects

A class can be understood as a design diagram. According to the design diagram, specific things can be created

  • Composition of classes
    • Attributes: various characteristics of the thing
    • Behavior: the function of the existence of the thing (what can be done)
  • Object: a real entity that can be seen and touched

A class is a description of an object, and an object is an entity of a class
A class can create multiple objects

Class definition

  • Class composition: properties and behavior
    • Attribute: reflected in the code through member variables (variables outside the methods in the class)
    • Behavior: reflected in the code through the member method (compared with the previous method, just remove the static keyword)
  • To define a class
    • Define class
    • Write the member variable of the class
    • Write member methods of classes
public class Class name{
	// Member variable
	Data type of variable 1;
	String name;		// Unassigned default null
	Data type of variable 2;
	int age;			// Unassigned default 0
	......
	// Member method
	Method 1;
	public void study(){
		System.out.println("study");
	}
	Method 2;
	......
}

Object creation and use

  • create object
    Format: class name object name = new class name ();
  • Use object
    Use member variable: object name. Variable name
    Using member methods: object name. Method name ();
  • case
// Requirements: define a class, and then define a mobile test class. In the mobile test class, the use of member variables and member methods is completed through objects
public class Phone{
	// Member variables: brand, price
	String brand;
	int price;
	// Member methods: calling, texting
	public void call(String name){
		System.out.println("to" + name + "phone");
	}
	public void sendMessage(){
		System.out.println("Mass texting");
	}
}

Object memory graph

  • Single object memory map
  • Two object memory diagram
  • Two references point to the same object memory graph
  • garbage collection

Note: when the address generated by an object or array in heap memory cannot be found in any way, it will be judged as "garbage" in memory
The garbage will be automatically cleaned and collected by the Java garbage collector when it is free

  • Member variables and local variables
    • Member variables: variables outside the methods in the class; Stored in heap memory; With the survival of the object; There are default initialization values
    • Local variables: variables in the method; Stored in stack memory; With the existence of method call, the method call ends; There is no default initialization value, which must be defined and assigned before use

encapsulation

  • private keyword: permission modifier, which can be used to modify members to improve data security
    Features: it can only be accessed in this class. For external access, you can define methods to set and obtain values

If the private modified member variable needs to be referenced by other classes, provide corresponding operations
Provide get variable name (); Method, which is used to obtain the value of the member variable. The method is decorated with public
Provide set variable name (); Method, which is used to set the value of member variable. The method is decorated with public

// New Student class
public class Student{
	private String name;
	private int age;
      
	public void setName(String n){
		name = n;
	}
	public String getName(){
		return name;
	}
	public void setAge(int a){
		age = a;
	}
	public int getAge(){
		return age;
	}
	public void show(){
		System.out.println(name + age);
	}
}
  • This keyword: you can call members (variables and methods) of this class to solve the problem of duplicate names of local variables and member variables

If local variables and member variables have the same name, Java uses the proximity principle
this represents the object reference of the class. this represents the object to which the method is called

public class Student{
	private int age;
	public void method(int age){
		this.age = age;			// Add this keyword to make the previous age become a member variable, and then assign the local variable age to the past
	}
}
  • this memory principle (if you are interested, you can check it online)
  • encapsulation
    • One of the three characteristics of object-oriented (encapsulation, inheritance and polymorphism)
    • Hide the implementation details and only expose the public access mode (similar to the patch panel)
    • Common manifestations:
      • Extracting code into methods is a kind of encapsulation of code
      • Extracting attributes into classes is an encapsulation of data
    • Private member variable, providing setXxx and getXxx methods
    • benefit
      • Improved code security
      • It improves the reusability of the code

Encapsulation in a narrow sense refers to the encapsulation of attributes; The generalized encapsulation also includes extracting reusable code into methods

Construction method

  • The method called when building and creating objects
  • Format:
    1. The method name is the same as the class name, and the case should be consistent
    2. There is no return value type, not even void
    3. There is no specific return value (result data cannot be brought back by return)
public class Student{
	public Student(){
		System.out.println("This is Student Class construction method");
	}
}
  • Execution time:
    1. Called when creating an object. Each time an object is created, the construction method will be executed
    2. Constructor cannot be called manually
  • Function: used to initialize the data (properties) of the object
class Student{
	private int age;
	public Student(int age){
		this.age = age;
	}
}
  • matters needing attention

If no construction method is defined, the system will give a default parameterless construction method
If a construction method is defined, the system will no longer provide the default construction method. If a parameterless construction is called at this time, an error will be reported

  • Coding and use of standard classes
/*
	JavaBean Class: a class that encapsulates data
*/
public class Student{
	// private variable
	private String name;
	private int age;
      
	// Nonparametric construction method
	public Student(){}
	//Parametric construction method
	public Student(String name, int age){
		this.name = name;
		this.age = age;
	}
      
	//set/get method
	public void setName(String name){
		this.name = name;
	}
      
	public String getName(){
		return name;
	}
      
	public void setAge(int age){
		this.age = age;
	}
      
	public String getAge(){
		return age;
	}
}

Tags: Java Back-end

Posted on Tue, 30 Nov 2021 03:53:01 -0500 by bsarika