当前位置: 代码迷 >> Java相关 >> [求助]多态中的问题
  详细解决方案

[求助]多态中的问题

热度:338   发布时间:2006-03-24 20:36:00.0
[求助]多态中的问题

我编了一道程序
能够通过编译
可是我想让它运行时
却出现了很多问题


import java.awt.Graphics;
import java.applet.Applet;
class DrawTool
{
public void paint(Graphics g)//本来在这里书上说的是abstract可是不知道为什么 //不能通过编译
{

}
}
class LineTool extends DrawTool
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,300);
}
}
class RectTool extends DrawTool
{
public void paint(Graphics g)
{
g.drawRect(1,1,10,10);
}
}

public class WelcomeApplet
{
public static void main(Graphics g)
{
g.drawString("我爱我家",250,51);
DrawTool tool1=new RectTool();
DrawTool tool2=new LineTool();
tool1.paint(g);
tool2.paint(g);
}
}
<HTML>
<HEAD>
<TITLE> Welcome Java Applet </TITLE>

</HEAD>

<BODY>
<applet

code="WelcomeApplet.class"
width=600
height=500>
</applet>

</BODY>
</HTML>

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>f:

F:\>cd java\myjava\

F:\java\myjava>appletviewer WelcomeApplet
java.lang.ClassCastException: WelcomeApplet
at sun.applet.AppletPanel.createApplet(AppletPanel.java:721)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:650)
at sun.applet.AppletPanel.run(AppletPanel.java:324)
at java.lang.Thread.run(Thread.java:595)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component
argument pData
at sun.awt.windows.Win32SurfaceData.initOps(Native Method)
at sun.awt.windows.Win32SurfaceData.<init>(Win32SurfaceData.java:448)
at sun.awt.windows.Win32SurfaceData.createData(Win32SurfaceData.java:316
)
at sun.awt.Win32GraphicsConfig.createSurfaceData(Win32GraphicsConfig.jav
a:357)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(WComponentPeer.java
:332)
at sun.awt.windows.WComponentPeer.replaceSurfaceData(WComponentPeer.java
:313)
at sun.awt.windows.WComponentPeer$2.run(WComponentPeer.java:353)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

F:\java\myjava>

虽然比较多
但是问题还是比较基础的
望大家give me a hand

搜索更多相关的解决方案: 多态  

----------------解决方案--------------------------------------------------------
public static void main(Graphics g)
main函数的参数怎么可能是Graphics呢
----------------解决方案--------------------------------------------------------

import java.awt.Graphics;
import java.applet.Applet;
class DrawTool
{
public void paint(Graphics g)//本来在这里书上说的是abstract可是不知道为什么 //不能通过编译
{

}
}
class LineTool extends DrawTool
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,300);


}
}
class RectTool extends DrawTool
{
public void paint(Graphics g)
{
g.drawRect(1,1,10,10);
}
}

public class WelcomeApplet extends Applet//主类要继承Applet否则不能实例化LineToo类和RectTool类自然运行Applet后就什么都没有啊
{

public void paint(Graphics g)//这里的参数我也改过来了
{

g.drawString("我爱我家",250,51);
(new RectTool()).paint(g);
(new LineTool()).paint(g);

}
}
不知道你是不是想达到以上程序的运行效果!


----------------解决方案--------------------------------------------------------

谢谢你们


----------------解决方案--------------------------------------------------------
  相关解决方案