Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Java encapsulation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge of what the Java encapsulation method is. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. What is packaging?

Concept: direct access to data in an object is prohibited and should be accessed through an operation interface. Proper encapsulation makes the code easier to understand and maintain, and enhances the security of the code.

(1) method is a kind of encapsulation.

(2) the keyword private is also a kind of encapsulation.

2. The use format of private private data type variable name

(1) use private to modify variables.

(2) provide three methods of getXXX/setXXX/isXXX to access member variables.

Public class Person {private String name; / / name private int age; / / Age private boolean male; / / determine whether the gender is male public void setMale (boolean b) {male = b;} public boolean isMale () {return male;} public void show () {System.out.println ("my name is:" + name + ", age:" + age) } / / this member method is specifically used to set data public void setAge (int num) {if (num) to age

< 100 && num >

= 9) {/ / if it is reasonable age = num;} else {System.out.println ("data is unreasonable!") ;}} / / this member method specifically whispers to obtain age data public int getAge () {return age;}} 3, encapsulation optimization-this keyword this. Member variable name

Use the this keyword to modify the variables in the method to solve the problem that member variables are hidden.

Public class Student {private String name; private int age; public void setName (String name) {this.name = name;} public String getName () {return this.name;} public void setAge (int age) {this.age = age } public int getAge () {return this.age;}} 4. Encapsulation optimization-construction method

When an object is created, the constructor is used to initialize the object and assign initial values to the members of the object.

Define format

Modifier constructor name (parameter list) {/ / method body} public class Student {/ / member variable private String name; private int age; / / No parameter constructor public Student () {System.out.println ("No parameter constructor is executed!") ;} / / full-parameter construction method public Student (String name, int age) {System.out.println ("all-parameter construction method is executed!") ; this.name = name; this.age = age;} / / Getter Setter public void setName (String name) {this.name = name;} public String getName () {return name;} public void setAge (int age) {this.age = age;} public int getAge () {return age;}}

Note:

(1) all classes have a constructor class because Java automatically provides a no-parameter constructor. If you customize the constructor, the default no-parameter constructor provided by Java becomes invalid.

(2) the constructor name must be the same as the class name, there is no return value, and no void modifier is required.

(3) the construction method can be overloaded. Overload = under the same class, different parameters with the same name.

5. Standard code-JavaBean

JavaBean is a standard specification for classes written in Java. Classes that conform to JavaBean require classes to be concrete, public, and have no-parameter constructors, and provide get and set methods for manipulating member variables.

Public class Student {private String name; / / name private int age; / / Age public Student () {} public Student (String name, int age) {this.name = name; this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name } public int getAge () {return age;} public void setAge (int age) {this.age = age;}} above is all the content of this article "what is the method of Java encapsulation?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report