Object Life cycle, Lecture Slide - Computer Science, Slides of C programming

Encapsulation, Object Life Cycle, Garbage Collection, Static Class Members, Const and Readonly Members, Using The this Reference, Rational, Graphics, Drawing, Line, Rectangular, Color Class

Typology: Slides

2010/2011

Uploaded on 10/06/2011

christina
christina šŸ‡ŗšŸ‡ø

4.6

(23)

393 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #18:
Object Life Cycle;
static, readonly, this
http://flint.cs.yale.edu/cs112/
2
Outline
rAdmin. and review
rMore language structures
mobject life cycle and finalizer/destructor
mstatic members
mconst and readonly members
musing this reference
3
Recap: Encapsulation and
Public and Private Accessibility
violate
Encapsulation
Unless properties
enforce
encapsulation
provide services
to clients
support other
methods in the
class
public private
variables
methods
4
Outline
rAdmin. and review
rMore language structures
Ƙobject life cycle and finalizer/destructor
mstatic members
mconst and readonly members
musing this reference
5
Object Life Cycle
rOperator new allocates memory
rConstructor initializes the state
rAssigned to a variable declared with the class as the
type
mthe object is referenced by the variable, e.g.,
Time1 t1 = new Time1(11, 45, 59);
rAn object may be referenced by multiple variables, e.g.,
Time1 t2 = t1;
rAn object may be referenced by no variable (the object
is a garbage)
e.g.,
t1 = new Time1(12, 55, 50);
t1 = null;
6
Garbage Collection
rWhen objects are no longer referenced, the
C# Language Runtime (CLR) performs
garbage collection
mGarbage collection avoids running out of memory
because unused memory has not been reclaimed
rAllocation and deallocation of other resources
(database connections, file access, etc.) must
be explicitly handled by programmers
pf3
pf4
pf5

Partial preview of the text

Download Object Life cycle, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #18:

Object Life Cycle;

static, readonly, this

http://flint.cs.yale.edu/cs112/

Outline

r Admin. and review

r More language structures

m object life cycle and finalizer/destructor

m static members

m const and readonly members

m using this reference

Recap: Encapsulation and

Public and Private Accessibility

violate

Encapsulation

Unless properties

enforce

encapsulation

provide services

to clients

support other

methods in the

class

public private

variables

methods

Outline

r Admin. and review

r More language structures

Ćæ object life cycle and finalizer/destructor

m static members

m const and readonly members

m using this reference

Object Life Cycle

r Operator new allocates memory

r Constructor initializes the state

r Assigned to a variable declared with the class as the

type

m the object is referenced by the variable, e.g.,

Time1 t1 = new Time1(11, 45, 59);

r An object may be referenced by multiple variables, e.g.,

Time1 t2 = t1;

r An object may be referenced by no variable (the object

is a garbage)

e.g.,

t1 = new Time1(12, 55, 50);

t1 = null;

Garbage Collection

r When objects are no longer referenced, the

C# Language Runtime (CLR) performs

garbage collection

m Garbage collection avoids running out of memory

because unused memory has not been reclaimed

r Allocation and deallocation of other resources

(database connections, file access, etc.) must

be explicitly handled by programmers

Garbage Collection

r You can use finalizer s in conjunction with the

garbage collector to release resources and

memory

m Before garbage collector reclaims an object’s

memory, it automatically calls the object’s finalizer

m Each class has only at most one finalizer (also

called destructor )

m Name of a destructor is the ~ character, followed

by the class name

m Destructors do not receive any arguments

Outline

r Admin. and review

r More language structures

m object life cycle and finalizer/destructor

Ćæ static members

m const and readonly members

m using this reference

static Class Members

r Every object of a class has its own copy of all

instance variables

r Sometimes it is useful if all instances of a class share

the same copy of a variable

r Declare variables using keyword static to create only

one copy of the variable at a time (shared by all

objects of the type)

r Static class members

m static data

  • can be accessed in instance methods and static methods

m static methods

  • invocation: ClassName.MethodName( … );
  • can only access static data of the class
    • Question: why not instance data?

See Employee.cs and StaticTest.cs

Outline

Employee.cs

1 // Employee.cs 2 // Employee class contains static data and a static method. 3 4 using System; 5 6 // Employee class definition 7 public class Employee 8 { 9 private string firstName; 10 private string lastName; 11 private static int count; // Employee objects in memory 12 13 // constructor increments static Employee count 14 public Employee( string fName, string lName ) 15 { 16 firstName = fName; 17 lastName = lName; 18 19 ++count; 20 21 Console.WriteLine( "Employee object constructor: " + 22 firstName + " " + lastName + "; count = " + Count ); 23 } 24 25 // destructor decrements static Employee count 26 ~Employee() 27 { 28 --count; 29 30 Console.WriteLine( "Employee object destructor: " + 31 firstName + " " + lastName + "; count = " + Count ); 32 } 33

