ஜாவாவில் this keyword தற்போதைய ஆப்ஜெக்ட்டை(current object)
குறிக்கும்reference variable ஆகும்.
public class Point {
    public int x = 0;
    public int y = 0;
    //constructor
    public Point(int a, int
b) {
        x = a;
        y = b;
    }
}
மேலே உள்ள
நிரலில் this
keyword உபயோகிக்கப்
படவில்லை.இதே நிரலை கீழே உள்ளவாறு எழுதினோம் என்றால் this keyword தேவைப்படும்.
public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
point class-ன் instance variable பெயரும் x,y. மற்றும் point constructor –ன் தலைப்பில் x,y என இரு local variable ம் உள்ளன.எனவே name collision தவிர்க்கவே this.x ,this.y என்று குறிப்பிடப்பட்டுள்ளது.this.x ,this.y ஆகியவை முறையே point class-ன் instance
variable x,y ஆகும்.
constructor உடன் this keyword பயன்பாடு.
cclass Student
{  
    int id;  
    String name;  
    Student()
{
System.out.println("default constructor is invoked");
}  
    Student (int id,String name)
{  
    this ();//it is used to invoked current class constructor.  
    this.id = id;  
    this.name = name;  
  }  
    void display()
{
System.out.println(id+" "+name);}  
    public static void main(String args[]){  
    Student e1 = new Student(111,"karan");  
    Student e2 = new Student(222,"Aryan");  
    e1.display();  
    e2.display();  
   }  
}
 Output:
       default constructor is invoked
       default constructor is invoked
       111 Karan 
               222 Aryan 
ஒரு
கிளாஸின் உள்ளே நிறைய constructors இருந்து ஒன்றிலிருந்து மற்றொரு constructor
 அழைக்கப்பட this keyword பயன்படுத்தப்படுகின்றது.மேலே உள்ள நிரலில் parameterized
constructor-ல் இருந்து
non parameterized constructor ஆனது this()என அழைக்கப்பட்டுள்ளது.
 நான் மதுரையில் C,C++,JAVA CLASSES நடத்தி வருகின்றேன்.
மேலும் DOTNET, PHP, TALLY, MS-OFFICE வகுப்புகளும் நடத்தி வருகின்றேன்.
தொடர்புக்கு:
91 96293 29142
No comments:
Post a Comment