- 论坛徽章:
- 0
|
关于java中的类
1、例程如下所示
class TestClass
{
double depth;
}
class TestClassDemo
{
public static void main(String args[]){
TestClass myclass = new TestClass();
TestClass yourclass = new TestClass();
TestClass ourclass = myclass;
myclass.depth = 1;
yourclass.depth = 2;
ourclass.depth = 3;
System.out.println("volume is:"+myclass.depth);
System.out.println("volume is:"+yourclass.depth);
System.out.println("volume is:"+ourclass.depth);
}
}
2、java中的类
java中的类类似于c中的指针唯一的区别是你不能像c中那样去操作它,new运算符在运行期间为对象分配内存,因此运行一次new,就分配一块内存区域,所以对象myclass和ourclass使用的是同一内存区域,myclass和yourclass使用的是两块不同的内存区域,所以输出的结果为3,2,3。(即myclass和ourclass中存的是相同的内存地址,yourclass中存的内存地址和他们是不一样的)
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6574/showart_36649.html |
|