书签 分享 收藏 举报 版权申诉 / 26
上传文档赚钱

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

  • 上传人(卖家):晟晟文业
  • 文档编号:3795678
  • 上传时间:2022-10-13
  • 格式:PPT
  • 页数:26
  • 大小:137.02KB
  • 【下载声明】
    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

    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

    展开阅读全文
    提示  163文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:《JAVA语言程序设计》第3章课件.ppt
    链接地址:https://www.163wenku.com/p-3795678.html

    Copyright@ 2017-2037 Www.163WenKu.Com  网站版权所有  |  资源地图   
    IPC备案号:蜀ICP备2021032737号  | 川公网安备 51099002000191号


    侵权投诉QQ:3464097650  资料上传QQ:3464097650
       


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

    163文库