类与对象的进一步研究
上一篇 / 下一篇 2013-11-26 22:51:48 / 个人分类:java
类与对象的进一步研究51Testing软件测试网-u$t"n`(T|`
0ZS r p0[$bX0
掌握JAVA中的内存划分
*[(k7~p[:_3H0初窥JAVA引用传递
A&_Iqn0垃圾的产生分析
,E:gf)Z N051Testing软件测试网#^jGt.YFW
P2hvKy6W0
class Person{ //人中有姓名和年龄两个属性 String name ; //表示姓名 int age ; //表示年龄 public void tell(){ //取得信息
System.out.println("姓名 :" + name + "; 年龄:" + age) ; } } ; public class ClassDemo02{ public static void
main(String args[]){ Person per = new
Person() ; //创建并实例化对象 Person per = null
; //声明对象 per = new Person()
; //实例化对象 } } ; 51Testing软件测试网i'G `}!S-F:M |
51Testing软件测试网]r@E$M
声明对象 Person per,栈内存中声明对象,
实例化对象:new Person(), 在堆中开辟内存,所有的内容都是默认值;
调用属性:对象.属性
调用方法 :对象.方法
#U Y!P4R6Qg0
51Testing软件测试网~ H&Z8A8W&O7R2Hc
class Person{ //人中有姓名和年龄两个属性 String name ; //表示姓名 int age ; //表示年龄 public void tell(){ //取得信息
System.out.println("姓名 :" + name + "; 年龄:" + age) ; } } ; public class ClassDemo03{ public static void
main(String args[]){ Person per = null
; //声明对象 per = new Person()
; //实例化对象 per.name = "张三" ; //为姓名赋值 per.age = 30 ; per.tell() ; } } ; 51Testing软件测试网)Vm\G/I;P |
输出如下:51Testing软件测试网V!O X n[
姓名 :张三; 年龄:30 |
51Testing软件测试网U:L[[.v&_
X6Z3MkP0
实例化多个对象
K1o)A.T8d$?0F0
class Person{ //人中有姓名和年龄两个属性 String name ; //表示姓名 int age ; //表示年龄 public void tell(){ //取得信息
System.out.println("姓名 :" + name + "; 年龄:" + age) ; } } ; public class ClassDemo04{ public static void
main(String args[]){ Person per1 = null
; //声明per1对象 Person per2 = null
; //声明per2对象 per1 = new Person() ;
//实例化per1对象 per2 = new Person()
; //实例化per2对象 per1.name = "张三" ; //设置per1 中的name属性内容 per1.age = 30 ; //设置per1 中的age属性内容 per2.name = "李四" ; per2.age = 20 ;
System.out.println("per1 对象中的内容-->") ; per1.tell() ;
System.out.println("per2 对象中的内容-->") ; per2.tell() ; } } ; 5hK ?Qtcw0 |
输出内容:
4s} o.NJE&K#^0
per1 对象中的内容-->姓名 :张三; 年龄:30 per2 对象中的内容-->姓名 :李四; 年龄:20 |
51Testing软件测试网G5a]d'oPW3U
x;UFR{3k#^ w0
引用51Testing软件测试网%hQU8V%S;jX
class Person{ //人中有姓名和年龄两个属性 String name ; //表示姓名 int age ; //表示年龄 public void tell(){ //取得信息
System.out.println("姓名 :" + name + "; 年龄:" + age) ; } } ; public class ClassDemo05{ public static void
main(String args[]){ Person per1 = null
; //声明per1对象 Person per2 = null
; //声明per2对象 per1 = new Person() ;
//只实例化per1对象 per2 = per1 ; //把per1中的堆内存空间使用权给per2 per1.name = "张三" ; //设置per1 中的name属性内容 per1.age = 30 ; //设置per1 中的age属性内容 // per2.name = "李四" ; per2.age = 20 ;
System.out.print("per1 对象中的内容-->") ; per1.tell() ; //调用类中的方法
System.out.print("per2 对象中的内容-->") ; per2.tell() ; } } ; dbi[};Qr0 |
输出内容51Testing软件测试网bJ T'b+]^C'k
per1 对象中的内容-->姓名 :张三; 年龄:20 per2 对象中的内容-->姓名 :张三; 年龄:20 |
51Testing软件测试网D/| \#_O;^
7fBgAc4[i0
class Person{ //人中有姓名和年龄两个属性 String name ; //表示姓名 int age ; //表示年龄 public void tell(){ //取得信息
System.out.println("姓名 :" + name + "; 年龄:" + age) ; } } ; public class ClassDemo06{ public static void
main(String args[]){ Person per1 = null
; //声明per1对象 Person per2 = null
; //声明per2对象 per2 = per1 ; //把per1中的堆内存空间使用权给per2 per1.name = "张三" ; //设置per1 中的name属性内容 per1.age = 30 ; //设置per1 中的age属性内容 per2.name = "李四" ; per2.age = 20 ; per2 =per1 ;
System.out.print("per1 对象中的内容-->") ; per1.tell() ; //调用类中的方法
System.out.print("per2 对象中的内容-->") ; per2.tell() ; } } ; 51Testing软件测试网?mf ~ g0o5w,dfu |
51Testing软件测试网N)C0Zec)B7g
输出内容:51Testing软件测试网-f:g5^`9Iz9o7`
per1 对象中的内容-->姓名 :张三; 年龄:20 per2 对象中的内容-->姓名 :张三; 年龄:20 51Testing软件测试网XZ7`S)pV |
51Testing软件测试网 ]3E ?ANN
51Testing软件测试网e ASW} x\0vTAG:
北漂一族,80后,计算机专业,从事SIP相关软件测试3年,热爱生活,崇尚运动。 爱看WWE、公开课。爱钻“牛角尖”,这就是我:luorivr!!!!!
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 345706
- 日志数: 96
- 图片数: 1
- 建立时间: 2012-12-27
- 更新时间: 2014-05-03