gpt4 book ai didi

java - 从对象内的对象调用方法?(Java)

转载 作者:行者123 更新时间:2023-11-30 02:54:11 25 4
gpt4 key购买 nike

我尝试过搜索和阅读(听起来类似)问题,但我似乎找不到解决方案。

我目前正在制作一个原型(prototype)来通过文本输入控制一些灯。文本输入已经正常工作,它可以正确过滤输入并仅挑选出相关部分。

无论如何,现在为了使其有用,我需要首先创建我的房子的对象,其中包含所有楼层,其中包含所有房间,其中包含所有 Actor (灯等)。

这些是所有这些对象的片段,从上到下,因为它们应该堆叠在彼此内部(代码尚未完成也未优化,我知道我可以对所有 if-else 语句使用 switch-case ):

import java.util.ArrayList;

public class Building {
String Name;
ArrayList<floor> floor = new ArrayList<floor>();

public Building(String Name){
this.Name = Name;
}

public boolean checkFloor(String Name){
int count = floor.size();
boolean ret = false;
while(count < floor.size()){
if (floor.get(count).Name == Name){
count = floor.size();
ret = true;
}
count++;
}
return(ret);
}

public void newFloor(String Name){
Name = Name.toLowerCase();
if (Name == "keller" || Name == "basement"){
floor basement = new floor("basement");
floor.add(basement);
}
else if (Name == "eg"){
floor ground = new floor("eg");
floor.add(ground);
}
else if (Name == "1og"){
floor first = new floor("first");
floor.add(first);
}
else if (Name == "2og"){
floor second = new floor("second");
floor.add(second);
}
else if (Name == "3og"){
floor third = new floor("third");
floor.add(third);
}
}
public void newRoom(String Floor, String Name){
}

}
<小时/>
    public class floor {
String Name;
ArrayList<room> room = new ArrayList<room>();
transcoder transcode = new transcoder();

public floor(String Name){
this.Name = Name;
}



public boolean checkRoom(String Name){
int count = room.size();
boolean ret = false;
while(count < room.size()){
if (room.get(count).Name == Name){
count = room.size();
ret = true;
}
count++;
}

return(ret);
}

public void newRoom(String mName){
String Name = transcode.getRoom(mName);
if( Name == "ground"){
room ground = new room("ground");
room.add(ground);
}
else if ( Name == "basement"){
room basement = new room("basement");
room.add(basement);
}
else if ( Name == "first"){
room first = new room("first");
room.add(first);
}
else if ( Name == "second"){
room second = new room("second");
room.add(second);
}
}
}
<小时/>
public class room {
String Name;
ArrayList<Actor> Actors = new ArrayList<Actor>();

public room(String Name){
this.Name = Name;
}

public void addActor(String Name, int Type, String Address, int Channel, boolean Dim){
Actors.add(new Actor(Name, Type, Address, Channel, Dim));
}

public void removeActor(String Name){
int count = 0;
while (count <= Actors.size()){
if (Actors.get(count).Name == Name){
Actors.remove(count);
count = Actors.size();
}
count++;
}
}

public boolean containsActor(String Name){
int count = 0;
boolean ret = false;
while (count < Actors.size()){

if (Actors.get(count).Name == Name){
ret = true;
count = Actors.size();
}
count++;
}
return(ret);
}

public String getAddress(String Name){
int count = 0;
String ret = "leer";
while (count < Actors.size()){
if (Actors.get(count).Name == Name){
ret = Actors.get(count).Address;
count = Actors.size();
}
count++;
}
return(ret);
}

public int getType(String Name){
int count = 0;
int ret = 0;
while (count < Actors.size()){
if (Actors.get(count).Name == Name){
ret = Actors.get(count).Type;
count = Actors.size();
}
count++;
}
return(ret);
}

}
<小时/>
public class Actor {
String Name;
String Address;
int Channel;
int Type;
boolean Dim;
int On; //muss noch deklariert werden!
int Off;


public Actor(String Name, int Type, String Address, int Channel, boolean Dim){
this.Name = Name;
this.Type = Type;
this.Address = Address;
this.Channel = Channel;
this.Dim = Dim;
}
}

我现在在主类中尝试做的是创建一个带有地板、房间和一些 Actor 的新建筑。代码如下:

System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor

现在在 Building.floor 的最后,我不太明白如何向对象楼层添加一个新房间。我很感激任何帮助,因为我还不太喜欢 Java,并且可能缺少一个明显的部分。

最佳答案

为了回答您的问题,在建筑物的地板上添加新房间的最简单方法是:

Building.floor.get(0).newRoom(Name);

floor 是一个 ArrayList,在第一个索引上调用 get() 将允许您调用 newRoom()

一些使您的代码更具可读性/易于理解的建议:

  • 通过将 floor 更改为 Floor,将 room 更改为 Room,将类名称大写。

  • ArrayList 的名称从 floor 更改为 floors

  • ArrayList 的名称从 room 更改为 rooms
  • 在主类中声明单独的 String 变量来存储用户输入。
  • 在添加到适当的 ArrayList 之前单独存储对象。

我想说的最重要的事情是向您的 Building 类添加一个 getFloor() 方法。

像这样(假设您进行了上述更改):

public Floor getFloor(String floorName){
Floor correctFloor = null;
for(Floor floor : floors) {
if(floor.Name == floorName) {
correctFloor = floor;
}
}
return correctFloor;
}

那么你代替

System.out.println("Gebäudename eingeben(egal): ");
String Name = user_input.nextLine();
Building Building = new Building(Name);
System.out.println("Stockname eingeben(eg): ");
Name = user_input.nextLine();
Building.newFloor(Name);
System.out.println("Raumname eingeben(wohnen): ");
Name = user_input.nextLine();
Building.floor.get(0).newRoom(Name);

您可以输入(再次假设上述更改)

System.out.println("Gebäudename eingeben(egal): ");
String buildingName = user_input.nextLine();
Building building = new Building(buildingName);
System.out.println("Stockname eingeben(eg): ");
String floorName = user_input.nextLine();
Floor floor = new Floor(floorName);
building.newFloor(floorName);
System.out.println("Raumname eingeben(wohnen): ");
String roomName = user_input.nextLine();
building.getFloor(floorName).newRoom(roomName);

这些只是建议,您可以通过多种方式做到这一点~祝你好运!

关于java - 从对象内的对象调用方法?(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com