Download presentation
Presentation is loading. Please wait.
Published byTerrance Ottaway Modified over 9 years ago
1
Chapter 9: Using Classes and Objects
2
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
3
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
4
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
5
Creating a Class from Which Objects Can Be Instantiated (cont’d.) 5Microsoft Visual C# 2012, Fifth Edition
6
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
7
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
8
Creating Instance Variables and Methods (cont’d.) 8Microsoft Visual C# 2012, Fifth Edition
9
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
10
Creating Objects (cont’d.) 10Microsoft Visual C# 2012, Fifth Edition
11
11Microsoft Visual C# 2012, Fifth Edition Creating Objects (cont’d.)
12
Passing Objects to Methods You can pass objects to methods just as you can simple data types 12Microsoft Visual C# 2012, Fifth Edition
13
Passing Objects to Methods (cont’d.) 13Microsoft Visual C# 2012, Fifth Edition
14
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
15
Creating Properties (cont’d.) 15Microsoft Visual C# 2012, Fifth Edition
16
Creating Properties (cont’d.) 16Microsoft Visual C# 2012, Fifth Edition
17
17Microsoft Visual C# 2012, Fifth Edition Creating Properties (cont’d.)
18
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
19
Using Auto-Implemented Properties (cont’d.) 19Microsoft Visual C# 2012, Fifth Edition
20
Using Auto-Implemented Properties (cont’d.) 20Microsoft Visual C# 2012, Fifth Edition
21
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
22
22Microsoft Visual C# 2012, Fifth Edition
23
More About public and private Access Modifiers (cont’d.) 23Microsoft Visual C# 2012, Fifth Edition
24
More About public and private Access Modifiers (cont’d.) 24Microsoft Visual C# 2012, Fifth Edition
25
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
26
Understanding the this Reference (cont’d.) 26Microsoft Visual C# 2012, Fifth Edition
27
27Microsoft Visual C# 2012, Fifth Edition Understanding the this Reference (cont’d.)
28
28Microsoft Visual C# 2012, Fifth Edition
29
Understanding the this Reference (cont’d.) 29Microsoft Visual C# 2012, Fifth Edition
30
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
31
Passing Parameters to Constructors Parameterless constructor – A constructor that takes no arguments 31Microsoft Visual C# 2012, Fifth Edition
32
Passing Parameters to Constructors (cont’d.) You can create a constructor that receives argument(s) 32Microsoft Visual C# 2012, Fifth Edition
33
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
34
34Microsoft Visual C# 2012, Fifth Edition
35
Overloading Constructors (cont’d.) 35Microsoft Visual C# 2012, Fifth Edition
36
Overloading Constructors (cont’d.) 36Microsoft Visual C# 2012, Fifth Edition
37
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
38
Understanding GUI Application Objects (cont’d.) 38Microsoft Visual C# 2012, Fifth Edition
39
Understanding GUI Application Objects (cont’d.) 39Microsoft Visual C# 2012, Fifth Edition
40
Understanding GUI Application Objects (cont’d.) 40Microsoft Visual C# 2012, Fifth Edition
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.