教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 初中教育 >

Chapter 7 Objects and Classes

来源:网络收集 时间:2026-04-27
导读: 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 c

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

Chapter 7 Objects and Classes.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/47392.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)