《JAVA语言程序设计》第3章课件.ppt

上传人(卖家):晟晟文业 文档编号:3795678 上传时间:2022-10-13 格式:PPT 页数:26 大小:137.02KB
下载 相关 举报
《JAVA语言程序设计》第3章课件.ppt_第1页
第1页 / 共26页
《JAVA语言程序设计》第3章课件.ppt_第2页
第2页 / 共26页
《JAVA语言程序设计》第3章课件.ppt_第3页
第3页 / 共26页
《JAVA语言程序设计》第3章课件.ppt_第4页
第4页 / 共26页
《JAVA语言程序设计》第3章课件.ppt_第5页
第5页 / 共26页
点击查看更多>>
资源描述

1、1对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问2对象的创建n对象成员(变量和方法)n静态(static)成员:属于类n实例成员:属于对象n创建对象/实例化对象new 例1:Apple a=new Apple();(创建对象)例2:Apple a;(对象的说明)a=new Apple();(实例化对象)1.对象的实例化通过构造方法(constructor)来实现2.构造方法的名字与类名相同3.构造方法没有返回值4.构造方法可以有多个,构成方法的重载(overload)3n例:对象的实例化和初始化 public static void main(String args)Squar

2、e s1=new Square();Square s2=new Square(20,50);Square s3=new Square(s1);System.out.println(s1.width()+“”+s1.height();System.out.println(s2.width()+“”+s2.height();System.out.println(s3.width()+“”+s3.height();class Square int a,h;Square()a=10;h=20;Square(int x,int y)a=x;h=y;Square(Square s)a=s.width();

3、h=s.height();int width()return a;int height()return h;计算结果:10 2020 5010 20对象的创建4n默认构造方法例 class Apple int color;Apple a=new Apple();n对象实例的判断:null例 Apple a;if(a=null)System.out.println(“Day dream”);对象的创建运行时系统自动赋予一个空构造函数如 public Apple();5n再谈构造方法对象的创建 class MyTest MyTest(boolean b)public static void mai

4、n(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对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问7对象的使用n通过对象引用对象的成员变量和成员方法class Square int a,

5、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对象的使用n引用对象的变量n格式:对象名.变量名n引用对象的方法n格式:对象名.方法名n例1nVector v=new Vector();nv.addEleme

6、nt(“s1”);n例2nint a=1,2,3,4,5;nint size=a.length;n例3nSystem.out.println();9对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问10对象的释放n将对象从内存中清除n内存的管理(枯燥、容易出错)n垃圾回收(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

7、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对象的释放n垃圾搜集器(Garbage Collector)n周期性地释放不再被引用的对象,自动完成n手动完成,nSystem.gc();(java.lang.System中)npublic static void gc()-Runs the garbage

8、 collector.12对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问13对象的访问n访问对象的私有(private)成员n通过定义一个公共方法来实现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.setNa

9、me(“李晓”);s=st.getName();14对象的访问n对象作为方法的参数访问权限修饰符 方法返回类型 方法名(参数)throws 异常名方法体;n参数:类型 变量名,n类型:基本数据类型/复合类型(对象)n参数的传递nPass by value15n例 对象用作方法的参数对象的访问class Test public static void main(String args)Spot s=new Spot(2,3);System.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();Trans ts=new Trans();ts.move(s,4,5);

10、System.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();class Spot private int x,y;Spot(int u,int v)setX(u);setY(v);void setX(int x1)x=x1;void setY(int y1)y=y1;int getX()return x;int getY()return y;class Trans void move(Spot p,int h,int k)p.setX(p.getX()+h);p.setY(p.getY()+k);D:java Tests点的坐标:2,3s点的坐标:6,8

11、16n例 对象用作方法的参数对象的访问class Test public static void main(String args)Spot s=new Spot(2,3);System.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();Spot.move(s,4,5);System.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();class Spot private int x,y;Spot(int u,int v)setX(u);setY(v);void setX(int x1)x=x1;void setY(int

12、y1)y=y1;int getX()return x;int getY()return y;static void move(Spot p,int h,int k)p.setX(p.getX()+h);p.setY(p.getY()+k);D:java Tests点的坐标:2,3s点的坐标:6,817n例 对象用作方法的参数对象的访问class Test public static void main(String args)Spot s=new Spot(2,3);System.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();s.move(4,5);Sy

13、stem.out.println(“s点的坐标:”+s.getX()+“,”+s.getY();class Spot private int x,y;Spot(int u,int v)setX(u);setY(v);void setX(int x1)x=x1;void setY(int y1)y=y1;int getX()return x;int getY()return y;void move(int h,int k)x=x+h;y=y+k;D:java Tests点的坐标:2,3s点的坐标:6,818对象的访问n对象的访问n对象作为方法的返回值访问权限修饰符 方法返回类型 方法名(参数)t

14、hrows 异常名方法体;n返回类型n有返回值:基本数据类型/复合类型(对象)n无返回值:void19对象的访问n对象作为方法的返回值n例:求两点坐标之间的中点坐标n思路:(x1,y1)和(x2,y2)(x,y)nx=(x1+x2)/2,y=(y1+y2)/2nSpot s1=new Spot(2,3);nSpot s2=new Spot(4,5);nSpot s =s1.midSpot(s2);20n例 对象用作方法的返回值对象的访问class Test public static void main(String args)Spot s1=new Spot(3.0,5.0);Spot s2

15、=new Spot(6.0,8.0);System.out.println(“s1点的坐标:”+s1.getX()+“,”+s1.getY();System.out.println(“s2点的坐标:”+s2.getX()+“,”+s2.getY();Spot s=s1.midSpot(s2);System.out.println(“中点的坐标:”+s.getX()+“,”+s.getY();class Spot private double x,y;Spot(double u,double v)setX(u);setY(v);void setX(double x1)x=x1;void setY

16、(double y1)y=y1;double getX()return x;double getY()return y;Spot midSpot(Spot s)double midX=(x+s.getX()/2;double midY=(y+s.getY()/2;return new Spot(midX,midY);D:java Tests1点的坐标:3.0,5.0s2点的坐标:6.0,8.0中点的坐标:4.5,6.521对象的访问n数组:类型相同的一列元素n作为一个对象看待public class ArrayDemo public static void main(String args)i

17、nt anArray =new int10;for(int i=0;i anArray.length;i+)anArrayi=i;System.out.print(anArrayi+);System.out.println();22对象的访问n对象数组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.ou

18、t.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对象的访问n对象作为另一个对象的成员变量n一个对象中包含另一个对象,组合关系class MobilePhone private String type;private Watch w;MobilePhone(String s)type=s;void setWatch(Watch a)w=a;long getTime()retur

19、n 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

20、UTC24对象的访问n关键词 thisnthis指当前对象n应用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

21、(int a,int b)int t;if(x y)t=x;x=y;y=t;25对象的访问n关键词 thisnthis指当前对象n应用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=326对象的访问n关键词 thisnthis指当前对象n应用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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(《JAVA语言程序设计》第3章课件.ppt)为本站会员(晟晟文业)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|