Home >Java >Javagetting Started >java object-oriented - encapsulation

java object-oriented - encapsulation

王林
王林forward
2019-11-28 14:12:502276browse

java object-oriented - encapsulation

Overview

Object-oriented programming language is a simulation of the objective world. In the objective world, member variables are hidden inside the object. It cannot be directly manipulated or modified. Encapsulation can be thought of as a protective barrier that prevents the code and data of the class from being freely accessed by other classes. To access the data of this class, you must use the specified method.

Appropriate encapsulation can make the code easier to understand and maintain, and also enhance the security of the code.

Principle

Hide the properties. If you need to access a property, provide public methods to access it.

Encapsulation steps

Use the private keyword to modify member variables.

For member variables that need to be accessed, a corresponding pair of getXxx methods and setXxx methods are provided.

Java learning video recommendation: java online tutorial

Encapsulated operation—private keyword

Meaning of private:

Private is a permission modifier, which represents the minimum permission. It can modify member variables and member methods. Member variables and member methods modified by private can only be accessed in this class.

Usage format of private

private 数据类型 变量名 ;

Use private to modify member variables, the code is as follows:

java object-oriented - encapsulation

Provide getXxx method / setXxx method, you can access member variables, the code is as follows:

java object-oriented - encapsulation

Encapsulation optimization 1——this keyword

We found that the name of the formal parameter in the setXxx method does not comply with the provisions of knowing the meaning by seeing the name. So if the modification is consistent with the name of the member variable, will it be known by the name? The code is as follows:

java object-oriented - encapsulation

#After modification and testing, we found a new problem, the member variable assignment failed. In other words, after modifying the formal parameter variable name of setXxx(), the method does not assign a value to the member variable! This is because the formal parameter variable name has the same name as the member variable name, causing the member variable name to be hidden. The variable name in the method cannot access the member variable, so the assignment fails. Therefore, we can only use the this keyword to solve this duplicate name problem.

The meaning of this

this represents the reference (address value) of the current object of the class, that is, the reference of the object itself.

Remember: which object the method is called on, this in the method represents that object. That is, whoever is calling, this represents.

this usage format:

this.成员变量名;
public class Student {  
	private String name;  
	private int age;
	public void setName(String name) {
		//name = name;  
		this.name = name;
	}
	public String getName() {  
		return name;
	}
	public void setAge(int age) {
		//age = age; 
		this.age = age;
	}
	public int getAge() {  
		return age;
	}
}

Tip: When there is only one variable name in the method, this is also modified by default, which can be omitted.

Encapsulation Optimization 2 - Construction Method

When an object is created, the construction method is used to initialize the object and assign initial values ​​to the member variables of the object. value.

Tip: Regardless of whether you customize the constructor or not, all classes have constructors, because Java automatically provides a parameterless constructor. Once you define a constructor, Java automatically provides The default no-parameter constructor will fail.

The definition format of the constructor method:

java object-oriented - encapsulation

In the way of writing the constructor method, the method name and its location The class names are the same. It has no return value, so no return type is needed, not even void. After using the constructor, the code is as follows:

java object-oriented - encapsulation

Note:

If you do not provide a constructor, the system will give Parameter construction method.

If you provide a constructor, the system will no longer provide a parameterless constructor.

Constructor methods can be overloaded, and parameters can be defined or not.

标准代码——JavaBean

JavaBean 是 Java语言编写类的一种标准规范。符合 JavaBean 的类,要求类必须是具体的和公共的,并且具有无 参数的构造方法,提供用来操作成员变量的 set 和 get 方法。

java object-oriented - encapsulation

编写符合 JavaBean 规范的类,以学生类为例,标准代码如下:

java object-oriented - encapsulation

测试类,代码如下:

public class TestStudent {
	public static void main(String[] args) {
		//无参构造使用
		Student s= new Student();  
		s.setName(" 柳 岩 "); 
		s.setAge(18);
		System.out.println(s.getName()+"‐‐‐"+s.getAge());
		//带参构造使用
		Student s2= new Student(" 赵 丽 颖 ",18);  
		System.out.println(s2.getName()+"‐‐‐"+s2.getAge());
	}
}

推荐相关文章教程:java语言入门

The above is the detailed content of java object-oriented - encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete