Java: Boshqa sinfdagi qiymatlarni qidirish

Ok, shuning uchun atributlar bilan xususiylashtirilgan sana va xususiy vaqt bilan ataladigan sinfga ega bo'lish kerak (Sana, Vaqt) .. Shu bilan birga, mening ismim sinfi kun, oy, yil va Vaqt soat, daqiqalar va o'z qadriyatlarini belgilab qo'ydim .. Uchrashuv sinfida maxsus sana tarixini qanday ishlataman va ikkala sinfning atributlari qadriyatlarini ham qabul qilaman. Savol biroz chalkash yoki Men buni tushunishga harakat qilardim, lekin buni hatto nima deb nomlayotganini bilmayman ... Yangi sinfda yana bir sinf e'lon qiladimi yoki nima? Iltimos yordam bering. Rahmat

1
Java API-da allaqachon mavjud bo'lsa, nega o'z sana va vaqt kurslarini yaratasiz?
qo'shib qo'ydi muallif m0skit0, manba
Java API-da allaqachon mavjud bo'lsa, nega o'z sana va vaqt kurslarini yaratasiz?
qo'shib qo'ydi muallif m0skit0, manba
iltimos, ba'zi kodni yozing. "java da mos yozuvlar bilan o'tish" ustida o'qish kerak kabi ko'rinadi
qo'shib qo'ydi muallif vikingsteve, manba

8 javoblar

To'g'ri tushunsam, sizning sinflaringiz shu kabi narsalarga qarashadi:

  class Appointment {
    private Date date;
    private Time time;
    }

class Date {
private String day;
private String month;
private int year;
}

class Time {
private int hour;
private int minutes;
}

Va siz o'zingizning qadriyatlaringiz bilan har bir vaqt va sana bo'yicha misolga egasiz, endi siz o'sha sana va vaqtning qadriyatlarini tayinlash sinfida belgilashni xohlaysizmi?

agar bo'lsa, siz Setters yoki konstruktorlardan foydalanishingiz mumkin.

Agar siz setterdan foydalansangiz, Sizning Uchrashuvingiz klassi quyidagi kabi ko'rinadi:

 class Appointment {
    private Date date;
    private Time time;

    public void setDate(Date date){
        this.date = date;
    }
    public void setTime(Time time) {
        this.time = time;
    }
}

Keyin yuqoridagi kabi foydalanishingiz mumkin:

public static void main(String[] args){
  Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(); //creating an empty Appointment object
  yourAppointment.setDate(yourDate); //setting your created date in appointment
  yourAppointment.setTime(yourTime); //setting your created time in appointment
}

Yoki siz suhbatdosh sinfida o'rnashgan o'rniga konstruktordan foydalanishingiz mumkin. Endi Uchrashuv sinfi quyidagi kabi ko'rinadi:

 class Appointment {
        private Date date;
        private Time time;

       private Appointment(Date date, Time time){
           this.date = date;
           this.time = time;
       }
    }

siz shunday sana va vaqt qiymatlarini belgilashingiz mumkin:

    public static void main(String[] args){
      Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(yourDate,yourTime); 
}
0
qo'shib qo'ydi

To'g'ri tushunsam, sizning sinflaringiz shu kabi narsalarga qarashadi:

  class Appointment {
    private Date date;
    private Time time;
    }

class Date {
private String day;
private String month;
private int year;
}

class Time {
private int hour;
private int minutes;
}

Va siz o'zingizning qadriyatlaringiz bilan har bir vaqt va sana bo'yicha misolga egasiz, endi siz o'sha sana va vaqtning qadriyatlarini tayinlash sinfida belgilashni xohlaysizmi?

agar bo'lsa, siz Setters yoki konstruktorlardan foydalanishingiz mumkin.

Agar siz setterdan foydalansangiz, Sizning Uchrashuvingiz klassi quyidagi kabi ko'rinadi:

 class Appointment {
    private Date date;
    private Time time;

    public void setDate(Date date){
        this.date = date;
    }
    public void setTime(Time time) {
        this.time = time;
    }
}

Keyin yuqoridagi kabi foydalanishingiz mumkin:

public static void main(String[] args){
  Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(); //creating an empty Appointment object
  yourAppointment.setDate(yourDate); //setting your created date in appointment
  yourAppointment.setTime(yourTime); //setting your created time in appointment
}

Yoki siz suhbatdosh sinfida o'rnashgan o'rniga konstruktordan foydalanishingiz mumkin. Endi Uchrashuv sinfi quyidagi kabi ko'rinadi:

 class Appointment {
        private Date date;
        private Time time;

       private Appointment(Date date, Time time){
           this.date = date;
           this.time = time;
       }
    }

