我们可以在未派生的类中使用“超级”关键字吗?

时间:2012-06-10 21:48:45

标签: java super

我们可以在未派生的类中使用超级关键字吗? (Sun认证程序员Java 2中有这样的代码,第7章)。这里,它在OfficeRoom类中使用,而不是子类。

1st question example code:
Building.Java

public class Building
{
long length;
long width;

OfficeRoom [] officeRooms; // The building has number of rooms

public Building (long len,long wid,OfficeRoom [] ors)
{
length = len;
width = wid;
officeRooms = ors;
}

public long area()
{
return length * width;
}
}




OfficeRoom.java

public class OfficeRoom
{
long length;
long width;

public Office(long len,wid)
{
super(len,wid)  //What is the job of super here?? It is not inside a subclass??
}

public long area()
{
return length * width;
}
}

书中是否有可能或写错了?

2 个答案:

答案 0 :(得分:4)

一切都是从Object派生的,所以答案总是,你可以使用super(),只要它是构造函数的第一个代码行。当然,它不会做任何有建设性的事情,但你仍然可以称之为。

答案 1 :(得分:1)

  1. 除了Object之外,所有类都是派生的,而您并没有这样写。所以问题毫无意义。

  2. 没有