Java toString() metodi
Ixtiyoriy ob’yektni string(satr) ko’rinishida ifodalash
uchun doimo toString() metodidan foydalanamiz
toString() metodi ob’yektning satr ko’rinishini qaytaradi.
Ixtiyoriy ob’yektni chop qilganimizda Java kompilyatori ob’ekt ichidagi
toString() metodini ishlatadi. Agar toString() metodi overrid qilinmagan bo’lsa
obektning byte ko’rinishi chop qilinadi. toString() metodini overrid qilish
orqali ob’ekt haqidagi kerakli ma’lumotlarni qaytarishimiz mumkin.
Misollar:
#1. toString() metodini overrid qilmasdan ob’ektni chop
qilish
Book.java:
/**
* Created by Mansurjon on 3/1/2017.
*/
public class Book {
private int id;
private String name;
private String author;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book(int id, String name, String author, float price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
}
BookMain.java
package
tostring;
/**
* Created by Mansurjon on 3/1/2017.
*/
public class BookMain {
public static void main(String[] args) {
Book book = new Book(1,"Философия Java", "B.Ekkel", 82);
System.out.println(book);
}
}
/**
* Created by Mansurjon on 3/1/2017.
*/
public class BookMain {
public static void main(String[] args) {
Book book = new Book(1,"Философия Java", "B.Ekkel", 82);
System.out.println(book);
}
}
Natija:
Book@1c63996
#2.
toString() metodini overrid qilish
Book.java:
package
tostring;
/**
* Created by Mansurjon on 3/1/2017.
*/
public class Book {
private int id;
private String name;
private String author;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book(int id, String name, String author, float price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Kitob haqida ma'lumotlar: {" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
/**
* Created by Mansurjon on 3/1/2017.
*/
public class Book {
private int id;
private String name;
private String author;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book(int id, String name, String author, float price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Kitob haqida ma'lumotlar: {" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
BookMain.java
BookMain.java
package
tostring;
/**
* Created by Mansurjon on 3/1/2017.
*/
public class BookMain {
public static void main(String[] args) {
Book book = new Book(1,"Философия Java", "B.Ekkel", 82);
System.out.println(book);
}
}
/**
* Created by Mansurjon on 3/1/2017.
*/
public class BookMain {
public static void main(String[] args) {
Book book = new Book(1,"Философия Java", "B.Ekkel", 82);
System.out.println(book);
}
}
Natija:
Kitob
haqida ma'lumotlar: {id=1, name='Философия Java', author='B.Ekkel', price=82.0}