siz shunday sana va vaqt qiymatlarini belgilashingiz mumkin:

    public static void main(String[] args){
      Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(yourDate,yourTime); 
}
0
qo'shib qo'ydi

To'g'ri tushunsam, sizning sinflaringiz shu kabi narsalarga qarashadi:

  class Appointment {
    private Date date;
    private Time time;
    }

class Date {
private String day;
private String month;
private int year;
}

class Time {
private int hour;
private int minutes;
}

Va siz o'zingizning qadriyatlaringiz bilan har bir vaqt va sana bo'yicha misolga egasiz, endi siz o'sha sana va vaqtning qadriyatlarini tayinlash sinfida belgilashni xohlaysizmi?

agar bo'lsa, siz Setters yoki konstruktorlardan foydalanishingiz mumkin.

Agar siz setterdan foydalansangiz, Sizning Uchrashuvingiz klassi quyidagi kabi ko'rinadi:

 class Appointment {
    private Date date;
    private Time time;

    public void setDate(Date date){
        this.date = date;
    }
    public void setTime(Time time) {
        this.time = time;
    }
}

Keyin yuqoridagi kabi foydalanishingiz mumkin:

public static void main(String[] args){
  Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(); //creating an empty Appointment object
  yourAppointment.setDate(yourDate); //setting your created date in appointment
  yourAppointment.setTime(yourTime); //setting your created time in appointment
}

Yoki siz suhbatdosh sinfida o'rnashgan o'rniga konstruktordan foydalanishingiz mumkin. Endi Uchrashuv sinfi quyidagi kabi ko'rinadi:

 class Appointment {
        private Date date;
        private Time time;

       private Appointment(Date date, Time time){
           this.date = date;
           this.time = time;
       }
    }

siz shunday sana va vaqt qiymatlarini belgilashingiz mumkin:

    public static void main(String[] args){
      Date yourDate = new Date("Monday",2,1993); //the date object which you already have
  Time yourTime = new Time(5,6);//the time object that you already have

  Appointment yourAppointment = new Appointment(yourDate,yourTime); 
}
0
qo'shib qo'ydi

O'z sinfingiz uchun maxsus sinfingiz bo'lsa, boshqa sinfga kirish uchun "getter" mehodiga ega bo'lishingiz kerak. Iltimos, qabul qiluvchi va sozlovchi misolini toping:

private String myField; //"private" means access to this is restricted

    public String getMyField()
{
     //in order to have access
    return this.myField;
}
public void setMyField(String value)
{
     //in order to set value
     this.myField = value;
}
0
qo'shib qo'ydi

O'z sinfingiz uchun maxsus sinfingiz bo'lsa, boshqa sinfga kirish uchun "getter" mehodiga ega bo'lishingiz kerak. Iltimos, qabul qiluvchi va sozlovchi misolini toping:

private String myField; //"private" means access to this is restricted

    public String getMyField()
{
     //in order to have access
    return this.myField;
}
public void setMyField(String value)
{
     //in order to set value
     this.myField = value;
}
0
qo'shib qo'ydi

O'z sinfingiz uchun maxsus sinfingiz bo'lsa, boshqa sinfga kirish uchun "getter" mehodiga ega bo'lishingiz kerak. Iltimos, qabul qiluvchi va sozlovchi misolini toping:

private String myField; //"private" means access to this is restricted

    public String getMyField()
{
     //in order to have access
    return this.myField;
}
public void setMyField(String value)
{
     //in order to set value
     this.myField = value;
}
0
qo'shib qo'ydi

Siz nima qilishni istayotganingizni bilmasangiz, ba'zi kodni yozing.

Yaxshiyamki, bu sizning asosiy sinfdagi Sana sinfi va vaqt sinfi bilan ishlashi kerak.

Date d=new Date();
Time t=new Time();

Bu sharhda bo'lishi mumkin, ammo menda yetarlicha vakillar yo'q. Kechirasiz.

0
qo'shib qo'ydi

Siz nima qilishni istayotganingizni bilmasangiz, ba'zi kodni yozing.

Yaxshiyamki, bu sizning asosiy sinfdagi Sana sinfi va vaqt sinfi bilan ishlashi kerak.

Date d=new Date();
Time t=new Time();

Bu sharhda bo'lishi mumkin, ammo menda yetarlicha vakillar yo'q. Kechirasiz.

0
qo'shib qo'ydi