Chinangwa cheKuburitsa toString () Maitiro muJava

Chii chiri chinangwa che toString() nzira muJava?

Kana isu tichida kumiririra chinhu chekirasi seTambo, saka tinogona kushandisa toString() nzira iyo inodzosera iwo mamiririri anomiririra chinhu.

Paunodhinda chinhu, nekumisikidza iyo Java compiler inosheedzera iyo toString() nzira pachinhu. Saka nekuwedzera iyo toString() nzira, isu tinogona kupa zvine musoro zvinobuda.

Ngatione iyi pfungwa mune inotevera muenzaniso:

Kuwedzeredza toString () Maitiro

package io.devqa.tutorials; public class ToStringExample {
private String firstName;
private String lastName;
private String email;
public ToStringExample() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('First name : ').append(this.firstName).append(' ');
sb.append('Last name : ').append(this.lastName).append(' ');
sb.append('Email : ').append(this.email).append(' ');
return sb.toString();
}
public static void main(String args[]) {
ToStringExample example = new ToStringExample();
example.setFirstName('Testing');
example.setLastName('Tester');
example.setEmail('testing@tester.com');

System.out.println(example);
} }

Iyi iri pamusoro kodhi inoburitsa zvinotevera:

First name : Testing Last name : Tester Email : testing@tester.com

Kana isu tisina kukunda toString() nzira, kuburitsa kwaizove kuri



io.devqa.tutorials.ToStringExample@60e53b93

Sezvinoonekwa, nekuwedzera iyo toString() nzira, tinogona kuburitsa zvine musoro kuratidza kwechinhu.