类不能被实例化 [英] class can't be instantiated

查看:218
本文介绍了类不能被实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java Applet和显卡的一个问题。我试图在Eclipse中运行它,它会失败。
即时通讯在新的java,我希望你能帮助我。
我有两个文件:Say.java和SayWhat.java。
Say.java:

I have a problem with java applet and graphics. I'm trying to run it in Eclipse and it fails. Im new in java and i hope you can help me. I have two files: Say.java and SayWhat.java. Say.java:

public class Say {
    SayWhat word = new SayWhat("Hello World");

}

SayWhat.java:

SayWhat.java:

import java.applet.Applet;
import java.awt.Graphics;

@SuppressWarnings("serial")
public class SayWhat extends Applet {
     Graphics g;
     String what;
    public SayWhat(String what) {
        this.what=what;
    }
    public void paint(Graphics g){
        g.drawString(what, 20, 20);
    }
}

这显示错误是:

load: SayWhat.class can't be instantiated.
java.lang.InstantiationException: SayWhat
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.applet.AppletPanel.createApplet(Unknown Source)
    at sun.applet.AppletPanel.runLoader(Unknown Source)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

能否请你告诉我,我究竟做错了什么?

Can you please tell me what am i doing wrong?

推荐答案

这是小应用程序都需要有一个公共的无参数的构造函数(或者由具有明确公有的无参数的构造函数,或者具有所有没有明确的构造;在后一种情况下,编译器将提供一个公共的无参数的构造函数作为默认)。类唯一的构造函数的参数:

An applet needs to have a public no-arg constructor (either by having an explicit public no-arg constructor, or by having no explicit constructors at all; in the latter case, the compiler will supply a public no-arg constructor as a default). Your class's sole constructor takes an argument:

public SayWhat(String what) {

因此​​类不能没有这样的说法被实例化,因此它不能被用来作为一个applet

so the class can't be instantiated without that argument, so it can't be used as an applet.

这篇关于类不能被实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