Dimension getSize() Returns the size of this component in the form of a Dimension object. Dimension getSize(Dimension rv) Store the width/height of this component into "return value" rv and return rv. Toolkit getToolkit() Gets the toolkit of this component. Object getTreeLock() Gets the locking object for AWT component-tree and layout Gets this component's locking object (the object that owns the thread sychronization monitor) for AWT component-tree and layout operations. 先别急着给分,问题还没弄明白呢 上面这个是jdk1.4的文档。getToolkit() 这个方法肯定是有的。他是在Component类中的方法 我的程序是这样的: import java.applet.*; import java.awt.*; import java.event.*; public class ex1 extends Applet implements ActionListener { Button button; .... Toolkit toolkit; pubilc void init() { .... button = new Button(); toolkit = getToolkit();//获得一个工具包对象 ... button.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) { toolkit.beep();//发出“嘟嘟嘟”声 ...} } }
我认为toolkit不是Toolkit类的实例,java是区分大小写的,它应该是其他类的实例。你可以把代码贴出来
你说的对getToolkit()确实不是Toolkit类中的方法。他是Component类中的方法。声明形式如下:
public Toolkit getToolkit()
根本就不是静态方法。
beep()同理。
书上的代码如下:
Toolkit toolkit;
....
toolkit = getToolkit();//获得一个工具包对象
...
toolkit.beep();//发出“嘟嘟嘟”声
public abstract void beep()
Emits an audio beep.
getDefaultToolkit()为Toolkit类的static方法,所以可以用“.”直接调用不用实例化Toolkit类的子类对象可以赋为Toolkit对象,getToolkit()方法得到的可以是其默认子类的 一个对象然后返回为toolkit
并且在那个子类中已经对beep()进行了重构
toolkit.beep()用到了多态性,动态绑定 所以可以这样使用
******给分 给分********
Returns the size of this component in the form of a Dimension object.
Dimension getSize(Dimension rv)
Store the width/height of this component into "return value" rv and return rv.
Toolkit getToolkit()
Gets the toolkit of this component.
Object getTreeLock()
Gets the locking object for AWT component-tree and layout Gets this component's locking object (the object that owns the thread sychronization monitor) for AWT component-tree and layout operations.
先别急着给分,问题还没弄明白呢
上面这个是jdk1.4的文档。getToolkit() 这个方法肯定是有的。他是在Component类中的方法
我的程序是这样的:
import java.applet.*;
import java.awt.*;
import java.event.*;
public class ex1 extends Applet implements ActionListener
{
Button button;
....
Toolkit toolkit;
pubilc void init()
{
....
button = new Button();
toolkit = getToolkit();//获得一个工具包对象
...
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
toolkit.beep();//发出“嘟嘟嘟”声
...}
}
}
Toolkit是一个抽象类没错
应该用Tookit.getDefaultToolkit();
他返回的是一个Toolkit的subclass文档说的很清楚
其实Sun写的跟自己创建类和对象都是一样的,他不会作弊的。
你只要搞清楚基本的东西,不要去怀疑他。
然后想想他用了什么高明的技术了,然后自己想明白了,那么你也可以“骗”别人了。
里面把这个问题作为例子。
import java.awt.*;
import java.event.*;
public class ex1 extends Applet implements ActionListener
{
Button button;
....
Toolkit toolkit;
pubilc void init()
{
....
button = new Button();
toolkit = getToolkit();//我觉得这里并不是Toolkit的实例,而是它的子类的一个实例,这里给隐藏了而已,用Toolkit申明toolkit只不过得到一个引用而已...
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
toolkit.beep();//这里也不是用的Toolkit的方法,而是它的子类的方法。子类对它的实现进行了封装。
...}
}
}
这里用到了Factory模式
getToolkit()方法是Component类中的一个方法,它的返回的是一个Toolkit对象。所以所有Component类的子类都继承了这个方法。
程序中用的getToolkit()方法就是调用Button类中的这个方法用于返回一个Toolkit对象,然后通过这个对象调用Toolkit类中的beep()方法。我觉得是书上调用beep()方法时写错了。书的作者可能认为这个beep()方法是实现了的方法。但他确是一个抽象方法。我觉得抽象方法不可以这么调用。不知道你门觉得意下如何?
如还有高论,本人洗耳恭听!
如果没有就结贴了。
谢谢后来几位朋友的建议。