java继承 - 我需要一个数组吗?

时间:2015-11-16 04:43:59

标签: java inheritance

我需要一个名为 building 的通用基类,它存储建筑物的楼层数, 房间数量及其总面积。我还需要一个名为 house 的派生类继承 建筑并存储卧室和浴室的数量,以及另一个 名为 office 的派生类,它也继承建筑并存储灭火器的数量和 电话。

我必须创建一个Java应用程序来演示这个类层次结构并提供所有必需的 方法。必须使用以下主要方法测试类。

public class TestBuildings 
{

 public static void main(String[] args) 
 {
    building building1 = new building(1, 10, 6000);
    building building2 = new building();
    System.out.println();

    house house1 = new house(1, 5, 3000);
    house house2 = new house(2, 2);
    System.out.println();

    office office1 = new office(1, 2, 15000);
    office office2 = new office(2, 6, 15000, 6, 30);
    System.out.println();

    //change building properties
    building2.setFloors(3);
    building2.setRooms(12);
    building2.setFeet(20000);
    System.out.println("Building 2: \n" + building2.toString() + "\n\n");

    //change house properties
    house1.setBedrooms(3);
    house1.setBaths(3);
    house2.setFloors(2);
    house2.setRooms(10);
    house2.setFeet(4200);
    System.out.println("House 1: \n" + house1.toString() + "\n\n");
    System.out.println("House 2: \n" + house2.toString() + "\n\n");

    //change office properties
    office1.setFireExt(4);
    office1.setTelephones(50);
    System.out.println("Office 1: \n" + office1.toString() + "\n\n");
 }
}

这就是我对课程建设

所拥有的
public class building 
{
 private int floors;
 private int rooms;
 private int sqft;

 public void setFloors(int floors)
 {
    this.floors = floors;
 }

 public int getFloors()
 {
    return floors;
 }

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

 public int getRooms()
 {
    return rooms;
 }

 public void setFeet(int sqft)
 {
    this.sqft = sqft;
 }

 public int getFeet()
 {
    return sqft;
 }
}

这是我的班级 house

public class house extends building
{
 private int bedrooms;
 private int baths;

 public void setBedrooms(int bedrooms)
 {
    this.bedrooms = bedrooms;
 }

 public int getBedrooms()
 {
    return bedrooms;
 }

 public void setBaths(int baths)
 {
    this.baths = baths;
 }

 public int getBaths()
 {
    return baths;
 }
}

最后,上课办公室

public class office extends building
{
 private int fire;
 private int phones;

 public void setFireExt(int fire)
 {
    this.fire = fire;
 }

 public int getFireExt()
 {
    return fire;
 }

 public void setTelephones(int phones)
 {
    this.phones = phones;
 }

 public int getTelephones()
 {
    return phones;
 }
}

我的问题是,我必须在我的三个类中添加,减去或修改以使主方法有效?我不知道在那里需要数组吗?

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:3)

您必须为您的建筑,住宅和办公室创建包含参数的构造函数。例如,用于构建:

public building(int floors, int rooms, int sqft) {
    this.floors = floors;
    this.rooms = rooms;
    this.sqft = sqft;
}

构建没有指定参数的建筑物对我来说没有意义 - 所以建筑物应该有指定的楼层,房间,平方英尺,至少应该提供一些默认值:

public class building {
    private int floors = 1;
    private int rooms = 1;
    private int sqft = 1; // Have no idea what is default value here
    ...
}

然后将house定义为从建筑物扩展的类。这意味着您的班级是一个建筑物,并且必须指定楼层,房间和平方英尺来构建它:

 public class house extends building {
    private int bedrooms;
    private int baths;

    public house(int floors, int rooms, int sqft) {
       super(floors, rooms, sqft);
    }
    ...
 }

再说一次,只有卧室和浴室的房子的建设者没有意义,没有指定楼层,房间,sqt的信息,所以你不能正确地初始化建筑。

答案 1 :(得分:-1)

public class building implements java.io.Serializable
{
 private int floors;
 private int rooms;
 private int sqft;

 public void setFloors(int floors)
 {
    this.floors = floors;
 }

 public int getFloors()
 {
    return floors;
 }

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

 public int getRooms()
 {
    return rooms;
 }

 public void setFeet(int sqft)
 {
    this.sqft = sqft;
 }

 public int getFeet()
 {
    return sqft;

 }
}