1. 2 Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie...

44
Chapter 10 Classes and Objects: A Deeper Look 1

Transcript of 1. 2 Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie...

Page 1: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

Chapter 10Classes and Objects: A Deeper Look

1

Page 2: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

2

Page 3: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

Classes

Classes are not just containers for methods◦ Virtually all are classes◦ Blueprint/Cookie Cutter/Recipe◦ Objects – instance of the class (new)◦ Type “prop” tab tab

OOP – vast topic

3

Page 4: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.2 Time Class Case Study

4

Page 5: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

Formatting: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspxPadding: http://msdn.microsoft.com/en-us/library/txafckwd.aspx

5

Page 6: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

6

Page 7: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

7

Page 8: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

8

Page 9: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

9

Page 10: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

Classpublic Aprotected Binternal Cprivate D

Classpublic Aprotected B internal Cprivate D

SubClasspublic Aprotected Binternal Cprivate D

ASSEMBLY

SubClass (outside package)

public Aprotected B internal Cprivate D

Class (outside package)

public Aprotected B internal Cprivate D

Assembly is a chunk of precompiled code that can be executed by the .NET runtime environment. Usually, a .NET program consists of one or more assemblies

Page 11: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.3 Controlling Access to Members

11

Page 12: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.4 Referring to the Current Object’s Members with the this Reference

12

Page 13: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

13

Page 14: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

14

Page 15: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

15

Page 16: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.5 Time Class Case Study: Overloaded Constructors

16

It's the marker for Inheritance and/or the constructor call chain.

Page 17: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

17

Page 18: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

18

Page 19: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

19

Page 20: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

20

Page 21: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

21

Page 22: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

22

Page 23: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

23

Page 24: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

24

Page 25: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

25

Page 26: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.6 Default and Parameter-less Constructors

26

Page 27: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.7 Composition

27

Page 28: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

28

Page 29: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

29

Page 30: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

30

Page 31: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

31

Page 32: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

32

Page 33: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

33

Page 34: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

34

Page 35: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.8 Garbage Collection and Destructors

35

Page 36: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.9 static Class Members

36

Page 37: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

37

Page 38: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

38

Page 39: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

39

Page 40: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

40

Page 41: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

static readonly field is set at run time, and can thus be modified by the containing class

the value of a const field is set to a compile time constant.

In the static readonly case, the containing class is allowed to modify it only in the variable declaration (through a variable initializer) in the static constructor (instance constructors, if it's not static)

static readonly is typically used if the type of the field is not allowed in a const declaration, or when the value is not known at compile time.

Instance readonly fields are also allowed. 

Remember that for reference types, in both cases (static and instance) the readonly modifier only prevents you from assigning a new reference to the field.  It specifically does not make immutable the object pointed to by the reference.

   

What is the difference between const and static readonly?

41

Page 42: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.10 readonly Instance Variables

42

Page 43: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.11 Data Abstraction and Encapsulation

43

Page 44: 1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)

10.12 Class View and Object Browser

44