Topic: 新手 一段代码编译错误求助 ('class' or 'interface' expected) |
Print this page |
1.新手 一段代码编译错误求助 ('class' or 'interface' expected) | Copy to clipboard |
Posted by: zxhhero Posted on: 2004-10-19 20:59 下面一段代码在编译时报错,我以前也引入过“java.util.*"没有错误,在这里请问是什么问题?如何解决?感谢先!! public class Cat{ private int catNumber; Cat(int i){catNumber=i;} void print(){ System.out.println("Cat#"+catNumber);} } public class Dog{ private int dogNumber; Dog(int i){dogNumber=i;} void print(){ System.out.println("Dog#"+dogNumber);} } import java.util.* ; public class CatsAndDogs{ public static void main(String[] args){ ArrayList cats=new ArrayList(); for (int i=0;i<7;i++) cats.add(new Cat(i)); cats.add(new Dog(7)); for(int i=0;i<cats.size();i++) ((Cat)cats.get(i)).print(); } } |
2.Re:新手 一段代码编译错误求助 [Re: zxhhero] | Copy to clipboard |
Posted by: helloworld Posted on: 2004-10-19 21:05 把上面的两个类,移到程序尾巴上. |
3.Re:新手 一段代码编译错误求助 [Re: zxhhero] | Copy to clipboard |
Posted by: zxhhero Posted on: 2004-10-19 21:34 楼上的大哥,好像不行! |
4.Re:新手 一段代码编译错误求助 [Re: helloworld] | Copy to clipboard |
Posted by: why Posted on: 2004-10-19 22:43 helloworld wrote: They are public classes! One needs to have 3 java files: Cat.java, Dog.java, and CatsAndDogs.java OR make Cat and Dog non-public and 把上面的两个类,移到程序尾巴上 |
5.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: zxhhero] | Copy to clipboard |
Posted by: yclc Posted on: 2004-10-20 10:52 3个人都要当主人,当然会吵架了 |
6.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: zxhhero] | Copy to clipboard |
Posted by: kulatasana Posted on: 2004-10-20 23:14 把上面的Dog和Cat类移到CatsAndAodgs类里做内部类就好 |
7.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: zxhhero] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-21 09:50 ((Cat)cats.get).print();这一句猫狗不分,强把狗变成猫,肯定有问题。 你应该把Cat和Dog继承同一个类或实现同一个接口,比如: public interface A{ public void print(); } 然后改:((Cat)cats.get).print();到((A)cats.get).print(); |
8.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: zxhhero] | Copy to clipboard |
Posted by: wddodo Posted on: 2004-10-21 10:43 楼上说的有道理。 |
9.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: zxhhero] | Copy to clipboard |
Posted by: eric_dasini Posted on: 2004-10-21 17:27 把 Cat 和 Dog class前面的 public 去掉! 每个java源文件中只能有一个class 被声明为public的!!这个class 的名字要和文件名同名!!! |
10.Re:新手 一段代码编译错误求助 ('class' or 'interface' expected) [Re: eric_dasini] | Copy to clipboard |
Posted by: kavinwang Posted on: 2004-10-21 18:46 eric_dasini wrote: 子类也可以声明为public的 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |