我正在看一個程序,它包含一條我不理解的行。可以解釋一下這段代碼在Java中的含義嗎?
NoticeBoard notice = (NoticeBoard) o;
那段代碼在幹什麼?
這條線就是從這裏取(張貼這一點,因爲你們可能想了解該行的完整上下文):
import java.util.Observable;
import java.util.Observer;
class NoticeBoard extends Observable
{
private String theNotice = "";
public void setTheNotice(final String notice)
{
theNotice = notice;
setChanged();
notifyObservers();
}
public String getTheNotice()
{
return theNotice;
}
}
class NoticeBoardObserver implements Observer
{
public void update(Observable o, Object arg)
{
NoticeBoard notice = (NoticeBoard) o;
System.out.println(notice.getTheNotice());
}
}
class Main
{
public static void main(String args[])
{
NoticeBoard floor4 = new NoticeBoard();
NoticeBoardObserver anObserver = new NoticeBoardObserver();
floor4.addObserver(anObserver);
floor4.setTheNotice("Its summer");
}
}
它的類型轉換。 – Layke 2011-04-17 00:07:10
[The Java Tutorials - Learning the Java language - Inheritance](http://download.oracle.com/javase/tutorial/java/IandI/subclasses.html) – BalusC 2011-04-17 00:09:25