//Box.java
public class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
//=============================================================================
//ppp.java
public class ppp {public static void main(String[] args) {
//声名对像的一种方法:
Box mybox1 = new Box();
//声名对像的另一种方法:
Box mybox2;
//本句执行后,mybox2包含的值为空,表示它没有引用对像。这时任何引用mybox2的尝试都将导致一个编译错误。
//如
//mybox2.setDim(10,10, 10);
//System.out.println(mybox2.volume());
mybox2=new Box();
//本句执行后,创建了一个实际的对象,并把对于它的引用赋给mybox2.
mybox2.setDim(10,10, 10);
System.out.println(mybox2.volume());
//--------------------------------------------------------------------
//数组的一种声名方式
int days1[]=new int[5];
days1[0]=0;
//数组的别一种声名方式
int days2[];
//
//days2[1]=1;
days2=new int[5];
days2[1]=1;
//数组的另一种声名方式(只有这要种方式不用new)
int days3[]={0,1,2,3,4};
//---------------
//int days4[];
//days4[]={1,2,3};
//这样是不可以的。
//--------------- 如果不用new,就必须要在声名同时给值初始化
int days5[];
days5=new int[3];
//days5[ ]={1,2,3}; 这样不行
//days5[3]={1,2,3}; 这样同样也是不行滴
}
}