ImageVerifierCode 换一换
格式:PPT , 页数:16 ,大小:632KB ,
资源ID:379147      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-379147.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Building Java Programs.ppt)为本站会员(syndromehi216)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Building Java Programs.ppt

1、Building Java Programs,Chapter 8 Lecture 8-2: Constructors and Encapsulationreading: 8.4 - 8.5 self-checks: #10-17 exercises: #9, 11, 14, 16,2,Object initialization: constructors,reading: 8.4self-check: #10-12 exercises: #9, 11, 14, 16,Initializing objects,Currently it takes 3 lines to create a Poin

2、t 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 most types of objects in Java.,Constructors,constructor: Initializes the state of new objects.public type(paramet

3、ers) statements;runs when the client uses the new keyword does not specify a return type; it implicitly returns the new object being createdIf a class has no constructor, Java gives it a default constructor with no parameters that sets all fields to 0.,Constructor example,public class Point int x;in

4、t y;/ Constructs a Point at the given x/y location.public Point(int initialX, int initialY) x = initialX;y = initialY;public void translate(int dx, int dy) x += dx;y += dy; ,Tracing a constructor call,What happens when the following call is made?Point p1 = new Point(7, 2);,public Point(int initialX,

5、 int initialY) x = initialX;y = initialY; public void translate(int dx, int dy) x += dx;y += dy; ,Client code, version 3,public class PointMain3 public static void main(String args) / create two Point objectsPoint p1 = new Point(5, 2);Point p2 = new Point(4, 3);/ print each pointSystem.out.println(“

6、p1: (“ + p1.x + “, “ + p1.y + “)“);System.out.println(“p2: (“ + p2.x + “, “ + p2.y + “)“); / move p2 and then print it againp2.translate(2, 4);System.out.println(“p2: (“ + p2.x + “, “ + p2.y + “)“); OUTPUT: p1: (5, 2) p2: (4, 3) p2: (6, 7),Common constructor bugs,Accidentally writing a return type s

7、uch as void:public void Point(int 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 wit

8、h the same name as the fields, rather than storing values into the fields. The fields remain 0.,Multiple constructors,A 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 point to

9、the origin, (0, 0)./ Constructs a new point at (0, 0). public Point() x = 0;y = 0; ,10,Encapsulation,reading: 8.5 - 8.6 self-check: #13-17 exercises: #5,Encapsulation,encapsulation: Hiding implementation details of an object from its clients.Encapsulation provides abstraction. separates external vie

10、w (behavior) from internal view (state) Encapsulation protects the integrity of an objects data.,Private fields,A field can be declared private. No code outside the class can access or change it.private type name;Examples:private int id;private String name;Client code sees an error when accessing pr

11、ivate fields:PointMain.java:11: x has private access in Point System.out.println(“p1 is (“ + p1.x + “, “ + p1.y + “)“);,Accessing private state,We can provide methods to get and/or set a fields value:/ A “read-only“ access to the x field (“accessor“)public int getX() return x;/ Allows clients to cha

12、nge 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);,Point class, version 4,/ A Point object represents an (x, y) location. public class Point private int x;private int y;publ

13、ic Point(int initialX, int initialY) x = initialX;y = initialY;public double distanceFromOrigin() return Math.sqrt(x * x + y * y);public int getX() 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

14、 + dy; ,Client code, version 4,public class PointMain4 public static void main(String args) / create two Point objectsPoint p1 = new Point(5, 2);Point p2 = new Point(4, 3);/ print each pointSystem.out.println(“p1: (“ + p1.getX() + “, “ + p1.getY() + “)“);System.out.println(“p2: (“ + p2.getX() + “, “

15、 + p2.getY() + “)“); / move p2 and then print it againp2.translate(2, 4);System.out.println(“p2: (“ + p2.getX() + “, “ + p2.getY() + “)“); OUTPUT: p1 is (5, 2) p2 is (4, 3) p2 is (6, 7),Benefits of encapsulation,Provides abstraction between an object and its clients.Protects an object from unwanted

16、access by clients. A bank app forbids a client to change an Accounts balance.Allows you to change the class implementation. Point could be rewritten to use polar coordinates (radius r, angle ), but with the same methods.Allows you to constrain objects state (invariants). Example: Only allow Points with non-negative coordinates.,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1