السلام عليكم ورحمه الله وبركاته
اتمنى ان يكون الجميع متمع بصحه وعافيه علي مانشاهده من تشويش خارجي خارج عن الاراده ( مثقف ) :wacko:
آخوتي/اخواتي الاحبه انا اليوم وصلت الي درس الواجهه ونعلم ان الواجهه تحتوي علي عده دوال ومن هذه الدوال هي Comparable .
ولكن واجهتني مشكله الا وهي اني لم افهم كيف استخدم هذه الداله ولماذا استخدمها مع اني قرات من الكتاب والانترنت بس مازلت اجهلها .
اليكم هذا الكود البسيط لعل وعسى ان يكون مناسب لواجهه Comparable :
public class SalaryEmo extends EmployeeChapter8
{
private double salary1;
public SalaryEmo(String name, String address, int number, double salary1)
{
super(name, address, number);
setSalary1(salary1);
}
public void mailCheck()
{
System.out.println("Within mailCheck of SalaryEmo class");
System.out.println("Mailing check to " + getName() + " with salary " + salary1);
}
public double getSalary1()
{
return salary1;
}
public void setSalary1(double newSalary)
{
if(newSalary >= 0.0)
{
salary1 = newSalary;
}
}
public double computePay()
{
System.out.println("Computing salary pay for " + getName());
return salary1 / 52;
}
}
public class ComparbleSalaryEmo extends SalaryEmo implements Comparable {
public ComparbleSalaryEmo(String name, String address, int number, double salary1)
{
super(name, address, number, salary1);
}
public int compareTo(Object o)
{
if (getSalary1() > ((ComparbleSalaryEmo)o).getSalary1())
return 1;
else if (getSalary1() < ((ComparbleSalaryEmo)o).getSalary1())
return -1;
else
return 0;
}
public static void main(String[] args)
{
ComparbleSalaryEmo s1 = new ComparbleSalaryEmo("Ali", "Mariners", 5, 23.00);
ComparbleSalaryEmo s2 = new ComparbleSalaryEmo("Kalid", "Kingston", 4, 20.00);
System.out.println(s1.compareTo(s2));
System.out.println(s2.compareTo(s1));
}
}
هذه الكود من كتابتي لاني كنت متابع للدرس في احد الكتب ولكن مازلت اجهل سبب استخدامه، او شرح الكود :blush:
وجزاكم الله اعلى الجنان
اخوكم
