我们可以在未派生的类中使用超级关键字吗? (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;
}
}
书中是否有可能或写错了?
答案 0 :(得分:4)
一切都是从Object派生的,所以答案总是是,你可以使用super(),只要它是构造函数的第一个代码行。当然,它不会做任何有建设性的事情,但你仍然可以称之为。
答案 1 :(得分:1)
除了Object之外,所有类都是派生的,而您并没有这样写。所以问题毫无意义。
没有