小弟我在学习接口的时候碰到了一些问题 我的代码如下import java.applet.*;
import java.awt.*;interface Com
{
    final  int MAX=100;
    void speak(String s);
    int f(int x);
    float g(float x,float y);
}class Interf  implements Com
{
    int xuehao;
    
    public void speak(String s)
    {
    }
    
    public int f(int x)
    {
        int sum=0;
        for(int i=1;i<=x;i++)
        {
            sum=sum+i;
        }
        return sum;
    }
    
    public float g(float x,float y)
    {
        return 6;
    }
        public class Example extends Applet
    {
        Interf Li;
        public void init()
        {
            Li=new Interf();
            Li.xuehao=12345;
        }
        public void paint(Graphics g)
        {
            g.drawString("xuehao:"+Li.xuehao+"add from 1 to 100 equals to:"+Li.f(100),10,20);
        }
    }
}我的开发工具是eclipse 语法没有错误 但是在运行时却没有结果 只是给出了这么一段错误提示:
java.lang.InstantiationException: Interf$Example
at java.lang.Class.newInstance0(Class.java:293)
at java.lang.Class.newInstance(Class.java:261)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:621)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:550)
at sun.applet.AppletPanel.run(AppletPanel.java:301)
at java.lang.Thread.run(Thread.java:534)
载入:无法实例化 Interf$Example.class。

我把这小程序改成应用程序就没有问题了 请问下这段代码是什么问题啊???

解决方案 »

  1.   

    顺便问下 这个小程序保存的时候文件名应该是什么啊?? 我用的是Interf.java
      

  2.   

    文件名是Interf.java,没错.
    但这个类名的前面应该加public
      

  3.   

    首先,你的Example 是Interf  的内部类.没必要在Example 类中维护Interf  的实例.因为Example是可以访问到Inters的所有属性.包括私有成员.
    你的那个异常是说实例化Example异常.构造Example的时候,必须持有一个Interf的引用.
      

  4.   

    为什么不把Example类提取出来,名字起Example.java
      

  5.   

    将楼主的程序改了下,没有问题了// 接口的定义Com.java
    public interface Com
    {
    final  int MAX=100;
        void speak(String s);
        int f(int x);
        float g(float x,float y);
    }// Interf.java
    public class Interf  implements Com
    {
        int xuehao;
        
        public void speak(String s)
        {
        }
        
        public int f(int x)
        {
            int sum=0;
            for(int i=1;i<=x;i++)
            {
                sum=sum+i;
            }
            return sum;
        }
        
        public float g(float x,float y)
        {
            return 6;
        }
    }// Example.java
    public class Example extends Applet
    {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Interf Li;
        public void init()
        {
            Li=new Interf();
            Li.xuehao=12345;
        }
        public void paint(Graphics g)
        {
            g.drawString("xuehao:"+Li.xuehao+"add from 1 to 100 equals to:"+Li.f(100),10,20);
        }
    }
      

  6.   

    还是不对啊 6楼的程序出现了语法错误 
    当然 在六楼的程序开头加了两句话
    import java.applet.*;
    import java.awt.*;
      
    错误在接口名 Com 和 公共类名 Examplepublic interface Com 提示的错误是 The public type Com must be defined in its own file
    public class Interf  implements Com 提示的错误是 Com cannot be resolved or is not a valid superinterface
    public class Example extends Applet提示的错误是 The public type Example must be defined in its own file这又是怎么回事啊。。
      我自己的那个也没有这些错误啊
      

  7.   

    1. Example是内部非静态类, 如果要创建的话必须用interfObject.new Example(),所以这样当然不能直接起动了,把Example声明成static就行了.
    2. 6楼的改法可以,不过,一个文件里只能有一个公开的类,改法是:只把Example声明成public,文件名改为Example