جلسه ۲۳: سازنده های کلاس – ادامه در جاوا

class Date {
private int day;
private int month;
private int year;
// Default constructor
public Date() {
// We must define the default values for day, month, and year
this.day = 0;
this.month = 0;
this.year = 0;
}
// Parameterized constructor
public Date(int day, int month, int year){
// The arguments are used as values
this.day = day;
this.month = month;
this.year = year;
}
// A simple print function
public void printDate(){
System.out.println("Date: " + day + "/" + month + "/" + year);
}
}
class Demo {
public static void main(String args[]) {
// Call the Date constructor to create its object;
Date date = new Date(1, 8, 2018); // Object created with specified values! // Object created with default values!
date.printDate();
}
}
فراخوانی یک سازنده از یک سازنده دیگر
در جاوا می توانیم یک سازنده را از داخل یک سازنده دیگر فراخوانی کنیم. وقتی یک سازنده را از یک سازنده دیگر فراخوانی می کنید، برای ارجاع به سازنده باید از کلمه کلیدی this
استفاده کنید. در کد زیر آن را در عمل می بینید:
class Date {
private int day;
private int month;
private int year;
private String event;
// Default constructor
public Date() {
// We must define the default values for day, month, and year
this.day = 0;
this.month = 0;
this.year = 0;
}
// Parameterized constructor
public Date(int day, int month, int year){
// The arguments are used as values
this.day = day;
this.month = month;
this.year = year;
}
// Parameterized constructor
public Date(int day, int month, int year, String event){
this(day, month, year); // calling the constructor
this.event = event;
}
// A simple print function
public void printDate(){
System.out.println("Date: " + day + "/" + month + "/" + year + " --> " + event);
}
}
class Demo {
public static void main(String args[]) {
// Call the Date constructor to create its object;
Date date = new Date(1, 1, 2019, "New Year"); // Object created with specified values! // Object created with default values!
date.printDate();
}
}
بکارگیری کلمه کلیدی this
قبل از پرانتز به این معنی است که سازنده دیگری در همان کلاس جاوا فراخوانی می کنید. در خط ۲۷ سازنده دوم در کلاس فراخوانی می شود. این قسمت بحث ما در مورد اصول کلاس ها در جاوا را تمام کرد. بخش بعدی به مفهوم پنهان سازی داده ها می پردازد ، که نقشی محوری در پیاده سازی کلاس هایی کارآمد دارد.
در ادامه جلسه قبلی در این جلسه، کمی بیشتر سازنده ها را بررسی می کنیم. موارد زیر را بیان خواهیم کرد
- متغیر مرجع
this
- فراخوانی یک سازنده از یک سازنده دیگر
متغیر مرجع this
متغیر مرجع this
در هر کلاسی وجود دارد. متغیر this
اشاره به خود شی کلاس دارد. وقتی از آرگومانی استفاده می کنیم که نام مشابهی با یک عضو داده کلاس داشته باشد ، از this
برای یافتن عضو داده از درون متد استفاده می کنیم. عبارت this.memberName
مشخص می کند که ما به متغیر memberName
کلاسی دسترسی پیدا می کنیم. در کد زیر آن را در عمل ببینید:
class Date {
private int day;
private int month;
private int year;
// Default constructor
public Date() {
// We must define the default values for day, month, and year
this.day = 0;
this.month = 0;
this.year = 0;
}
// Parameterized constructor
public Date(int day, int month, int year){
// The arguments are used as values
this.day = day;
this.month = month;
this.year = year;
}
// A simple print function
public void printDate(){
System.out.println("Date: " + day + "/" + month + "/" + year);
}
}
class Demo {
public static void main(String args[]) {
// Call the Date constructor to create its object;
Date date = new Date(1, 8, 2018); // Object created with specified values! // Object created with default values!
date.printDate();
}
}
فراخوانی یک سازنده از یک سازنده دیگر
در جاوا می توانیم یک سازنده را از داخل یک سازنده دیگر فراخوانی کنیم. وقتی یک سازنده را از یک سازنده دیگر فراخوانی می کنید، برای ارجاع به سازنده باید از کلمه کلیدی this
استفاده کنید. در کد زیر آن را در عمل می بینید:
class Date {
private int day;
private int month;
private int year;
private String event;
// Default constructor
public Date() {
// We must define the default values for day, month, and year
this.day = 0;
this.month = 0;
this.year = 0;
}
// Parameterized constructor
public Date(int day, int month, int year){
// The arguments are used as values
this.day = day;
this.month = month;
this.year = year;
}
// Parameterized constructor
public Date(int day, int month, int year, String event){
this(day, month, year); // calling the constructor
this.event = event;
}
// A simple print function
public void printDate(){
System.out.println("Date: " + day + "/" + month + "/" + year + " --> " + event);
}
}
class Demo {
public static void main(String args[]) {
// Call the Date constructor to create its object;
Date date = new Date(1, 1, 2019, "New Year"); // Object created with specified values! // Object created with default values!
date.printDate();
}
}
بکارگیری کلمه کلیدی this
قبل از پرانتز به این معنی است که سازنده دیگری در همان کلاس جاوا فراخوانی می کنید. در خط ۲۷ سازنده دوم در کلاس فراخوانی می شود. این قسمت بحث ما در مورد اصول کلاس ها در جاوا را تمام کرد. بخش بعدی به مفهوم پنهان سازی داده ها می پردازد ، که نقشی محوری در پیاده سازی کلاس هایی کارآمد دارد.