简体   繁体   English

Java-对象中具有相同数据类型的多个字段

[英]Java - Multiple fields of same data type in an object

I was practicing inheritance and composition after a lesson and decided to write a small program to try some things out, stumbled upon a problem, I'll show my code, it has 4 classes including the Main.java: 一堂课后,我正在练习继承和合成,并决定编写一个小程序来尝试一些事情,偶然发现一个问题,我将展示我的代码,它有4个类,其中包括Main.java:

public class Main {

public static void main(String[] args) {

    Person person1 = new Person("Person1", 170); //"Name", height
    Person person2 = new Person("Person2", 200); //"Name", height

    Bed bed1 = new Bed(160);

    Bedroom bedroom1 = new Bedroom(bed1, person1);

    bedroom1.sleep();

public class Bedroom {

private Bed theBed;
private Person thePerson;

//Constructors

public void sleep() {
    if(thePerson.getHeight() > 180) {
        System.out.println("You are too tall to sleep on this bed.");
    } else {
        theBed.sleepOnBed();
    }
}

//Getters

public class Bed {

private Person thePerson;
private int height;

//Constructor

public void sleepOnBed() {
        System.out.println("You sleep on the bed.");
}

//Getters 

public class Person {

private String name;
private int height;

//Constructor

//Getters

What I want to do is to use both person1 and person2 in my bedroom1 object in Main.java and then test the sleep() method on both of them but I just can't figure out a way to use it. 我想要做的是在Main.java的bedroom1对象中同时使用person1person2 ,然后在bedroom1对象上测试sleep()方法,但我只是想不出一种使用方法。

I tried things like: 我尝试过类似的事情:

public class Bedroom {

private Bed theBed;
private Person thePerson1;
private Person thePerson2;

public Bedroom(Bed theBed, Person thePerson1, Person thePerson2) {
    this.theBed = theBed;
    this.thePerson1 = thePerson1;
    this.thePerson2 = thePerson2;
} 


public class Main {

public static void main(String[] args) {

    Person person1 = new Person("Person1", 170);
    Person person2 = new Person("Person2", 200);

    Bed bed1 = new Bed(160);

    Bedroom bedroom1 = new Bedroom(bed1, person1, person2);

    bedroom1.sleep(); 

But as you might understand, that lead to nothing. 但是,正如您可能理解的那样,这不会导致任何结果。 I'm just so tired and can't find any leads online, might be because I use the wrong keywords idk. 我太累了,无法在线找到任何潜在客户,可能是因为我使用了错误的关键字idk。

I want my program to take multiple objects of data type Person and see if their height matches the conditions to sleep in the bed, that's pretty much it. 我希望我的程序采用多个数据类型为Person对象,并查看它们的高度是否符合在床上睡觉的条件,仅此而已。

You can replace Person with a List of Person objects in the bedroom class and then loop over the array in the sleep method. 您可以在bedroom类中将Person替换为Person对象列表,然后在sleep方法中遍历数组。

public Bedroom(Bed bed1, List<Person> aListOfPersons)
{
  this.persons = aListOfPersons;
}

public void sleep()
{
  for(Person aPerson : persons)
   {
      //check for each aPerson if he fits in the bed
   }
 }

Welcome to Stackoverflow and happy coding! 欢迎使用Stackoverflow,祝您编程愉快!

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

相关问题 从具有相同类型的多个字段的字符串构造到嵌套对象 - Mapstruct from string to nested object for multiple fields with the same type 从一个 object 复制 not null and not empty fields 到 java 中的另一个 object 相同类型(对象是相同类型) - Copy not null and not empty fields from one object to another object of the same type(Objects are same type) in java Java - 使用相同数据类型的多个构造函数 - Java - Multiple constructors using same data type Java 8 Streams - 使用流将相同类型的多个对象映射到列表 - Java 8 Streams - Map Multiple Object of same type to a list using streams 具有多个字段的Java JTable对象 - Java JTable object with multiple fields 如何在 jooq/java 中字段的自定义数据类型中对 object 数据类型进行类型转换 - How to typecast object data type in custom data type of fields in jooq/java 如何声明具有相同数据类型的多个参数? (爪哇) - How to declare multiple parameters with the same data type? (java) 从一个列表中提取多个字段并将其保存到另一列表或相同类型的新列表中。 在Java 8中 - Extracting multiple fields from one list and save it to another list or same type new list. In java 8 映射两个 java object 有相同的字段 - Mapping two java object have same fields 获取Java中通用类型对象的字段 - Get fields of generic type object in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM