①整型初始化为0; 2实型初始化为0.0f、0.0d; ③逻辑型初始化为false; ④字符型初始化为’u0000'; ⑤类对象初始化为nul,表明引用不 指向任何内存地址。 人民邮电出版社 POSTS TELECOM PRESS
整型初始化为0; 实型初始化为0.0f、0.0d; 逻辑型初始化为false; 字符型初始化为’\u0000’; 类对象初始化为null,表明引用不 指向任何内存地址
例3.1定义类和使用类对象 public class TestBirthday public static void main(String args[){ Birthday obj1=new Birthday(); obj1.showO; Birthday obj2=new Birthday(1990,11,12); obj2.showO; 爹人民邮电出版社 OSTS TELECOM PRESS
例3.1 定义类和使用类对象 public class TestBirthday { public static void main(String args[]) { Birthday obj1=new Birthday(); obj1.show(); Birthday obj2=new Birthday(1990,11,12); obj2.show(); } }
class Birthday int year,month,day;∥分别表示年月日 Birthday(){ } Birthday(int year,int month,int day){ this.year=year;/this代表对象本身 this.month=month; this.day-day; void show(){ System.out.println("出生年月:"+year+"年“ +month+-"月"+day+"日"); 人民邮电出版社 POSTS TELECOM PRESS
class Birthday { int year, month, day; //分别表示年月日 Birthday( ) { } Birthday(int year, int month, int day) { this.year=year; //this代表对象本身 this.month=month; this.day=day; } void show(){ System.out.println("出生年月:"+year+"年“ +month+"月"+day+"日"); } }
2,声明其它美对象作为一个美的城 类的域(变量)可以是基本数据类型, 也可以是其他类的对象。 爹人民邮电出版社 POSTS TELECOM PRESS n
2.声明其它类对象作为一个类的域 类的域(变量)可以是基本数据类型, 也可以是其他类的对象
例3.2类对象作为类的域 //TestPerson.java public class TestPerson public static void main(String args[]){ Person obj-=new Person("张三",'男',new Birthday(1988,8,8),1.75) obj.showO; System.out.println("- 改变类中变量的取值 obj.name=-"王立"; obj.birth=new Birthday(1988,1,20); obj.birth.showO); obj.showO); 人民邮电出版社 POSTS TELECOM PRESS
例 3.2 类对象作为类的域 //TestPerson.java public class TestPerson { public static void main(String args[]) { Person obj=new Person("张三", '男', new Birthday(1988,8,8), 1.75); obj.show(); System.out.println("========改变类中变量的取值=========="); obj.name="王立"; obj.birth=new Birthday(1988,1,20); obj.birth.show(); obj.show(); } }