繁体   English   中英

Java构造函数或新类

[英]Java Constructors or new class

嘿,我是新来的java,请原谅我要问的内容是否显而易见,但是我将尽我所能解释。

它只是为大学而设的一个项目,所以它不是认真的。

我有一个名为MarsRoom的类,该类保存有关房间所有尺寸的属性,例如墙壁的总高度和宽度,以便计算房间为了调整所需的太阳能量而遭受的热量损失使房间保持在室温下。

我遇到的问题是在构造函数中传递房间大小的属性的更好的实践或解决方案(但这可能会变得相当长,因为以下内容不仅是我可能需要的)还是专门为那个房间创建一个完全不同的类,例如ROOM TYPE U? 并在那里设置属性。

就目前而言,我可以通过用新值实例化房间来创建一个全新的房间,但是它会变得有点长,而我宁愿不为可能与另一个房间不同的另一个房间创建一个全新的类在其中一堵墙上几米!

因此,我真正想了解的是,是否可以在实例化时将那么多属性传递给构造函数?

//the instantiation in the runnable
MarsRoom room1 = new MarsRoom("RoomU", 40, 40, 20, 20, 8, 2, 4);

//the constructor in the MarsRoom class
public MarsRoom(String roomname, int windowsH, int windowsW, int wallsH, int wallsW, int windowC, int heaters, int lights){
    name = roomname;
    TotalWindowHeight = windowsH;
    TotalWindowWidth = windowsW;
    TotalWallHeight = wallsH;
    TotalWallWidth = wallsW;
    windowCeiling = windowC;
    numheaters = heaters;
    numlights = lights; 
    roomheaters = new Heaters[numheaters];
}

我想说您应该在这里添加工厂方法

基本上,保留您的构造函数,但添加类似

static Room createLaundryRoom(laundryRoomParameters) {
  return new Room(...laundry room parameters plus defaults
    common to all laundry rooms...);
}

面向对象编程的最大好处之一是可以不重复代码。 因此,对象定义了数据(成员)和功能(方法),并且不需要在运行时创建具有硬值的这些“原型”的实例 为每个房间创建一个新类

可能仅与另一间房间的墙壁相差几米

就是通过重复来拒绝OOP(和Java)。 我会坚持使用构造函数 ,如果最终出现了类似的房间,请尝试使用建议的静态工厂方法之一,或者使用继承 Oracle破坏常见功能。

使用以下键创建地图

Map<String, Integer> map = new HashMap();
map.put("TotalWindowHeight", new Integer(10));
map.put("TotalWindowWidth", new Integer(5));
...
map.put("NumberOfHeaters", new Integer(3));

MarsRoom room1 = new MarsRoom("RoomU", map);

构造函数将像:

public MarsRoom(String roomname, HashMap<String, Integer> params) {
    name = roomname;
    TotalWindowHeight = map.get("TotalWindowHeight").intValue();
    TotalWindowWidth = map.get("TotalWindowWidth").intValue;
    ...
    roomheaters = new Heaters[map.get("NumberOfHeaters").intValue()];
}

OO不好,但是好像您正在快速寻找东西。 如果您想要好的OO,则需要为Window创建一个对象,并且在其中具有高度和宽度,为天花板创建另一个对象,并且不应该将多个字段作为字段,应该具有一个数组来存储加热器对象,并且等等,但这很快并且可以满足您的要求。

您没有显示MarsRoom的字段是什么,但是对于每个功能,我都会有一个子对象集合。 MarsRoom具有-Windows列表。 MarsRoom具有“墙壁列表”。 等等...然后为每个提供者的setter和getter和方法添加这些功能的新实例。

由于这是在学校里,所以我只包含了一些伪代码。

 public class MarsWindow {
    int     height;
    int     length;

    // Setters & Getters
    // standard getters & setters go here

    int getArea() {
        return this.height * this.width;
    }
}

public class MarsRoom {
    List<MarsWindow>   windows;
    List<MarsWall>     walls;
    List<MarsLight>    lights;
    List<MarsHeater>   heaters;

    public List<MarsWindow> addWindow(MarsWindow window) {
        // Add a window to the "windows" list here
    }

    public List<MarsWall> addWall(MarsWall wall) {
        // Add a wall to the "walls" list here
    }

    // Do this for the other fields

    int getTotalWindowArea() {
        int area = 0;
        // Iterate over all windows
        for(MarsWindow window : windows) {
            area += window.getArea();
        }
        return area;
    }

    // Add other calculation methods here

}

尽管从技术上讲是合法的,但参数列表很长的构造函数可能不方便使用。 这也取决于您此列表在将来还是在子类中增长。

如果您有很多参数,但是它们有默认值,有时只需要更改几个,那么您可能会发现Builder模式很有用。 这个想法是用函数调用替换构造函数参数,并允许将它们链接起来,例如:

public MarsRoom() {
    //empty or just basic stuff set here
}

public MarsRoom setTotalWindowHeight(int TotalWindowHeight) {
    this.TotalWindowHeight = TotalWindowHeight;
    return this;
}

public MarsRoom setTotalWindowWidth(int TotalWindowWidth) {
    this.TotalWindowWidth = TotalWindowWidth;
    return this;
}

...

然后,您可以致电:

MarsRoom room1 = new MarsRoom()
    .setTotalWindowHeight(20)
    .setTotalWindowWidth(40);

当然,如果您想以这种方式设置所有参数,则它比单个构造函数更长(您可能更可读)。 但是,如果只设置10个参数中的2个,通常会更方便。

如果您只是想不复制传递给构造函数的参数,则可以将它们放在单独的静态方法中,如下所示:

public static MarsRoom newRoomU() {
    return new MarsRoom("RoomU", 40, 40, 20, 20, 8, 2, 4);
}

您还可以使用某种多态性,或者使用不同类型的房间,或者与此类似的东西,然后使用具有所有房间将具有的公共值的超类。

您还可以具有多个构造函数,并根据房间类型等为希望设置的值使用不同的构造函数。

与对象而不是基元一起使用总是更好,您可以使用工厂创建对象。

    //the constructor in the MarsRoom class
public MarsRoom(String roomname, WindowDimension windowDimension, WallsDimensions wallDimension, RoomAmbience ambience){
    }

    public class WindowDimension{
 private int height; //int windowsH
 private int width; //int windowsW
 private int circumference; //assumed windowC is circumference
}

public class WallsDimension{
 private int height; //int wallsH
 private int width; //int wallsW
}

public class RoomAmbience{
    private int heaters;
    private int lights;
}

暂无
暂无

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

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