Chapter 7 Objects and Classes
JAVAPPT英文版
Chapter 7
Objects and Classes
2010-11-4
Introduction to Java Programming
Chapter 7- 1
JAVAPPT英文版
7.1 IntroductionObjects:To understand objects and classes and use classes to model objects (§7.2). To learn how to declare a class and how to create an object of a class (§7.3). To understand the roles of constructors and use constructors to create objects (§7.3). To use UML graphical notations to describe classes and objects (§7.3). To distinguish between object reference variables and primitive data type variables (§7.4). To use classes in the Java library (§7.5). To declare private data fields with appropriate get and set methods to make class easy to maintain (§7.6-7.8). To develop methods with object arguments (§7.9). To understand the difference between instance and static variables and methods (§7.10). To determine the scope of variables in the context of a class (§7.11).2010-11-4 Introduction to Java Programming Chapter 7 - 2
JAVAPPT英文版
Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods.
2010-11-4
Introduction to Java Programming
Chapter 7 - 3
JAVAPPT英文版
7.2 Defining Classes for Objects Classes are constructs that define objects of the same type. A Java class uses variables to define data fields and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.
2010-11-4
Introduction to Java Programming
Chapter 7 - 4
JAVAPPT英文版
Classesclass Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double findArea() { return radius * radius * 3.14159; } }2010-11-4 Introduction to Java Programming Chapter 7 - 5
Data field
Constructors
Method
JAVAPPT英文版
7.3 Constructing Objects Using ConstructorsCircle() { } Constructors are a special kind of methods that are invoked to construct objects. Circle(double newRadius) { radius = newRadius; }
2010-11-4
Introduction to Java Programming
Chapter 7 - 6
JAVAPPT英文版
Constructors, cont.Constructors must have the same name as the class itself. Constructors do not have a return type—not even void. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.
2010-11-4
Introduction to Java Programming
Chapter 7 - 7
JAVAPPT英文版
Creating Objects Using Constructorsnew ClassName();
Example: new Circle(); new Circle(5.0);
2010-11-4
Introduction to Java Programming
Chapter 7 - 8
JAVAPPT英文版
Default
ConstructorA class may be declared without constructors. In this case, a no-arg constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class.
2010-11-4
Introduction to Java Programming
Chapter 7 - 9
JAVAPPT英文版
Constructing Objects, cont.Circle radius: doubleUML Graphical notation for classes UML Graphical notation for fields UML Graphical notation for methods
findArea(): double new Circle() circle1: Circle radius = 2 ... new Circle() circlen: Circle radius = 5UML Graphical notation for objects
2010-11-4
Introduction to Java Programming
Chapter 7 - 10
JAVAPPT英文版
7.4 Accessing Objects via Reference VariablesReference variables and Reference Types Accessing an Object’s Data and Methods The null value Default value for a data field Differences between variables of primitive types and reference types
2010-11-4
Introduction to Java Programming
Chapter 7 - 11
JAVAPPT英文版
7.4 Accessing Objects via Reference VariablesReference variables and Reference Types
To declare a Object’s Data and Methods Accessing an reference variable, use the syntax: ClassName The null value objectRefVar;
Example: Default value for a data fieldCircle myCircle; Differences between variables of primitive typesand reference types
To create an objectmyCircle = new Circle(2.0);
2010-11-4
Introduction to Java Programming
Chapter 7 - 12
JAVAPPT英文版
7.4 Accessing Objects via Reference VariablesReference variables and Reference Types Accessing an Object’s Data and Methods The null value objectRefVar.data Default value for a data field e.g., myCircle.radius
Referencing the object’s data:
Differences between variables of primitive types and reference types
Invoking the object’s method:
objectRefVar.methodName(arguments)
e.g., myCircle.findArea()2010-11-4 Introduction to Java Programming Chapter 7 - 13
JAVAPPT英文版
7.4 Accessing Objects via Reference VariablesReference variables and Reference Types Accessing an Object’s Data and Methods The null value, Default value for a data fieldIf a variable of a reference type does not reference types Differences between variables of primitive any object, the variable holds and reference types a special literal value, null. The default value of a data field is …… 此处隐藏:5295字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [初中教育]婚姻家庭法学教学教案
- [初中教育]浅谈小学语文教学中的创新教育
- [初中教育]中华人民共和国侵权责任法2009
- [初中教育]2016-2022年中国薄膜太阳能电池行业发
- [初中教育]多级轻型井点降水的应用
- [初中教育]外语教学法流派介绍和简评
- [初中教育]实验一、典型环节及其阶跃响应
- [初中教育]内蒙古2012-2013学年度国家奖学金获奖
- [初中教育]移动通信营销渠道管理探讨
- [初中教育]初三化学第一学期第一第二章基础知识点
- [初中教育]一天的食物教学设计
- [初中教育]光导照明系统的基本结构及工作原理
- [初中教育]长春市十一高、东北师范大学附属中学、
- [初中教育]“十三五”规划重点-配重式装卸车项目
- [初中教育]领导方法和领导艺术
- [初中教育]第三章 植物病虫草鼠害诊断与防治基
- [初中教育]2019届九年级语文上册 第二单元 6纪念
- [初中教育]甲级单位编制水豆腐项目可行性报告(立
- [初中教育]Ch8-1补充 09101数据库系统原理及应用-
- [初中教育]2017-2023年中国吊装设备行业市场分析
- 制作毕业纪念册需要哪些材料
- 2015-2016学年高二化学苏教版选修4课件
- 哈佛管理导师-创建商业案例
- 职场交际中的谈吐礼仪知识与职场会议接
- 中国糕点及面包行业发展现状与竞争战略
- 沂河“12·7”洪水茶山拦河坝
- 管道水流量计算公式
- 4-2发电机火灾事故处置方案
- 数字信号处理实验五
- 2009年经济师(中级)金融专业知识全真试
- 历史街区保护规划--04历史文化遗产保护
- 宁夏回族自治区中小学职称评价标准
- 评先评优测评表
- 圆的切线证明及线段长求解在在中考中的
- 【解析版】2015年江苏省南京外国语学校
- 人教版八年级上册科学第一章习题精华
- 责任心与执行力
- SA8000社会责任管理体系标准培训
- IgA肾病的饮食应注意
- 杭州市建设工程文件归档整理方案(试行)




