版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2025学年度漳州科技职业学院单招《物理》模拟题库含答案详解【B卷】
- 2024-2025学年度环境影响评价工程师之环境影响评价相关法律法规高频难、易错点题附完整答案详解【名校卷】
- 2024-2025学年医学检验(师)检测卷附答案详解【完整版】
- 2024-2025学年度医疗卫生系统人员真题及参考答案详解(培优A卷)
- 2024-2025学年度医学检验(师)测试卷附答案详解【轻巧夺冠】
- 2024-2025学年主管护师(中级)练习题(名校卷)附答案详解
- 2024-2025学年度辅警招聘考试真题及参考答案详解(完整版)
- 2024-2025学年全国统考教师资格考试《教育教学知识与能力(小学)》测试卷含答案详解(黄金题型)
- 2024-2025学年度施工员考前冲刺练习及答案详解(各地真题)
- 2024-2025学年全国统考教师资格考试《教育教学知识与能力(小学)》试题(各地真题)附答案详解
- 【新教材】人教PEP版(2024)四年级下册英语 Unit 1 Class rules 教案
- 露天矿山节后复工安全培训
- (2025年)医学基础知识考试试题库与答案
- 《2025年新湘教版六年级下册小学信息科技备课教案》
- 2026年甘肃省公信科技有限公司面向社会招聘80人(第一批)笔试模拟试题及答案解析
- 2026年中级消控岗位能力测试题目及答案
- 智能医学应用基础- 课件全套 娄岩 第1-13章 智能医学基础理论 -智能医学的伦理、法律与社会问题
- 拖轮安全意识培训课件
- 2026年宁夏单招装备制造大类普高生职业适应性题库含答案
- 引产补偿协议书
- 2026年江苏单招语数英冲刺密卷含答案省考试院命题组同源题
评论
0/150
提交评论