40
Chapter 9: Using Classes and Objects

Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Embed Size (px)

Citation preview

Page 1: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Chapter 9:Using Classes and Objects

Page 2: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding Class Concepts

• Types of classes– Classes that are only application programs with a Main()

method– Classes from which you instantiate objects

• Can contain a Main() method, but it is not required

• Everything is an object– Every object is a member of a more general class

• An object is an instantiation of a class• Instance variables (also called fields)– Object attributes – Data components of a class

2Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding Class Concepts (cont’d.)

• Instance methods– Methods associated with objects– Every instance of the class has the same methods

• Class client or class user– A program or class that instantiates objects of another

prewritten class

3Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating a Class from Which Objects Can Be Instantiated

• Class header or class definition parts– An optional access modifier

• Default is internal

– The keyword class– Any legal identifier for the name of your class

• Class access modifiers– public– protected– internal– private

4Microsoft Visual C# 2012, Fifth Edition

Page 5: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating a Class from Which Objects Can Be Instantiated (cont’d.)

5Microsoft Visual C# 2012, Fifth Edition

Page 6: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Instance Variables and Methods

• When creating a class, define both its attributes and its methods

• Field access modifiers– new, public, protected, internal, private, static, readonly, and volatile

• Most class fields are nonstatic and private– Provides the highest level of security

6Microsoft Visual C# 2012, Fifth Edition

Page 7: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Instance Variables and Methods (cont’d.)

• Using private fields within classes is an example of information hiding

• Most class methods are public• private data/public method arrangement– Allows you to control outside access to your data

7Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Instance Variables and Methods (cont’d.)

8Microsoft Visual C# 2012, Fifth Edition

Page 9: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Objects

• Declaring a class does not create any actual objects• The two-step process to create an object:– Supply a type and an identifier– Create the object, which allocates memory for it

• When you create an object, you call its constructor

9Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Objects (cont’d.)

10Microsoft Visual C# 2012, Fifth Edition

Page 11: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

11Microsoft Visual C# 2012, Fifth Edition

Creating Objects (cont’d.)

Page 12: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Passing Objects to Methods

• You can pass objects to methods just as you can simple data types

12Microsoft Visual C# 2012, Fifth Edition

Page 13: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Passing Objects to Methods (cont’d.)

13Microsoft Visual C# 2012, Fifth Edition

Page 14: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Properties

• Property– A member of a class that provides access to a field of a

class– Defines how fields will be set and retrieved

• Properties have accessors– set accessors for setting an object’s fields– get accessors for retrieving the stored values

• Read-only property– Has only a get accessor

14Microsoft Visual C# 2012, Fifth Edition

Page 15: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Properties (cont’d.)

15Microsoft Visual C# 2012, Fifth Edition

Page 16: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Creating Properties (cont’d.)

16Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

17Microsoft Visual C# 2012, Fifth Edition

Creating Properties (cont’d.)

Page 18: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Using Auto-Implemented Properties

• Auto-implemented property– The property’s implementation is created for you

automatically with the assumption that:• The set accessor should simply assign a value to the appropriate

field• The get accessor should simply return the field

• When you use an auto-implemented property, you do not need to declare the field that corresponds to the property

18Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Using Auto-Implemented Properties (cont’d.)

19Microsoft Visual C# 2012, Fifth Edition

Page 20: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Using Auto-Implemented Properties (cont’d.)

20Microsoft Visual C# 2012, Fifth Edition

Page 21: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

More About public and private Access Modifiers

• Occasionally, you need to create public fields or private methods– You can create a public data field when you want all

objects of a class to contain the same value

• const within a class is always static– Belongs to the entire class, not to any particular instance

21Microsoft Visual C# 2012, Fifth Edition

Page 22: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

22Microsoft Visual C# 2012, Fifth Edition

Page 23: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

More About public and private Access Modifiers (cont’d.)

23Microsoft Visual C# 2012, Fifth Edition

Page 24: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

More About public and private Access Modifiers (cont’d.)

24Microsoft Visual C# 2012, Fifth Edition

Page 25: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding the this Reference

• You might eventually create thousands of objects from a class

• this reference– An implicitly passed reference

• When you call a method, you automatically pass the this reference to the method– It tells the method which instance of the class to use

25Microsoft Visual C# 2012, Fifth Edition

Page 26: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding the this Reference (cont’d.)

26Microsoft Visual C# 2012, Fifth Edition

Page 27: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

27Microsoft Visual C# 2012, Fifth Edition

Understanding the this Reference (cont’d.)

Page 28: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding the this Reference (cont’d.)

28Microsoft Visual C# 2012, Fifth Edition

Page 29: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding the this Reference (cont’d.)

29Microsoft Visual C# 2012, Fifth Edition

Page 30: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding Constructors

• Constructor– A method that instantiates an object

• Default constructor– An automatically supplied constructor without parameters

• Default value of the object– The value of an object initialized with a default constructor

30Microsoft Visual C# 2012, Fifth Edition

Page 31: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Passing Parameters to Constructors

• Parameterless constructor– A constructor that takes no arguments

31Microsoft Visual C# 2012, Fifth Edition

Page 32: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Passing Parameters to Constructors (cont’d.)

• You can create a constructor that receives argument(s)

32Microsoft Visual C# 2012, Fifth Edition

Page 33: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Overloading Constructors

• C# automatically provides a default constructor until you provide your own constructor

• Constructors can be overloaded– You can write as many constructors as you want, as long

as their argument lists do not cause ambiguity

33Microsoft Visual C# 2012, Fifth Edition

Page 34: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

34Microsoft Visual C# 2012, Fifth Edition

Page 35: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Overloading Constructors (cont’d.)

35Microsoft Visual C# 2012, Fifth Edition

Page 36: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Overloading Constructors (cont’d.)

36Microsoft Visual C# 2012, Fifth Edition

Page 37: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding GUI Application Objects

• Objects you have been using in GUI applications are just like other objects– They encapsulate properties and methods

37Microsoft Visual C# 2012, Fifth Edition

Page 38: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding GUI Application Objects (cont’d.)

38Microsoft Visual C# 2012, Fifth Edition

Page 39: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding GUI Application Objects (cont’d.)

39Microsoft Visual C# 2012, Fifth Edition

Page 40: Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method

Understanding GUI Application Objects (cont’d.)

40Microsoft Visual C# 2012, Fifth Edition