《JAVA语言程序设计》第3章课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《《JAVA语言程序设计》第3章课件.ppt》由用户(晟晟文业)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA语言程序设计 JAVA 语言程序设计 课件
- 资源描述:
-
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
展开阅读全文