A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© daveyang 中级黑马   /  2015-3-22 12:05  /  789 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

此程序是为了探测main函数是否可以被作为方法调用,但是出错了。
我在一个帖子上看到,这是可行的,不知道我的错误在哪里,请指正
class BeCalled{
public static void main(String args[]){
  for(int i=0;i<agrs.length;i++)
   System.out.println(arg[i]);
}
}
class Caller{
public static void main(String args[]){
  Becalled test=new BeCalled();
  String init[] = {"11","22","33"};
  test.main(init[]);
}
}
出错信息:
mainreference.java:11: 错误: 需要 '.class'
                test.main(init[]);
                                ^
1 个错误

4 个回复

倒序浏览
class BeCalled{
public static void main(String args[]){
  for(int i=0;i<args.length;i++)
   System.out.println(args[i]);
}
}
class Caller{
public static void main(String args[]){
  //BeCalled test=new BeCalled();
  String init[] = {"11","22","33"};
  BeCalled.main(init);
}
}
这样就好了,你可以对比一下,看看你的代码哪里有问题,我是改的你代码

点评

编译通过,我原来也是你这样写的,不过在参数传递时用的是int[],看来参数传递这块还是不太清楚,受教了  发表于 2015-3-22 12:30
回复 使用道具 举报
哥们,你第一个类里,一个单词写了三种写法。。。。囧。。。。。正确答案看一楼的

点评

谢谢  发表于 2015-3-22 13:00
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马