有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为用户定义的类使用setDate的数组。。JAVA

我创建了一个具有3个属性的用户定义类Date,day,month,year,然后我将Date类调用到我的约会类中,它变成了private Date。。现在我需要使用setter和getter。。我知道如何为单个属性创建setter和getter,但对于像setDate这样的整个类,我在约会类中调用了它,我不知道如何做到这一点。。这就是我试图做的,但是值没有改变

ArrayList<Appointment> ai= new ArrayList();
        Appointment ap= new Appointment(date,time);
        ap.setDoctor_id("1");
        ap.setMedication("w");
        ap.setPatient_id("3");
        ap.setProblem("pro");
        ap.setRoom("ss");
        ai.add(ap);

        Appointment o=null;
        try
        {
            //Creating a deep clone of ap and assigning it to o

            o = (Appointment) ap.clone1();
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
        date.setDay(4);
        date.setMonth(9);
        date.setYear(2000);
        o.setDate(date);

        o.setRoom("pp");
        o.setProblem("Happiness");
        o.setMedication("Anti-Happiness pills");
        ai.add(o);

        Appointment t= (Appointment) o.clone1();

        date.setDay(1);
        date.setMonth(12);
        date.setYear(18);
        t.setDate(date);
        t.setRoom("p");
        t.setProblem("depressin");
        t.setMedication("Anti-depression pills");

        ai.add(t);

共 (1) 个答案

  1. # 1 楼答案

    如果你知道如何使用setter来表示整数,你可以这样做:

    private int setDay(int day) {
     this.day = day
    }
    

    您还知道如何创建新对象:

    Appointment ap= new Appointment(date,time);
    

    对于一个可能看起来像:

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

    如果你的约会构造器看起来像:

    public Date(int day, int month, int year) {
      this.day = day;
      this.month = month;
      this.year = year;
    }
    

    您可以按照与新约会类似的方式进行新约会:

    Date newDate = new Date(day, month, year)
    

    你可以用同样的方法做二传手!它要么看起来像:

    private setDate(int day, int month, int year) {
       this.date = new Date(day, month, year);
    }
    

    或者:

    private setDate(Date date) {
       this.date = date;
    }
    

    对于上面的一个,你必须先确定日期,这样你就可以这样称呼它:

    Date newDate = new Date(day,month,year);
    setDate(newDate);