Chapter 7 Objects and Classes
更新时间:2023-09-06 04:49:01 阅读量: 教育文库 文档下载
- chapter推荐度:
- 相关推荐
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 null for a reference type, 0 for a numeric type, false for a boolean type, and '\u0000' for a char type. However, Java assigns no default value to a local variable inside a method.
2010-11-4
Introduction to Java Programming
Chapter 7 - 14
JAVAPPT英文版
examplepublic class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // has default value false char gender; // c has default value '\u0000'
public static void main(String[] args) { Student student = new Student(); System.out.println("name? " + http://www.77cn.com.cn); System.out.println("age? " + student.age); System.out.println("isScienceMajor? "+student.isScience
Major); System.out.println("gender? " + student.gender); } }2010-11-4 Introduction to Java Programming Chapter 7 - 15
JAVAPPT英文版
examplepublic class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } } Compilation error: variables not initialized
2010-11-4
Introduction to Java Programming
Chapter 7 - 16
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 - 17
JAVAPPT英文版
7.5 Using classes from the Java LibraryJava provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string. For example, the following codejava.util.Date date = new java.util.Date(); System.out.println(date.toString());
displays a string like Sun Mar 09 13:50:19 EST 2003.
2010-11-4
Introduction to Java Programming
Chapter 7 - 18
JAVAPPT英文版
7.6 Visibility Modifiers, Accessors, and Mutatorspublic The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. By default, the class, variable, or method can be accessed by any class in the same package.
2010-11-4
Introduction to Java Programming
Chapter 7 - 19
正在阅读:
Chapter 7 Objects and Classes09-06
2017-2022年中国植物鞣料市场监测及投资决策研究报告 - 图文06-02
新人教版小学英语五年级英语下册各单元作文11-29
2018年高考语文大一轮复习:八文言文阅读学案3含答案05-21
饶雪漫经典爱情语录02-10
我家的多肉作文三年级300字06-28
电子专业术语常用名词缩写中英文对照10-09
心理学实验报告反应时实验12-23
- 1专题二 LINQ和LINQ to Objects
- 2Colour corrections for high redshift objects due to intergalactic attenuation
- 3Chapter 7 Capital Allocation Between the Risky Asset and the Risk-Free Asset
- 4Chapter 3 Environment
- 5chapter8
- 6Homework Chapter 5
- 7Chapter 2 Franklin
- 8chapter8
- 9ERouting - Chapter - 1
- 10Homework Chapter 32
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- Chapter
- Objects
- Classes
- 正午太阳高度角计算、太阳高度角、光照图(9月16)演示文稿
- 数控机床仿真加工实训报告
- 机械制图教学计划终稿
- 质量罚款通知范文
- 垂直整合
- 2012创新方案 第04章第1讲 曲线运动 运动的合成与分解
- 河南省洛阳市2015届高三上学期第一次统一考试 语文含答案
- 中国经济的现状分析_赵力
- 装饰公司报价单
- 2017年浙江省造价工程师安装计量:工程量清单计量模拟试题
- 中国人工皮肤市场专项调研及未来五年发展策略分析报告
- “十三五”重点项目-姜丝项目可行性研究报告
- 2015-2020年中国MPV商务车行业市场分析与投资方向研究报告
- 室内设计参考文献摘要
- 丰田精益管理模式
- 系解实验考
- BSCI-23环境健康安全政策
- 路灯照明工程施工方案
- 上海交通大学土力学与地基基础0
- 四川煤炭重点企业名单