简体   繁体   English

如何在JAVA中将构造函数中的值传递给另一个类中的setter?

[英]How to pass a value from a constructor to a setter in another class in JAVA?

Im learning object oriented programming in school right now, and there are some aspects of it I don't quite understand yet. 我现在在学校学习面向对象编程,有一些方面我还不太了解。 I have a program that creates a database of users with their names, and birthdates. 我有一个程序,用他们的名字和生日创建一个用户数据库。 So I have 3 classes: person, PersonProgram(the main), and Date. 所以我有3个类:person,PersonProgram(主要)和Date。 The Person class has the constructor, setter, and getters for the names and the birth date. Person类具有名称和出生日期的构造函数,设置器和getter。 The Date class has error checking for proper dates and leap years etc. In the main program I create 5 People, and then give menu options to change and modify the names and dates. Date类有错误检查正确的日期和闰年等。在主程序中我创建了5个人,然后给出菜单选项来更改和修改名称和日期。 So for example, if the user wants to change the name my code looks like this: 例如,如果用户想要更改名称,我的代码如下所示:

System.out.println("Enter new first name:");
people[choice-1].setFirstName(input.next());

and that works and makes sense to me. 这对我来说很有意义。 But I want to know how I can change the date properly? 但我想知道如何正确更改日期? The Date constructor takes 3 integers for the day, year, and month, so in the main program I prompt the user to input the 3 new dateswhich are stored in day, month, year integers. Date构造函数占用日,年和月的3个整数,因此在主程序中,我提示用户输入以日,月,年整数存储的3个新日期。 So my understanding is from there I would pass those 3 integers to the Date constructor: 所以我的理解是从那里我将这3个整数传递给Date构造函数:

new Date(month, day, year);

What I am confused on is where to go from there. 我感到困惑的是从哪里去。 The Date constructor gets the new Date call, and passes it to the setters. Date构造函数获取新的Date调用,并将其传递给setter。 How can this newly created date object be passed back to the Person program, so the setter in Person for the birthdate can update the corresponding Person object? 如何将这个新创建的日期对象传递回Person程序,那么生日期的Person中的setter可以更新相应的Person对象吗? If I am not clear on my question please let me know, I figured I could articulate what I am trying to ask without posting all my code. 如果我不清楚我的问题,请让我知道,我想我可以清楚地表达我想要问的内容而不发布我的所有代码。

In your Person class you should have something like this: 在你的Person类中你应该有这样的东西:

public class Person {

    private Date birthDate;

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate){
        this.birthDate = birthDate;
    }
}

And then in you would set the birthDate like: 然后你会将birthDate设置为:

person[choice-1].setBirthDate(new Date(month,day,year));

Taking into consideration that you are starting with OOP there is an important concept here, Encapsulation, the Person class restricts the free access to its fields, like birthDate , and sets the rules for the interaction with them. 考虑到你从OOP开始,这里有一个重要的概念,Encapsulation, Person类限制对其字段的自由访问,如birthDate ,并设置与它们交互的规则。 As an example you could check if the date is null before assigning it. 作为示例,您可以在分配日期之前检查日期是否为空。

public void setBirthDate(Date birthDate){
    if(birthDate != null) {
        this.birthDate = birthDate;
    } else {
        //Whatever you wanna do here (throw an Exception, etc, etc)
    }
}

Comment Question 评论问题

Although it would be better to create another question : 虽然最好再创建一个问题

Do I have to create an instance of the Date class in my Person class? 我是否必须在Person类中创建Date类的实例? Or anywhere for that matter? 或者在任何地方?

No, the property/field birthDate is a reference to a Date object which will be stored in memory until no references are left. 不,属性/字段birthDate是对Date对象的引用 ,该对象将存储在内存中,直到没有引用为止。 And it's up to you where to create them, nonetheless there are Creational Patterns , a familiy of Design Patterns that help you with this matter. 这取决于你在哪里创建它们,尽管如此,还有Creational Patterns ,一个设计模式的家族,可以帮助你解决这个问题。

is it the birth date in person class of type 'Date'? 它是“日期”类型的出生日期吗?

if so, you should create an instance of your class Date , do the control that you need and pass it to the constructor or setter of the birth date in the persson instance . 如果是这样,您应该创建类Date的实例,执行您需要的控件并将其传递给persson实例中的出生日期的构造函数或setter。

Date birthDate = new Date(month, day, year);
// Some controls
people[choice-1].setBirthDate(birthDate);
Date the_birth_date = new Date(mounth, day, year);
people[choice - 1].setBirthDate(the_birth_date);

You can set it like this: people[choice-1].setBirthday(new Date(month, day, year)); 您可以这样设置: people[choice-1].setBirthday(new Date(month, day, year)); . You would have to give the option to select the Person first. 您必须先选择选择Person。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM