JAVA语言程序设计第3章2.ppt
1,对象和类(续),对象的创建对象的使用对象的释放对象的访问,2,对象的创建,对象成员(变量和方法)静态(static)成员:属于类实例成员:属于对象创建对象/实例化对象new 例1:Apple a=new Apple();(创建对象)例2:Apple a;(对象的说明)a=new Apple();(实例化对象)对象的实例化通过构造方法(constructor)来实现构造方法的名字与类名相同构造方法没有返回值构造方法可以有多个,构成方法的重载(overload),3,例:对象的实例化和初始化,计算结果:10 2020 5010 20,对象的创建,4,默认构造方法例 class Apple int color;Apple a=new Apple();对象实例的判断:null例 Apple a;if(a=null)System.out.println(“Day dream”);,对象的创建,运行时系统自动赋予一个空构造函数如 public Apple();,5,再谈构造方法,对象的创建,class MyTest MyTest(boolean b)public static void main(String args)/MyTest m1=new MyTest();MyTest m2=new MyTest(false);,class MyTest MyTest(boolean b)MyTest()public static void main(String args)MyTest m1=new MyTest();MyTest m2=new MyTest(false);,运行时系统自动赋予一个空构造方法,仅仅当该类没定义构造方法的情况下,6,对象和类(续),对象的创建对象的使用对象的释放对象的访问,7,对象的使用,通过对象引用对象的成员变量和成员方法,class Square int a,h;Square()a=10;h=20;Square(int x,int y)a=x;h=y;Square(Square r)a=r.width();h=r.height();int width()return a;int height()return h;void set(int x,int y)a=x;h=y;,q1.set(30,40);q1.a=30;q1.h=40;目的相同第一方式更安全、更面向对象(数据封装)避免直接操纵变量,8,对象的使用,引用对象的变量格式:对象名.变量名引用对象的方法格式:对象名.方法名例1Vector v=new Vector();v.addElement(“s1”);例2int a=1,2,3,4,5;int size=a.length;例3System.out.println();,9,对象和类(续),对象的创建对象的使用对象的释放对象的访问,10,对象的释放,将对象从内存中清除内存的管理(枯燥、容易出错)垃圾回收(Garbage Collection),The Java platform allows you to create as many objects as you want(limited,of course,by what your system can handle),and you dont have to worry about destroying them.The Java runtime environment deletes objects when it determines that they are no longer being used.This process is called garbage collection.,11,对象的释放,垃圾搜集器(Garbage Collector)周期性地释放不再被引用的对象,自动完成手动完成,System.gc();(java.lang.System中)public static void gc()-Runs the garbage collector.,12,对象和类(续),对象的创建对象的使用对象的释放对象的访问,13,对象的访问,访问对象的私有(private)成员通过定义一个公共方法来实现,class Student private String name;private String id;Student(String s1,String s2)name=s1;id=s2;String getName()return name;void setName(String s)name=s;,Student st=new Student(“李忠”,“001”);String s=st.getName();st.setName(“李晓”);s=st.getName();,14,对象的访问,对象作为方法的参数访问权限修饰符 方法返回类型 方法名(参数)throws 异常名方法体;参数:类型 变量名,类型:基本数据类型/复合类型(对象)参数的传递Pass by value,15,例 对象用作方法的参数,对象的访问,D:java Tests点的坐标:2,3s点的坐标:6,8,16,例 对象用作方法的参数,对象的访问,D:java Tests点的坐标:2,3s点的坐标:6,8,17,例 对象用作方法的参数,对象的访问,D:java Tests点的坐标:2,3s点的坐标:6,8,18,对象的访问,对象的访问对象作为方法的返回值访问权限修饰符 方法返回类型 方法名(参数)throws 异常名方法体;返回类型有返回值:基本数据类型/复合类型(对象)无返回值:void,19,对象的访问,对象作为方法的返回值例:求两点坐标之间的中点坐标思路:(x1,y1)和(x2,y2)(x,y)x=(x1+x2)/2,y=(y1+y2)/2Spot s1=new Spot(2,3);Spot s2=new Spot(4,5);Spot s=s1.midSpot(s2);,20,例 对象用作方法的返回值,对象的访问,D:java Tests1点的坐标:3.0,5.0s2点的坐标:6.0,8.0中点的坐标:4.5,6.5,21,对象的访问,数组:类型相同的一列元素作为一个对象看待,public class ArrayDemo public static void main(String args)int anArray=new int10;for(int i=0;i anArray.length;i+)anArrayi=i;System.out.print(anArrayi+);System.out.println();,22,对象的访问,对象数组,class Student private String name;private String id;Student(String s1,String s2)name=s1;id=s2;String getName()return name;void setName(String s)name=s;void display()System.out.println(name+“”+id);,Student st=new Student10;for(int i=0;i st.length;i+)sti=new Student();for(int i=0;i st.length;i+)sti.display();,23,对象的访问,对象作为另一个对象的成员变量一个对象中包含另一个对象,组合关系,class MobilePhone private String type;private Watch w;MobilePhone(String s)type=s;void setWatch(Watch a)w=a;long getTime()return w.getTime();,class Watch long getTime()return System.currentTimeMillis();,MobilePhone mp=new MobilePhone(“nokia”);Watch w=new Watch();mp.setWatch(w);long l=mp.getTime();,public static long currentTimeMillis()the difference,measured in milliseconds,between the current time and midnight,January 1,1970 UTC,24,对象的访问,关键词 thisthis指当前对象应用1:加强程序可读性(this可有可无),class Demo1 double x,y;Demo1(double i,double j)this.x=i;this.y=j;double ave()return(x+y)/2;public static void main(String args)Demo1 d=new Demo1(3,4);System.out.println(d.ave();,class Demo2 int x,y,z;Demo2(int a,int b)x=a;y=b;this.swap(a,b);void swap(int a,int b)int t;if(x y)t=x;x=y;y=t;,25,对象的访问,关键词 thisthis指当前对象应用2:对同一个对象执行多次方法调用,class Leaf int i=0;Leaf increment()i+;return this;void print()System.out.println(“i=”+i);public static void main(String args)Leaf x=new Leaf();x.increment().increment().increment().print();,D:java Leafi=3,26,对象的访问,关键词 thisthis指当前对象应用3:在一个构造函数中调用另一个构造函数,class Flower String name;int price;Flower()this(tulip,10);Flower(String s,int i)name=s;price=i;,void print()System.out.println(name+price);public static void main(String args)Flower f=new Flower();f.print();,D:java Flowertulip 10,