Employee destructor

Decrease static member

count, to signify that there

is one less employee

Update number of Employees

Outline

Employee.cs

34 // FirstName property 35 public string FirstName 36 { 37 get 38 { 39 return firstName; 40 } 41 } 42 43 // LastName property 44 public string LastName 45 { 46 get 47 { 48 return lastName; 49 } 50 } 51 52 // static Count property 53 public static int Count 54 { 55 get 56 { 57 return count; 58 } 59 } 60 61 } // end class Employee

Outline

StaticTest.cs

1 // StaticTest.cs 2 // Demonstrating static class members. 3 4 using System; 5 6 // StaticTest class definition 7 class StaticTest 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 Console.WriteLine( "Employees before instantiation: " + 13 Employee.Count + "\n" ); 14 15 // create two Employees 16 Employee employee1 = new Employee( "Susan", "Baker" ); 17 Employee employee2 = new Employee( "Bob", "Jones" ); 18 19 Console.WriteLine( "\nEmployees after instantiation: " + 20 "Employee.Count = " + Employee.Count + "\n" ); 21 22 // display the Employees 23 Console.WriteLine( "Employee 1: " + 24 employee1.FirstName + " " + employee1.LastName + 25 "\nEmployee 2: " + employee2.FirstName + 26 " " + employee2.LastName + "\n" ); 27 28 // mark employee1 and employee1 objects for 29 // garbage collection 30 employee1 = null; 31 employee2 = null; 32 33 // force garbage collection 34 System.GC.Collect(); 35

Create 2 Employee objects

Set Employee objects to null

Force garbage collection

Using the this reference

r Every object can reference itself by using the

keyword this

r Often used to distinguish between a method’s

variables and the instance variables of an

object

m e.g., in constructors

See Time4.cs and ThisTest.cs

Outline

Time4.cs

1 // Time4.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time4 class definition 7 public class Time 8 { 9 private int hour; // 0- 10 private int minute; // 0- 11 private int second; // 0- 12 13 // constructor 14 public Time4( int hour, int minute, int second ) 15 { 16 this.hour = hour; 17 this.minute = minute; 18 this.second = second; 19 } 20 21 // create string using this and implicit references 22 public string BuildString() 23 { 24 return "this.ToStandardString(): " + 25 this.ToStandardString() + 26 "\nToStandardString(): " + ToStandardString(); 27 } 28

The this reference is used to set the

class member variables to the

constructor arguments

The this reference is used to

refer to an instance method

Outline

Time4.cs

29 // convert time to standard-time (12 hour) format string 30 public string ToStandardString() 31 { 32 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 33 ( ( this.hour == 12 || this.hour == 0 )? 12 : 34 this.hour % 12 ), this.minute, this.second, 35 ( this.hour < 12? "AM" : "PM" ) ); 36 } 37 38 } // end class Time

The this reference is used to

access member variables

Outline

ThisTest.cs

Program Output

1 // ThisTest.cs 2 // Using the this reference. 3 4 using System; 5 using System.Windows.Forms; 6 7 // ThisTest class definition 8 class Class 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time4 time = new Time4( 12, 30, 19 ); 14 15 MessageBox.Show( time.BuildString(), 16 "Demonstrating the "this" Reference" ); 17 } 18 }

Backup Slides

Outline

r Admin. and review

r More language structures

m object life cycle and finalizer/destructor

m static members

m const and readonly members

m using this reference

Ćæ Two more examples

Ćæ Rational

m Using objects to draw multiple figures

Rational

r How to represent rational numbers such as

r What methods does a rational number need

to support?

See Rational.cs and RationalNumbers.cs

Outline

r Admin. and review

r More language structures

m object life cycle and finalizer/destructor

m static members

m const and readonly members

m using this reference

Ćæ Two more examples

m Rational

Ćæ Using objects to draw multiple figures

Graphics: Drawing Shapes

Using the Graphics class

r Create a Windows application

r In the OnPaint() method, draw any objects you want

r Many methods to draw different shapes

DrawString(string str, Font f, SolidBrush b, int x, int y);

DrawLine(Pen p, int x1, int y1, int x2, int y2);

DrawEllipse(Pen p, int x, int y, int width, int height);

C# uses a coordinate

system with the origin

at the upper left corner

Y

(0, 0) X

Example: Drawing a Line

X

Y

page.DrawLine (pen, 10, 20, 150, 45);

page.DrawLine (pen, 150, 45, 10, 20);

oror

//Variable page is an object of class Graphics

Example: Drawing a Rectangle

X

Y

page.DrawRectangle (p, 50, 20, 100, 40);

The Color Class

r A color is defined in a program using an

object created from the Color class

r The Color class also contains several static

predefined colors