មេរៀនទី៤៣:ស្វែងយល់ toString() method
ប្រសិនបើចង់បង្ហាញ object ដូចជា string, toString() method។ ដែល
toString() method returns string តំណាងនៃ object។ ប្រសិនបើលោកអ្នក print
object, java compiler toString() method លើ object។ So overriding the
toString() method, returns the desired output, it can be the state of an
object etc. depends on your implementation.
គុណសម្បត្តិនៃ toString() method
ដោយ overriding the toString() method of the Object class, we can return values of the object, so we don’t need to write much code.
ស្វែងយល់ពីបញ្ហានៃ ដោយមិនប្រើ toString() method
Let’s see the simple code that prints reference.
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Student s1=new Student(101,”Raj”,”lucknow”);
Student s2=new Student(102,”Vijay”,”ghaziabad”);
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
Output:Student@1fee6fc
Student@1eed786
ឧទាហរណ៍នៃ toString() method
Now let’s see the real example of toString() method.
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public String toString(){//overriding the toString() method
return rollno+” “+name+” “+city;
}
public static void main(String args[]){
Student s1=new Student(101,”Raj”,”lucknow”);
Student s2=new Student(102,”Vijay”,”ghaziabad”);
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
Download ឧទាហរណ៍នេះ
Output:101 Raj lucknow
102 Vijay ghaziabad
គុណសម្បត្តិនៃ toString() method
ដោយ overriding the toString() method of the Object class, we can return values of the object, so we don’t need to write much code.
ស្វែងយល់ពីបញ្ហានៃ ដោយមិនប្រើ toString() method
Let’s see the simple code that prints reference.
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Student s1=new Student(101,”Raj”,”lucknow”);
Student s2=new Student(102,”Vijay”,”ghaziabad”);
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
Output:Student@1fee6fc
Student@1eed786
ឧទាហរណ៍នៃ toString() method
Now let’s see the real example of toString() method.
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public String toString(){//overriding the toString() method
return rollno+” “+name+” “+city;
}
public static void main(String args[]){
Student s1=new Student(101,”Raj”,”lucknow”);
Student s2=new Student(102,”Vijay”,”ghaziabad”);
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
}
}
Download ឧទាហរណ៍នេះ
Output:101 Raj lucknow
102 Vijay ghaziabad
Post a Comment