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 private modifier

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

Share

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

This article mainly explains "what is the private modifier". Friends who are interested may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the private modifier"?

1. Private access modifiers are mainly used to hide implementation details and protect data.

2. Variables declared as private access types can only be accessed through the public getter in the class. Methods, variables, and constructors declared as private can only be accessed by their own class, and classes and interfaces cannot be declared as private.

Example

Public class Logger {private String format; / / Private methods cannot be accessed outside public String getFormat () {/ / define a method that throws the value of the format return this.format;} public void setFormat (String format) {/ / define a method that modifies the property this.format = format in the class through this method In the instance, the format variable in the Logger class is private, so other classes cannot directly get and set the value of the variable. To enable other classes to manipulate this variable, two public methods are defined: getFormat () (returning the value of format) and setFormat (String) (setting the value of format). So far, I believe you have a deeper understanding of "what is the private modifier". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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