xiaolaba3330 发表于 2008-4-23 00:17

关于内部类写测试类的问题

package java1;

public class Group4
{
    public class Student1         //抽象内部类
    {
        int count;
        String name;
         public void output() {}              //抽象方法
    }
    public class Student extends Student1//继承抽象内部类
    {
        public Student(String n1)
        {
            name = n1;
            count++;                                 //Student.count
        }
        public void output()                         //实现抽象方法
        {
            System.out.println(this.name +"  count="+this.count);
        }
    }
  [color=Red] /* public Group4()
    {
        Student s1 = new Student("A");
        s1.output();
        Student s2 = new Student("B");
        s2.output();
    }
    public static void main (String args[])
    {
        Group4 g4 = new Group4();
    }
} */[/color]
}
这是例题的源程序,其中红色的部分我想给它写到另外的一个测试类中,
package java1;

public class text {
    public static void main(String[] args) {
        Group4 g4 = new Group4();
      [color=Cyan]Student s1,s2;[/color] }
}

但是蓝色的部分却报错,为什么?应该怎么改啊?

huwangvs 发表于 2008-4-23 09:16

Group4.Student s1,s2;

sunkaidong 发表于 2008-4-23 09:45

用内部类是为了实现多继承的破开java单继承的限制的..楼上的方式没错...

xiaolaba3330 发表于 2008-4-23 13:54

谢谢2位,我去试试,有问题还会再来的!!

xiaolaba3330 发表于 2008-4-23 14:16

还是不明白,麻烦帮我写个测试类吧! 谢谢啦!!

  Group4.Student s1,s2;
        Group4.Student s1=new Student("A");
         s1.output();

或者帮我改一下这个!!

[[it] 本帖最后由 xiaolaba3330 于 2008-4-23 14:17 编辑 [/it]]

huwangvs 发表于 2008-4-23 15:14

Group4 group = new Group4();
Group4.Student s1=group.new Student("A");

都写成内部类了,何必这么麻烦还要到外面去实现呢。。。。。

xiaolaba3330 发表于 2008-4-23 16:45

回复 6# 的帖子

我是初学者,每个例题都想用各种方法练练手,熟悉一下!

谢谢这位大侠了!!

页: [1]

编程论坛