免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1074 | 回复: 0

关于java中的类 [复制链接]

论坛徽章:
0
发表于 2005-07-19 23:44 |显示全部楼层
关于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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP