This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Java Learning Notes series — Sun Bujian 1208

【 Detail + super basic 】Java- Learning Notes 01

【 Detail + super basic 】Java- Study Notes 02

【 Detail + super basic 】Java- Study Notes 03

【 Detail + super basic 】Java- Study Notes 04

【 detail + super basic 】Java- Study Notes 05

【 Detail + super basic 】Java- Study Notes 06

【 Detail + super basic 】Java- Study Notes 07

Ongoing updates….

Five, the encapsulation

Encapsulation: To define the properties and behaviors of the same class in a class as properties and methods of the class. The visibility of each property and method is also defined through access control characters.

Purpose of encapsulation: Enhance security and simplify programming by allowing users to access members of a class through an external interface without worrying about implementation details.

Encapsulation in Java:

  • Method is a kind of encapsulation
  • The private keyword is also an encapsulation

Private key

Private is a modifier that can be used to modify members (variables, methods). Members that are modified by private can only be accessed in this class. If a member variable that is modified by private needs to be used by another class, the corresponding operation (get set method) is provided.

Provide the “get variable name ()” method to get the value of a member variable. The method is decorated with public

Provides a “set variable name (parameter)” method that sets the value of a member variable. The method is decorated with public

Example code:

class student { 
// Member variables
	String name;
	private int age; 
// Provide the get/set method
	 public void setAge(int a){ 	
		age = a; 
	} 
	public int getAge(a){
		return age; 
	}
// Member methods
	public void show(a){
		System.out.println(name + "," + age)
		} 
	}
public class studentDemo{ 
	public static void main(String[] args){
// Create an object
		Student s = new Student();
// Assign a value to the member variable
		s.name = "Sun Bujian";
		s.setAge(18); 
// Call the show methods.show(); }}Copy the code

The application of private

Requirements: define standard student class, require name and age to use private decorates, and provide set and get methods and easy to display data show method, test class to create objects and use, the final console output sun Bujin 18

Example code:

class student { 
// Member variables
	private String name; 
	private int age;
/ / get/set methods
	public void setName(String n){ 
		name = n; 
	}
	public String getName(a){
		return name; 
	}
    public void setAge(int a){
    	age = a; 
    }
	public int getAge(a){ 
		return age; 
	}
	public void show(a){ 
		System.out.println(name + ","+ age); }}public class studentDemo {
	public static void main(String[] args){
// Create an object
		Student s = new Student();
// Use the set method to assign values to member variables
		s.setName("Sun Bujian"); 
		s.setAge(18);
		s.show();
// Use the get method to get the value 'of the member variable
			System.out.println(s.getName() + ","+ s.getAge()); }}Copy the code

This keyword

This is used to refer to member variables. Its main purpose is to distinguish between local variables and member variables with the same name.

If a method parameter has the same name as a member variable, a variable that is not modified by this is a parameter, and a variable that is modified by this is a member variable

public class student {
	private String name;
	private int age; 
	public void setName(String name) { 
		this.name = name; 
	} 
	public String getName(a) {
		return name; 
	} 
	public void setAge(int age) {  
		this.age = age; 
	} 
	public int getAge(a)	{ 
		return age; 
	}
	public void show(a) { 
		System.out.println(name + ","+ age); }}Copy the code

Packaging ideas

  1. Encapsulation is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism) object-oriented programming language is the simulation of the objective world, the objective world member variables are hidden in the object, the outside is unable to directly operate.
  2. Encapsulation principle: some information of the class is hidden inside the class, and external programs are not allowed to directly access it. Instead, operations on the hidden information and access member variables are implemented through methods provided by the class, providing corresponding getXxx() and setXxx() methods.
  3. Benefits of encapsulation: The operation of member variables is controlled by methods, which improves the security of code. The code is encapsulated by methods, which improves the reusability of code.

A constructor

1.1 Overview of construction methods

Constructors are a special kind of method

Student stu = new Student();

Format:

public classThe name of the class{modifier class name (parameter) {}}Copy the code

Function: mainly to complete the initialization of object data

class student {
	private String name; 
	private int age;
// The constructor
	public Student(a) { 
		System.out.println("No-parameter construction method"); 
	} 
	public void show(a) {
		System.out.println(name + ","+ age); }}public class studentDemo { 
	public static void main(String[] args) { 
// Create an object
		Student s = newStudent(); s.show(); }}Copy the code

1.2 Precautions for construction methods

Creation of the constructor

If no constructor is defined, the system will provide a default no-argument constructor. If a constructor is defined, the system will no longer provide a default constructor.

Overloading of constructors

If you have a custom constructor with a parameter and a no-parameter constructor, you must write another no-parameter constructor.

Important: Member variables can be initialized using a constructor with a parameter

class student {
	private String name;
	private int age;
	public Student(a) {}
	public Student(String name) {
		this.name = name;
	}
	public Student(int age) {
		this.age = age;
	}
	public Student(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void show(a) { 
		System.out.println(name + ","+ age); }}public class studentDemo {
	public static void main(String[] args) { 
// Create an object
		Student s1 = new Student(); 
		s1.show();
//public Student(String name) 
		Student s2 = new Student("Sun Bujian"); 
		s2.show();
//public Student(int age) 
		Student s3 = new Student(18); 
		s3.show(); 
//public Student(String name,int age) 
		Student s4 = new Student("Sun Bujian".18); s4.show(); }}Copy the code

1.3 Standard writing

Requirements: Define standard student classes that require the creation of objects using null and parameter constructors, respectively. Objects created with null parameters are assigned by setXxx, objects created with parameters are assigned directly, and data is displayed by show method.

class student { 
// Member variables
	private String name;
	private int age; 
// The constructor
	public Student(a) {}public Student(String name, int age) { 
		this.name = name; this.age = age; 
	} 
// Member methods
	public void setName(String name) { 
		this.name = name; 
	}
	public String getName(a) {  
		return name; 
	} 
	public void setAge(int age) {
		this.age = age; 
	}
	public int getAge(a) {
		return age; 
	}  
	public void show(a) {
		System.out.println(name + ","+ age); }}/* There are two ways to create an object and assign values to its member variables. 1: setXxx() is used to assign the object after the object is created with no parameters. 2: The object with attribute values is directly created with the parameter constructor


public class studentDemo {
	public static void main(String[] args) { 
// setXxx() is used to assign a value to an object created by the no-parameter constructor
		Student s1 = new Student(); 
		s1.setName("Sun Bujian");
		s1.setAge(18); 
		s1.show();
// Use the parameter constructor to directly create an object with an attribute value
		Student s2 =new Student ("Sun Bujian".18); s2.show(); }}Copy the code

Welcome to subscribe column to invite you to drink a cup of Java, hope to bring convenience to friends in need, but also hope to get everyone’s attention and support.