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

上传人:公**** 文档编号:570224702 上传时间:2024-08-02 格式:PPT 页数:26 大小:286.50KB
返回 下载 相关 举报
《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页
点击查看更多>>
资源描述

《《JAVA语言程序设计》第3章.ppt》由会员分享,可在线阅读,更多相关《《JAVA语言程序设计》第3章.ppt(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(Stri

2、ng args) Square 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,

3、 int y) a = x; h = y;Square(Square s) a = s.width(); 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()

4、; 5n再谈构造方法对象的创建 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); 运行时系统自动赋予一个空构造方法,仅仅

5、当该类没定义构造方法的情况下6对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问7对象的使用n通过对象引用对象的成员变量和成员方法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 =

6、 30;q1.h = 40;目的相同第一方式更安全、更面向对象(数据封装)避免直接操纵变量8对象的使用n引用对象的变量n格式: 对象名.变量名n引用对象的方法n格式: 对象名.方法名n例1nVector v = new Vector();nv.addElement(“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)

7、 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 garba

8、ge collection. 11对象的释放n垃圾搜集器(Garbage Collector)n周期性地释放不再被引用的对象,自动完成n手动完成,nSystem.gc(); (java.lang.System中)npublic static void gc() - Runs the garbage collector. 12对象和类(续)n对象的创建n对象的使用n对象的释放n对象的访问13对象的访问n访问对象的私有(private)成员n通过定义一个公共方法来实现class Student private String name;private String id;Student(String

9、 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对象的访问n对象作为方法的参数访问权限修饰符 方法返回类型 方法名(参数) throws 异常名方法体;n参数: 类型 变量名, n类型: 基本数据类型/复合类型(对象)n参数的传递nPass by v

10、alue15n例 对象用作方法的参数对象的访问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); System.out.println(“s点的坐标:” +s.getX()+“,”+s.getY();class Spot private int x, y;Spot (int u, int v) setX(u); s

11、etY(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,816n例 对象用作方法的参数对象的访问class Test public static void main(String args) Spot s = new Spot(2,

12、 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 y1) y=y1; int getX() return x; int getY() return y; static void mo

13、ve(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); System.out.println(“s点的坐标:” + s.getX()+“,”+s.g

14、etY();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对象作为方法的返回值访问权限修饰符 方法返回类型 方法名(参数) throws 异常名方法体;n返回类型

15、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

16、 s2 = 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(

17、double x1) x=x1;void setY(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 pub

18、lic 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对象的访问n对象数组class Student private String name; private String id;Student (String s1, String s2) name = s1; id = s2; String getName() ret

19、urn 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对象的访问n对象作为另一个对象的成员变量n一个对象中包含另一个对象,组合关系class MobilePhone private String type;

20、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 cur

21、rentTimeMillis()the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 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)

22、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对象的访问n关键词 thisnthis指当前对象n应用2: 对同一个对象执行多次方法调用class Leaf int i = 0;Leaf increment() i+;return this;void print() System.ou

23、t.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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号