Wednesday, 1 March 2017

toString() in Java



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);

    }

}
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 +
                
'}';
    }
}

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);

    }

}
Natija:
Kitob haqida ma'lumotlar: {id=1, name='Философия Java', author='B.Ekkel', price=82.0}


3 comments:

  1. ? agar toString() metodini overrid qilsak biz shu class obektini String tipidagi o'zgaruvchiga tog'ridan - to'g'ri o'zlashtira olamizmi?

    ReplyDelete
    Replies
    1. yo'q!
      Agar #2 misolda String bookString = book deyilsa xatolik yuzaga keladi, buning uchun String bookString = book.toString() kabi ishlatish lozim

      Delete
    2. Javada har bir class Object class ini bolasi sifatida hosil qilinadi, shuning uchun ularda toString() kabi metodlarni overrid qilish mumkin

      Delete

Note: only a member of this blog may post a comment.

Tasks and Threads

  Differences Between Task And Thread: 1. The Thread class is used for creating and manipulating a thread in Windows.  2. A Task represents ...