كود
/*
* Test.java
*
* Created on 27 أبريل, 2008, 10:11 م
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author eisa
*/
public class Test {
String name;
String price;
int num;
public Test(String name,String price,int num){
this.name=name;
this.price=price;
this.num=num;
}
public static void main(String args[]){
Test t=new Test("Khafougi","99",15);
System.out.println(t);
System.out.println(t.toString());
}
public String toString(){
return "Name: " + name + ", " +
"Price :" + price +", " +
"Number of Part: " + num +", ";
}
}
المثد toString فائدتها هي انها تقوم بتمثيل الاوبجكت على شكل String
بالتالي انت اذا اردت ان تطبع الاوبجكت فقط قم ببناء الداله toString واجعلها ترجع كل instance members للاوبجكت تبعك
وفي System.out.print قم بتمرير الاوبجكت وهو بشكل ضمني ينادي دالة toString