2.4. Javada this kalit so’zi
Java dasturlash tilida this kalit so’zi sinf maydonlariga murojaatni belgilaydi. Masalan this.a bu sinfdagi a metodiga murojat deganidir. Java tilining qulayligini oshirish maqsadida va o’zgaruvchilardan maksimal darajada unumli foydalanish uchun this kalit so’zi kiritilgan. Bu kalit so’zini shu sinfdagi maydon deb tasavvur qilsa bo’ladi. Quyidagi misolda yanada yaxshiroq tushunib olishinmiz mumkin:
class Point {
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public double distance() { // koordimata boshidan masofa
return Math.sqrt(this.x * this.x + this.y * this.y);???
}
}
public class MainClass {
public static void main(String[] arg) {
Point obj = new Point();
obj.setX(5);
obj.setY(3);
System.out.println(obj.getX() + " " + obj.getY());
System.out.println("Koordinata boshidan masofa"+obj.distance());
}
}
this kalit so’zidan foydalanishni sinfdagi barcha metodlarida ko’rib turibsiz. Demak this.x yoki this.y amallari shu sinfning x yoki y maydonlari demakdir. Umuman olganda qaysidir dasturlash tilini o’rganganda ularni amaliyotda o’rgangan foydaliroqligini inobatga olgan holda ko’proq har xil sinflar yaratishga to’xtalamiz. Yuqoridagi o’rgangan amallarimiz yordamida Java tilida kattaroq sinfni yaratib ko’ramiz va undan foydalanishni ko’rib chiqamiz:
class Employee{
private String name;
private double salary;
public void setName(String name){
this.name=name;
}
public void setSalary(double salary){
this.salary=salary;
}
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
public void raiseSalary(double byPercent){
salary*=byPercent/100;
}
public boolean setFirstSalary(double salary){
if (salary<1000){
return false;
}
else {
this.salary=salary;
return true;
}
}
public int equels(Employee other){
if (salary>other.salary){
return 1;
}else{
if (salary==other.salary){
return 0;
}else{
return 1;
}
}
}
}
public class MainClass {
public static void main(String[] args) {
Employee[] staff=new Employee[3];
staff[0]=new Employee();//
staff[0].setName("Abdullayev G.R");
staff[0].setSalary(500);
staff[1]=new Employee();
staff[1].setName("Salayev U.R");
staff[1].setSalary(400);
staff[2]=new Employee();
staff[2].setName("Raximov I.D");
staff[2].setSalary(300);
for (int i=0;istaff[i].raiseSalary(5000);
}
for (int i=0;i
Employee e=staff[i];
System.out.println("Name "+e.getName()+" Salary "+e.getSalary());
}
Employee e=staff[1];
if (staff[1].setFirstSalary(52000)){
System.out.println("Ismi "+e.getName()+" Salary "+e.getSalary());
}else{
System.out.println("error Name "+e.getName()+" old Salary "+e.getSalary());
}
}
}
Yuqoridagi dastur ikkita sinfdan tashkil topgan. Bular Employee va MainClass sinflaridir. Employee sinfiga ishchi xodimning nomi va ish haqi maydonlari kiritilgan. Shu maydonlarga qiymat berish va qiymatini ko’rish imkoniyati mavjud. Bundan tashqari ishchi xodimning ish haqini oshirish va shu kabi amallar bajarilgan. Bu sinfdan foydalanishni MainClass sinfida ko’rishingiz mumkin, unda uchta obekt yaratilgan va ularga har xil parametrlar berilgan.
Do'stlaringiz bilan baham: |