微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

所以我迷失在 Java 中创建类、私有/公共变量、get/set 函数等

如何解决所以我迷失在 Java 中创建类、私有/公共变量、get/set 函数等

这是我作业的第一部分: 创建一个名为“House”的新 Java 类。

Add constructor without parameters and add a system out put of your choice
Add 4 Private Member Variables
    an integer that represents the number of rooms
    a String that represents the address
    a boolean that represents if the house is for sale
    a double that represents the value of the house
Add a Get and Set functions for ALL 4 member variables

在我的房屋代码中,我认为这是正确的做法:

public class House {
private int Rooms;
private String Address;
private double Value;
private boolean Sale;

public House() {
    System.out.println("CONSTRUCTOR EXECUTED");
}

public void setRooms(int Rooms) {
    this.Rooms = Rooms;
}

public int getRooms () {
    return Rooms;
}

public void setAddress (String Address){
    this.Address = Address;
}

public String getAddress() {
    return Address;
}

public void getValue(double Value) {
    this.Value = Value;
}

public double setValue() {
    return Value;
}

public void getSale(boolean Sale) {
    this.Sale = Sale;
}

public boolean getSale() {
    return Sale;
}

}

对于第二部分,我更困惑的是:

Create a Main Class and add the static main function
Initialize Main and add a start function
In the start function create new House object
    Call all 4 Set functions
    Output all 4 Get Functions
        Example Output "The House has '4' rooms located at 'address' and is or is not for sale'
        If the house is for sale,out the value of the house

所以我创建了主文件,我认为我的启动是正确的,但我不知道如何将其全部打印出来。到目前为止,这至少是我所拥有的:

public class Main {
public static void main(String[] arg) {
}

private void start() {
    House rooms = new House ();


}

}

我绝对不想要答案,只是一些正确方向的指示。

解决方法

查看说明,我认为您不需要创建单独的 Main 类。我认为当它说“Initialize Main”时,它只是意味着将 main 方法添加到 House 类中。

此外,您现在拥有的方式无法正常工作。 start() 将无法从 main 方法调用,因为 main 是静态的,而 start 不是。

我的感觉是最好的方法是将 mainstart 放在 House 类中。然后,您可以在 House 方法中初始化 main 的实例,然后对该实例调用 start。从那里,start 可以执行其余的说明。

希望这会有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。