标题:PUBLIC问题?
只看楼主
飞跃无限
Rank: 1
等 级:新手上路
威 望:1
帖 子:180
专家分:0
注 册:2006-4-16
 问题点数:0 回复次数:2 
PUBLIC问题?

import java.io.*;
public abstract class TuXing
{
public double a;
public void setA(double i)
{
a=i;
}
public abstract double area();
}
public class ZhengFangXing extends TuXing
{
public double area()
{
return a*a;
}
}
public class ChangFangXing extends TuXing
{
public double b;
public void setB(double i)
{
b=i;
}
public double area()
{
return a*b;
}
}
public class SanJiaoXing extends TuXing
{
public double b;
public double c;
public void setB(double i)
{
b=i;
}
public void setC(double j)
{
c=j;
}
public double area()
{
double p=(a+b+c)/2.0;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}


public class Test11
{
public static void main(String args[])
{
ZhengFangXing zheng=new ZhengFangXing();
ChangFangXing chang=new ChangFangXing();
SanJiaoXing san=new SanJiaoXing();
if(args.length==0)
{
System.out.println("你在命令行中没有输入参数!");
// System.exit();
}
if(args.length==1)
{
zheng.setA(Double.parseDouble(args[0]));
System.out.println("正方形的面积是:"+zheng.area());
}
if(args.length==2)
{
chang.setA(Double.parseDouble(args[0]));
chang.setB(Double.parseDouble(args[1]));
System.out.println("长方形的面积是:"+chang.area());
}
if(args.length>=3)
{
san.setA(Double.parseDouble(args[0]));
san.setB(Double.parseDouble(args[1]));
san.setC(Double.parseDouble(args[2]));
System.out.println("三角形的面积是:"+san.area());
}
}
}


编译后怎么有这样的错误啊?
C:\>javac Test11.java
Test11.java:2: 类 TuXing 是公共的,应在名为 TuXing.java 的文件中声明
public abstract class TuXing
^
Test11.java:11: 类 ZhengFangXing 是公共的,应在名为 ZhengFangXing.java 的文件中
声明
public class ZhengFangXing extends TuXing
^
Test11.java:18: 类 ChangFangXing 是公共的,应在名为 ChangFangXing.java 的文件中
声明
public class ChangFangXing extends TuXing
^
Test11.java:30: 类 SanJiaoXing 是公共的,应在名为 SanJiaoXing.java 的文件中声明
public class SanJiaoXing extends TuXing
^
4 错误

然后我改成:
import java.io.*;
abstract class TuXing
{
public double a;
public void setA(double i)
{
a=i;
}
public abstract double area();
}
class ZhengFangXing extends TuXing
{
public double area()
{
return a*a;
}
}
class ChangFangXing extends TuXing
{
public double b;
public void setB(double i)
{
b=i;
}
public double area()
{
return a*b;
}
}
class SanJiaoXing extends TuXing
{
public double b;
public double c;
public void setB(double i)
{
b=i;
}
public void setC(double j)
{
c=j;
}
public double area()
{
double p=(a+b+c)/2.0;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}


public class Test11
{
public static void main(String args[])
{
ZhengFangXing zheng=new ZhengFangXing();
ChangFangXing chang=new ChangFangXing();
SanJiaoXing san=new SanJiaoXing();
if(args.length==0)
{
System.out.println("你在命令行中没有输入参数!");
// System.exit();
}
if(args.length==1)
{
zheng.setA(Double.parseDouble(args[0]));
System.out.println("正方形的面积是:"+zheng.area());
}
if(args.length==2)
{
chang.setA(Double.parseDouble(args[0]));
chang.setB(Double.parseDouble(args[1]));
System.out.println("长方形的面积是:"+chang.area());
}
if(args.length>=3)
{
san.setA(Double.parseDouble(args[0]));
san.setB(Double.parseDouble(args[1]));
san.setC(Double.parseDouble(args[2]));
System.out.println("三角形的面积是:"+san.area());
}
}
}
(把前面的几个public去了后就行了)
乍啦?
可能是很低级的问题吧?

搜索更多相关主题的帖子: PUBLIC 
2006-11-15 12:39
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 
这是JAVA里面的规定
一个JAVA文件里只能有一个public 类

可惜不是你,陪我到最后
2006-11-15 12:54
飞跃无限
Rank: 1
等 级:新手上路
威 望:1
帖 子:180
专家分:0
注 册:2006-4-16
得分:0 

吞下了。谢了老千。

2006-11-15 13:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-103126-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.270147 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved