菜鸟学java百问(1)

Tanglong1982 2002-11-07 10:54:48
各位高手:
小弟刚学java不久,在书上有如下一段程序:
package lee;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class Flower{
private int petalCount=0;
private String s = new String ("null");
Flower (int petals) {
petalCount = petals;
System.out.println(
"Coustructor w/ int arg only,petalCount="+petalCount
);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only,s=" + ss);
s=ss;
}
Flower (String s, int petals) {
this(petals);
this.s=s;
System.out.println("String & int args");
}
Flower() {
this("hi",47);
System.out.println(
"deault counstructor (no args)");
}
void print() {
System.out.println(
"petalCount=" + petalCount + "s=" + s);
}
public static void main(Sting[] args) {
Flower x=new Flower();
x.print();


我用jbuilder6 run project 结果出了一下错误
"Untitled1.java": Error #: 475 : class Flower is public; must be declared in a file named Flower.java at line 12, column 8
"Untitled1.java": Error #: 300 : class Sting not found in class lee.Flower at line 40, column 27
这个程序是为了说明 this 关键字的构建器中调用构建器 内容
但是,对于这个问题我还不很清楚, 希望对此能为我详细说明一下。
1 哪里体现构建器调用??
2 怎么运行不了程序????
3 关于 this 关键字 除了这里说的,还有什么相关重要知识??

希望对于这个问题能给小弟一个全面详细的解说!!
...全文
34 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallfox 2002-11-08
  • 打赏
  • 举报
回复
public class 必须和文件名同名

而且你的sting打错了,这是很不应该的

关于this 补充一点,当传入参数和private var不同名时,可以省略this
反之不能
redkey 2002-11-08
  • 打赏
  • 举报
回复
在类文件中使用public 来定义的类必须与文件名相同。
coolskeeter 2002-11-08
  • 打赏
  • 举报
回复
1.
"Untitled1.java": Error #: 475 : class Flower is public; must be declared in a file named Flower.java at line 12, column 8

这句话的意思是:在"Untitled1.java"文件中出错了,Flower类是public的,那么他所在的文件名必须是Flower.java.
在java中,一个.java文件中可以声明多个class,但是最多只能有一个public声明,而且这个文件的名字必须和这个public class同名。你这里的public类是Flower,但是文件名是Untitled1.java,所以出问题了。

2。

"Untitled1.java": Error #: 300 : class Sting not found in class lee.Flower at line 40, column 27
这句话的意思是:在“Untitled1.java"文件中出现了如下错误:Sting这个类在类lee.Flower中没有找到。后面的40,27是错误的位置。

这类“class *** not found”一般是没有import 这个类所在的包。但是你的问题还不是没有加入包,而是把“String”误写为“Sting”。这种问题应该在编译前就仔细查照以下。

3。关于构建器的调用:
你这个类里边有两个成员变量,有四个构造函数。
其中

Flower (int petals) {
petalCount = petals;
System.out.println(
"Coustructor w/ int arg only,petalCount="+petalCount
);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only,s=" + ss);
s=ss;
}

这两个分别是带一个参数的,并且给参数赋值,另外一个参数就是你定义的初始值了。

还有两个构造器:
Flower (String s, int petals) {
this(petals); //这里就调用了Flower(int petals)这个构造器。相当于先执行Flower(int petals),然后在执行下面的语句。
this.s=s;
System.out.println("String & int args");
}

同样的:

Flower() {
this("hi",47);
System.out.println(
"deault counstructor (no args)");
}

这个就是先执行Flower(String ss,int petals)这句,然后执行下面的。


4。程序不能运行?
那是因为你还没有编译通过,当改掉你的所有错误,编译通过后,就可以执行你的代码。
Flower x=new Flower();
x.print();

执行的结果是这样子的:
首先Flower x=new Flower();
这句调用Flower()构造器,而它内部由调用Flower("hi",47),而这个构造器由调用Flower(int petals).
所以首先输出:
Coustructor w/ int arg only,petalCount=47 //这是Flower(int)方法中的
接着输出:
String & int args//这是Flower(String,int)方法中的。
再输出:
deault counstructor (no args)//这才是Flower()方法中的。
最后输出:
petalCount=47s=hi
这是你的x.print()语句输出的。



HawaiiLeo 2002-11-08
  • 打赏
  • 举报
回复
this 指的是当前的类,可以用this 引用当前的类
parent 指的是当前类的父类
dongjh 2002-11-08
  • 打赏
  • 举报
回复
类名和文件名要一样
boycer 2002-11-07
  • 打赏
  • 举报
回复
先说说你的第一个错误:
Untitled1.java": Error #: 475 : class Flower is public; must be declared in a file named Flower.java at line 12, column 8
这是因为你的Flower类是public的,但是你的命名没有是Flower.java


"Untitled1.java": Error #: 300 : class Sting not found in class lee.Flower at line 40, column 27

public static void main(Sting[] args) {//这里的String错了!
Flower x=new Flower();
x.print();
ggzzkk 2002-11-07
  • 打赏
  • 举报
回复
.java文件名要和类名一样,是区分大小写的。

62,614

社区成员

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

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