-4

我们可以在非派生类中使用 super 关键字吗?(Sun Certified Programmer for 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;
}
}

有可能还是书上写错了?

4

2 回答 2

4

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

于 2012-06-10T21:49:43.130 回答
1
  1. All classes are derived except for Object, and you aren't writing that. So the question is meaningless.

  2. No.

于 2012-06-11T00:42:48.710 回答