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 use of set/get and toString in Java

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

Share

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

This article mainly introduces "what is the use of set/get and toString in Java". In daily operation, I believe that many people have doubts about the use of set/get and toString in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the use of set/get and toString in Java?" Next, please follow the editor to study!

1. Constructor

The greatest function of the constructor is to initialize the object when it is created. When we new an object and pass in parameters, we automatically call the constructor and complete the initialization of the parameters. As follows:

Public class Test01 {private String name; / / parameterized constructor public Test01 (String name) {this.name = name;} public static void main (String [] args) {Test01 t=new Test01 ("Wade"); System.out.println (t.name);}}

The running result is "Wade"

Insert the constructor shortcut key: alt+insert,Constructor (constructor)

This means that when we create a Test01 object, we automatically call the constructor to complete the initialization, and the name property in the program becomes Wade. The constructor mentioned above is used to initialize the property after the parameter is passed in the new object. However, why my program does not write the constructor can still complete the initialization.

1. If you do not write a constructor, the program automatically generates a constructor with no arguments and no operations (default, hidden).

two。 If a constructor is written, the custom constructor overrides the no-argument constructor.

3. If you only write a constructor with parameters, and there is no constructor without arguments, you cannot use new XXX (); instantiate the object in this way, in the instantiated object code, new XXX ("* *"); the parameters in parentheses must be consistent with the parameters of the constructor, otherwise an error will be reported. (of course, you can also generate objects and assign them through their set and get methods. )

Therefore, it is safer and more commonly used to define both parameter constructors and parameter constructors in the java class, as follows:

Public class Test01 {private String name; / / Parametric constructor public Test01 (String name) {this.name = name;} / / Parametric constructor public Test01 () {} public static void main (String [] args) {/ / you can call the parameterized constructor instantiated object Test01 t=new Test01 ("Wade"); / / you can also call the parameterized constructor instantiated object Test01 t1=new Test01 (); System.out.println (t.name+ "," + t1.name) }}

The running result is "Wade,null"

This will be very convenient in practical use.

2. Set () / get () method

Let's first take a look at the superficial meaning of the words set and get. Set means setting and get means getting. As the name implies, these two methods are used to set and get data.

Then let's take a look at the closeness and security in JAVA object-oriented programming. Closeness is to close the domain variables in the class, that is, to modify them with private, so that other classes cannot access the variable. In this way, we enclose these variables inside the class, which improves the security of the data. What should we do when we want to manipulate these domain variables? We can assign a value to the variable as soon as the object is instantiated in two ways, the first is through a public constructor (or constructor). The second is through the above mentioned set and get methods, here I give a specific example, I define a Person class, which has two private domain variables name, age, and then I define setname (), getname (), setage (), getage () these four methods to implement the operation of name and age. In this way, instead of directly manipulating the domain variables in the Person class, I indirectly manipulate these variables through the set and get methods, which can improve the security of the domain variables and ensure the encapsulation of the domain variables.

Now take the above code as an example to add the set/get method

Use the same shortcut key: alt+insert,Getter and Setter

Public class Test01 {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} / / Parametric constructor public Test01 (String name) {this.name = name;} / / No-parameter constructor public Test01 () {} public static void main (String [] args) {/ / you can call the parameterized constructor instantiated object Test01 t=new Test01 ("Wade") / / you can also call the no-parameter constructor to instantiate the object Test01 t1=new Test01 (); / / use the set () method to assign the attribute value t.setName ("CP3"); / / use the traditional method to assign the value t1.name = "Rose"; / / use the get () method to get the attribute value System.out.println ("t:" + t.getName () + "," + "T1:" + t1.name). / / in the same class, t.getName () and t.name have the same effect, but if it is not the same class and the property is private, you can only use t.getName (), and the set () method is the same. }}

The result of the operation is "tpart CP3 # t1Rose".

This is the encapsulation idea of the java programming language. I hope you can understand it.

3. ToString () method

The Object class has a toString () method that each class you create inherits. It returns a String representation of the object and is very helpful for debugging. However, the default toString () method often does not meet the requirements and needs to be overridden. The toString () method converts the object to a string.

Add the above code at the end

System.out.println (t.toString ()); System.out.println (t1.toString ())

The result of running is

Fully qualified name @ address first address Test01@6d6f6e28Test01@135fbaa4

What is this? Is the address value of the object, but this is not the information we need. We want the various property values of the object, so we need to override the toString () method.

Use the same shortcut key: alt+insert,toString ()

Public class Test01 {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} / / Parametric constructor public Test01 (String name) {this.name = name;} / / No-parameter constructor public Test01 () {} / / override the toString () method. You can customize @ Override public String toString () {return "Test01 {" + "name='" + name +'\'+'}'. } public static void main (String [] args) {/ / you can call the parameterized constructor to instantiate the object Test01 t=new Test01 ("Wade"); / / you can also call the parameterless constructor to instantiate the object Test01 t1=new Test01 (); / / use the set () method to assign the attribute t.setName ("CP3"); / / the traditional assignment t1.name = "Rose" / / use the get () method to get the attribute value System.out.println ("t:" + t.getName () + "," + "T1:" + t1.name); / / in the same class, t.getName () and t.name have the same effect, but if it is not the same class and the attribute is private, you can only use t.getName (), and the set () method is the same. System.out.println (t.toString ()); System.out.println (t1.toString ());}}

The result of running is

T:CP3,t1:RoseTest01 {name='CP3'} Test01 {name='Rose'}

This gives us the information we want, which is the basic use of rewriting toString () in the java class.

At this point, the study on "what is the use of set/get and toString in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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