类的问题

yjws 2005-11-16 06:57:42
怎么样才能把主类的数据传送给其它类呀,最好给个实例
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mofengtt 2005-11-17
  • 打赏
  • 举报
回复
下面这个例子中,从main类中,把命令行参数传给了StatChar类,是不是你说的传递呢??


/*************************************************************
*编写一个程序,用一个数组来记录一个字符串中每个字母出现 *
*的次数(忽略大小写),并输出结果,该字符串为运行时由命 *
*令行参数给出! *
*************************************************************/
class StatCharInStr
{
public String str;

public StatCharInStr(String s)
{
str=s;
}

public void stat()
{
str=str.trim().toLowerCase();
//统计时,不区分大小写,去掉空格后,统一转化成小写的
char eachChar[]=new char[str.length()];
//keep each unique char in command line
int eachCharTimes[]=new int[str.length()];
//keep times of each unique char in command line
eachChar=str.toCharArray();
int index=0;
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(isExist(eachChar,ch)>=0) //重复出现的字符
eachCharTimes[isExist(eachChar,ch)]++;
else
{ //首次出现的字符
eachChar[index]=ch;
eachCharTimes[index]++;
index++;
}
}
System.out.println("String "+str+" statistic as follow:");
for(int i=0;i<eachChar.length;i++)
{
if(eachCharTimes[i]!=0)
System.out.println(eachChar[i]+" "+eachCharTimes[i]);
}

}

public int isExist(char charArray[],char ch)
{
for(int i=0;i<charArray.length;i++)
{
if(charArray[i]==ch)
return i;
}
return (-1);
}

/*public int numOfNotZoneElem(int charArr[])
{
int count=0;
for(int i=0;i<charArr.length;i++)
if(charArr[i]!=0)
count++;
return count;
}*/
}



class ex_5_6_2
{
public static void main(String args[])
{
StatCharInStr statChar=new StatCharInStr(args[0]);
statChar.stat();
}
}


不好意思的是,这个程序,我经过调试,并是不很完善,但是在jdk 1.4上,编译通过,基本的功能可以实现!!

望谈论,以完善之!!

期待中.............
f_acme 2005-11-16
  • 打赏
  • 举报
回复
参数传递,可行否?
Mr_IT 2005-11-16
  • 打赏
  • 举报
回复
这方法比较多,要看你打算处理什么问题了---

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