LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1009|回复: 5

java对包的识别是不是与目录有关?

[复制链接]
发表于 2005-10-17 22:29:48 | 显示全部楼层 |阅读模式
我生成了一个Rectangle类,并做为一单独文件保存并编译为CLASS文件。
如下:
class Rectangle {
  double width,length;

  public Rectangle(double dWidth,double dLength) {
    width=dWidth;
    length=dLength;
  } //the constructor of class Rectangle

  public double area() {
    double a;
    a=length*width;
    return a;
  }
} //end of class Rectangle
又生成了一个测试的程序:
class JLab0401_01 {
  public static void main(String[] args) {
    Rectangle myRect=new Rectangle(10.0,20.0);
    double theArea;

    System.out.println("The width of myRect is"+myRect.width);    //Q1
    System.out.println("The length of my Rect is"+myRect.length); //Q2

    theArea=myRect.area();
    System.out.println("My rectangle has area"+theArea);
  }
} //end of class JLab0401
令我不解的话,为什么Q2,Q1能打印出正确的数据来?
而width,length不是仅具有包访问权限吗?是不是因为在当前目录下所互相依赖的文件就是在一包内,就如上边的两个文件?
谢谢大家。
发表于 2005-10-17 23:26:04 | 显示全部楼层
恩,同一个目录下是属于同一个包
回复 支持 反对

使用道具 举报

发表于 2005-10-18 14:36:04 | 显示全部楼层
默认没有包的话,,同一个路径下的都属于一个包。:)。
回复 支持 反对

使用道具 举报

发表于 2005-10-19 05:59:23 | 显示全部楼层
同一个类     同一个包   不同包的子类   不同包的非子类

private       yes

default       yes              yes

protected   yes              yes            yes
   
public          yes               yes           yes                      yes


你程序里的width和height就属于default(没有定义为private, protected或public),然后class JLab0401_0和Rectangle在同一个目录中(default package),所以可以访问。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-19 09:04:59 | 显示全部楼层
嗯,谢谢三位大哥的回复。
回复 支持 反对

使用道具 举报

发表于 2005-11-3 12:59:27 | 显示全部楼层
especially depends on the classpath env ;)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表