Chapter 7 Objects and Classes

更新时间:2023-09-06 04:49:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

本文来源:https://www.bwwdw.com/article/l6ui.html

Top