山东省发放《医疗器械经营企业许可证 (2)ppt课件_第1页
山东省发放《医疗器械经营企业许可证 (2)ppt课件_第2页
山东省发放《医疗器械经营企业许可证 (2)ppt课件_第3页
山东省发放《医疗器械经营企业许可证 (2)ppt课件_第4页
山东省发放《医疗器械经营企业许可证 (2)ppt课件_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Copyright 2008 by Pearson EducationBuilding Java ProgramsChapter 8Lecture 8-2: Constructors and Encapsulationreading: 8.4 - 8.5self-checks: #10-17exercises: #9, 11, 14, 16n江西不孕不育医院 srsgyyCopyright 2008 by Pearson Education2Object initialization: constructorsreading: 8.4self-check: #10-12exercises: #

2、9, 11, 14, 16Copyright 2008 by Pearson Education3Initializing objectsCurrently it takes 3 lines to create a Point and initialize it:Point p = new Point();p.x = 3;p.y = 8; / tediousWed rather pass the fields initial values as parameters:Point p = new Point(3, 8); / better!We are able to this with mos

3、t types of objects in Java.Copyright 2008 by Pearson Education4Constructorsconstructor: Initializes the state of new objects.public type(parameters) statements;runs when the client uses the new keyworddoes not specify a return type;it implicitly returns the new object being createdIf a class has no

4、constructor, Java gives it a default constructor with no parameters that sets all fields to 0.Copyright 2008 by Pearson Education5Constructor examplepublic class Point int x; int y; / Constructs a Point at the given x/y location. public Point(int initialX, int initialY) x = initialX; y = initialY; p

5、ublic void translate(int dx, int dy) x += dx; y += dy; Copyright 2008 by Pearson Education6Tracing a constructor callWhat happens when the following call is made?Point p1 = new Point(7, 2);public Point(int initialX, int initialY) x = initialX; y = initialY;public void translate(int dx, int dy) x +=

6、dx; y += dy;x yp1Copyright 2008 by Pearson Education7Client code, version 3public class PointMain3 public static void main(String args) / create two Point objects Point p1 = new Point(5, 2); Point p2 = new Point(4, 3); / print each point System.out.println(p1: ( + p1.x + , + p1.y + ); System.out.pri

7、ntln(p2: ( + p2.x + , + p2.y + ); / move p2 and then print it again p2.translate(2, 4); System.out.println(p2: ( + p2.x + , + p2.y + ); OUTPUT:p1: (5, 2)p2: (4, 3)p2: (6, 7)Copyright 2008 by Pearson Education8Common constructor bugsAccidentally writing a return type such as void: public void Point(i

8、nt initialX, int initialY) x = initialX; y = initialY; This is not a constructor at all, but a method!Storing into local variables instead of fields (shadowing): public Point(int initialX, int initialY) int x = initialX; int y = initialY; This declares local variables with the same name as the field

9、s, rather than storing values into the fields. The fields remain 0.Copyright 2008 by Pearson Education9Multiple constructorsA class can have multiple constructors.Each one must accept a unique set of parameters.Write a constructor for Point objects that accepts no parameters and initializes the poin

10、t to the origin, (0, 0)./ Constructs a new point at (0, 0).public Point() x = 0; y = 0;Copyright 2008 by Pearson Education10Encapsulationreading: 8.5 - 8.6self-check: #13-17exercises: #5Copyright 2008 by Pearson Education11Encapsulationencapsulation: Hiding implementation details of an object from i

11、ts clients.Encapsulation provides abstraction.separates external view (behavior) from internal view (state)Encapsulation protects the integrity of an objects data.Copyright 2008 by Pearson Education12Private fieldsA field can be declared private.No code outside the class can access or change it.priv

12、ate type name;Examples:private int id;private String name;Client code sees an error when accessing private fields:PointMain.java:11: x has private access in PointSystem.out.println(p1 is ( + p1.x + , + p1.y + ); Copyright 2008 by Pearson Education13Accessing private stateWe can provide methods to ge

13、t and/or set a fields value:/ A read-only access to the x field (accessor)public int getX() return x;/ Allows clients to change the x field (mutator)public void setX(int newX) x = newX;Client code will look more like this:System.out.println(p1: ( + p1.getX() + , + p1.getY() + );p1.setX(14);Copyright

14、 2008 by Pearson Education14Point class, version 4/ A Point object represents an (x, y) location.public class Point private int x; private int y; public Point(int initialX, int initialY) x = initialX; y = initialY; public double distanceFromOrigin() return Math.sqrt(x * x + y * y); public int getX()

15、 return x; public int getY() return y; public void setLocation(int newX, int newY) x = newX; y = newY; public void translate(int dx, int dy) x = x + dx; y = y + dy; Copyright 2008 by Pearson Education15Client code, version 4public class PointMain4 public static void main(String args) / create two Po

16、int objects Point p1 = new Point(5, 2); Point p2 = new Point(4, 3); / print each point System.out.println(p1: ( + p1.getX() + , + p1.getY() + ); System.out.println(p2: ( + p2.getX() + , + p2.getY() + ); / move p2 and then print it again p2.translate(2, 4); System.out.println(p2: ( + p2.getX() + , + p2.getY() + ); OUTPUT:p1 is (5, 2)p2 is (4, 3)p2 is (6, 7)Copyright 2008 by Pearson Education16Benefits of encapsulationProvides abstraction between an object and its clients.Protects an object from unwanted access by clients.A bank app forbids a client to change an

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论