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 concept of Java encapsulation

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the concept of Java packaging". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "what is the concept of Java packaging" can help you solve the problem.

The concept of encapsulation

Some information of the class is hidden inside the class, which does not allow external programs to access it directly, but operates and accesses the hidden information through the methods provided by this class.

Why do I need encapsulation?

When we create an object of a class, we can assign a value to the property of the object through the method of "object. Property". Here, the assignment operation is restricted by the data type and storage scope of the attribute. In addition, there are no other constraints, but in practical problems, we often need to add additional constraints to attribute assignments. This condition can not be reflected in the property declaration, we can only add restrictive conditions through the method. At the same time, we need to prevent users from using the "object. Attribute" way to assign the property, then we need to declare the property as private.

Advantages of encapsulation:

1. Good packaging can reduce coupling.

two。 The structure within the class can be modified freely.

3. You can have more precise control over member variables.

4. Hide information and implement details.

Implementation steps of encapsulation

(1) modify the attribute to private

(2) create the getter/setter method, obtain and set the data through these two methods, and the object can read and write the data by calling these two methods.

Class person {private int age;// modified attribute to privatepublic int getAge () {return age;} public void setAge (int a) {age = a;}}

Encapsulating the internal complexity of hidden objects, only exposing simple interfaces to facilitate external calls, the embodiment of encapsulation requires permission modifiers to cooperate.

Packages in Java

The package in Java is to better manage the class in the project and resolve the conflict of files with the same name. When it comes to the package in java, then we have to mention the package keyword. We generally use package to declare the package to which the class or interface belongs, declared in the first line of the source file, such as package java.lang, each. One layer of file directory is represented at a time. Interfaces, classes and different packages with the same name cannot be named under the same package. For the use of the package, you need to use the import keyword. The classes and interfaces under the specified package are imported using the import structure shown in the source file. Usually import is declared between the package declaration and the class declaration. In java, the package name specification is spelled in full lowercase letters.

Member of a class in java-constructor

Any class has a constructor, which is used to create the object and initialize the properties of the object. The method to create the object of the class is: new+ constructor, such as Person p = new Person (); if there is no constructor that explicitly defines the class, the system provides a null parameter constructor by default

Define the format of the constructor

Permission modifier class name (formal parameter list) {}

Summary: the order in which attributes are assigned:

1 default initialization

2 explicit initialization

3 initialization in the constructor

4 through the "object. Method" or "object. Property" method, assignment

This keyword in java

The this keyword can be used to modify: properties, method constructors, and this as the current object or the object currently being created

In the method of a class, we can call the current object property or method using either "this. Property" or "this. Method". But usually, we choose to omit "this." In special cases, if the parameter of the method has the same name as the property of the class, we must display the way to use the "this. Variable", which appears to be a property.

The same is true for the constructor of the class, which calls the constructor of an empty parameter: this () Calls with parameters such as public Person (int age) can be called with this (age), but the constructor cannot call itself but can only call other constructors. If there are n constructors, then only one constructor can be called using this, and the constructor this call must be declared in the first line of the constructor, and this can also be used to compare the size between objects.

Public class Boy {private int age; public int getAge () {return age;} public void setAge (int a) {age = a;} public void compare (Boy boy) {if (this.age > boy.age) {System.out.println ("YES");} else if (this.age < boy.age) {System.out.println ("No") This is the end of the introduction on "what is the concept of Java encapsulation". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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