Using Classes and Objects

Slides:



Advertisements
Similar presentations
Road Map Introduction to object oriented programming. Classes
Advertisements

OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
ASP.NET Programming with C# and SQL Server First Edition
1.11 Introduction to OOP academy.zariba.com 1. Lecture Content 1.What is OOP and why use it? 2.Classes and objects 3.Static classes 4.Properties, fields.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JavaScript, Fourth Edition
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Corporation
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Using the Standard.NET Framework Classes Telerik Software Academy C# Fundamentals – Part 2.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Learning & Development Mobile apps for iPhone & iPad.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 3 Implementing Classes
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Object-oriented Programming
Lecture 3: Introduction to Object and Classes
C# for C++ Programmers 1.
Creating and Using Objects, Exceptions, Strings
Chapter 13: Overloading and Templates
Object-Oriented Programming: Classes and Objects
Classes and OOP.
More About Objects and Methods
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object-Oriented Programming: Classes and Objects
Creating and Using Classes
Lecture 9 Concepts of Programming Languages
Lecture 4 Using Classes Richard Gesick.
CSC240 Computer Science III
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
CS2011 Introduction to Programming I Objects and Classes
Assessment – Java Basics: Part 1
Class.
OO Programming Concepts
Chapter 9 Introduction To Classes
Chapter 7 Objects and Classes
Lecture 9 Concepts of Programming Languages
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Using Classes and Objects Using the Standard .NET Framework Classes

Table of Contents Classes and Objects Classes in C# Enumerations * 07/16/96 Table of Contents Classes and Objects What are Objects? What are Classes? Classes in C# Declaring Class Fields and Properties: Instance and Static Instance and Static Methods Constructors Enumerations (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Table of Contents (2) Structures Namespaces Random class * 07/16/96 Table of Contents (2) Structures Namespaces Random class Introduction to .NET Common Type System (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* Modeling Real-world Entities with Objects 07/16/96 Classes and Objects Modeling Real-world Entities with Objects (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

What are Objects? Software objects model real-world objects or abstract concepts Examples: bank, account, customer, dog, bicycle, queue Real-world objects have states and behaviors Account' states: holder, balance, type Account' behaviors: withdraw, deposit, suspend

What are Objects? (2) How do software objects implement real- world objects? Use variables/data to implement states Use methods/functions to implement behaviors An object is a software bundle of variables and related methods

Objects Represent Things from the real world  checks  people  shopping list …  numbers  characters  queues  arrays Things from the real world Things from the computer world

What is a Class? The formal definition of class: * 07/16/96 What is a Class? The formal definition of class: Definition by Google Classes act as templates from which an instance of an object is created at run time. Classes define the properties of the object and the methods used to control the object's behavior. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Classes Classes provide the structure for objects Classes define: Define their prototype, act as template Classes define: Set of attributes Represented by variables and properties Hold their state Set of actions (behavior) Represented by methods A class defines the methods and types of data associated with an object

(Properties and Fields) Classes – Example Class Name Attributes (Properties and Fields) Account +Owner: Person +Ammount: double Operations (Methods) +Suspend() +Deposit(sum:double) +Withdraw(sum:double)

Objects An object is a concrete instance of a particular class Creating an object from a class is called instantiation Objects have state Set of values associated to their attributes Example: Class: Account Objects: Ivan's account, Peter's account

Objects – Example Object Class Object Object ivanAccount +Owner="Ivan Kolev" +Ammount=5000.0 Account +Owner: Person +Ammount: double Object peterAccount +Owner="Peter Kirov" +Ammount=1825.33 +Suspend() +Deposit(sum:double) +Withdraw(sum:double) Object kirilAccount +Owner="Kiril Kirov" +Ammount=25.0

Using Classes and their Class Members * 07/16/96 Classes in C# Using Classes and their Class Members (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Classes in C# Classes – basic units that compose programs Implementation is encapsulated (hidden) Classes in C# can contain: Fields (member variables) Properties Methods Constructors Inner types Etc. (events, indexers, operators, …)

Classes in C# – Examples Example of classes (structures): System.Console System.String (string in C#) System.Int32 (int in C#) System.Array System.Math System.Random System.DateTime System.Collections.Generics.List<T>

Declaring Objects An instance of a class or structure can be defined like any other variable: Instances cannot be used if they are not initialized using System; ... // Define two variables of type DateTime DateTime today; DateTime halloween; // Declare and initialize a structure instance DateTime today = DateTime.Now;

Accessing Fields and Properties * 07/16/96 Fields and Properties Accessing Fields and Properties (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Fields Fields are data members of a class Can be variables and constants (read-only) Accessing a field doesn’t invoke any actions of the object Just accesses its value Example: String.Empty (the "" string)

Accessing Fields Constant fields can be only read Variable fields can be read and modified Usually properties are used instead of directly accessing variable fields Examples: // Accessing read-only field String empty = String.Empty; // Accessing constant field int maxInt = Int32.MaxValue;

Properties Properties look like fields Usually used as wrappers Have name and type Can contain code, executed when accessed Usually used as wrappers To control the access to the data fields Can contain more complex logic Can have two components called accessors get for reading their value set for changing their value

Properties (2) According to the implemented accessors properties can be: Read-only (get accessor only) Read and write (both get and set accessors) Write-only (set accessor only) Example of read-only property: String.Length Example of read-write property: Console.BackgroundColor

Accessing Properties and Fields – Example using System; ... DateTime christmas = new DateTime(2009, 12, 25); int day = christmas.Day; int month = christmas.Month; int year = christmas.Year; Console.WriteLine( "Christmas day: {0}, month: {1}, year: {2}", day, month, year); "Day of year: {0}", christmas.DayOfYear); Console.WriteLine("Is {0} leap year: {1}", year, DateTime.IsLeapYear(year));

Accessing Properties and Fields * 07/16/96 Accessing Properties and Fields Live Demo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Instance and Static Members * 07/16/96 Instance and Static Members Accessing Object and Class Members (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Instance and Static Members Fields, properties and methods can be: Instance (or object members) Static (or class members) Instance members are specific for each object Example: different dogs have different name Static members are common for all instances of a class Example: DateTime.MinValue is shared between all instances of DateTime

Accessing Members – Syntax Accessing instance members The name of the instance, followed by the name of the member (field or property), separated by dot (".") Accessing static members The name of the class, followed by the name of the member <instance_name>.<member_name> <class_name>.<member_name>

Instance and Static Members – Examples Example of instance member String.Length Each string object has a different length E.g. "I like C#".Length  9 Example of static member Console.ReadLine() The console is only one (global for the program) Reading from the console does not require to create an instance of it

Calling Instance and Static Methods * 07/16/96 Methods Calling Instance and Static Methods (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Methods Methods manipulate the data of the object to which they belong or perform other tasks Examples: Console.WriteLine(…) Console.ReadLine() String.Substring(index, length) Array.GetLength(index) List<T>.Add(item) DateTime.AddDays(count)

Instance Methods Instance methods manipulate the data of a specified object or perform any other tasks If a value is returned, it depends on the particular class instance Syntax: The name of the instance, followed by the name of the method, separated by dot <object_name>.<method_name>(<parameters>)

Calling Instance Methods – Examples Calling instance methods of String: Calling instance methods of DateTime: String sampleLower = new String('a', 5); String sampleUpper = sampleLower.ToUpper(); Console.WriteLine(sampleLower); // aaaaa Console.WriteLine(sampleUpper); // AAAAA DateTime now = DateTime.Now; DateTime later = now.AddHours(8); Console.WriteLine("Now: {0}", now); Console.WriteLine("8 hours later: {0}", later);

Calling Instance Methods * 07/16/96 Calling Instance Methods Live Demo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Static Methods Static methods are common for all instances of a class (shared between all instances) Returned value depends only on the passed parameters No particular class instance is available Syntax: The name of the class, followed by the name of the method, separated by dot <class_name>.<method_name>(<parameters>)

Calling Static Methods – Examples Constant field Static method using System; double radius = 2.9; double area = Math.PI * Math.Pow(radius, 2); Console.WriteLine("Area: {0}", area); // Area: 26,4207942166902 double precise = 8.7654321; double round3 = Math.Round(precise, 3); double round1 = Math.Round(precise, 1); Console.WriteLine( "{0}; {1}; {2}", precise, round3, round1); // 8,7654321; 8,765; 8,8 Static method Static method

Calling Static Methods * 07/16/96 Calling Static Methods Live Demo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* 07/16/96 Constructors Constructors are special methods used to assign initial values of the fields in an object Executed when an object of a given type is being created Have the same name as the class that holds them Do not return a value A class may have several constructors with different set of parameters (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Constructors (2) Constructor is invoked by the new operator Examples: * 07/16/96 Constructors (2) Constructor is invoked by the new operator Examples: <instance_name> = new <class_name>(<parameters>) String s = new String(new char[]{'a','b','c'}); String s = new String('*', 5); // s = "*****" DateTime dt = new DateTime(2009, 12, 30); DateTime dt = new DateTime(2009, 12, 30, 12, 33, 59); Int32 value = new Int32(); (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Parameterless Constructors The constructor without parameters is called default (parameterless) constructor Example: Creating an object for generating random numbers with a default seed Parameterless constructor call using System; ... Random randomGenerator = new Random(); The class System.Random provides generation of pseudo-random numbers

Constructor with Parameters Example Creating objects for generating random values with specified initial seeds using System; ... Random randomGenerator1 = new Random(123); Console.WriteLine(randomGenerator1.Next()); // 2114319875 Random randomGenerator2 = new Random(456); Console.WriteLine(randomGenerator2.Next(50)); // 47

Generating Random Numbers * 07/16/96 Generating Random Numbers Live Demo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

More Constructor Examples Creating a DateTime object for a specified date and time Different constructors are called depending on the different sets of parameters using System; DateTime halloween = new DateTime(2009, 10, 31); Console.WriteLine(halloween); DateTime julyMorning = new DateTime(2009,7,1, 5,52,0); Console.WriteLine(julyMorning);

Creating DateTime Objects * 07/16/96 Creating DateTime Objects Live Demo (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Types Limited to a Predefined Set of Values Enumerations Types Limited to a Predefined Set of Values

Enumerations Enumerations in C# are types whose values are limited to a predefined set of values E.g. the days of week Declared by the keyword enum in C# Hold values from a predefined set public enum Color { Red, Green, Blue, Black } … Color color = Color.Red; Console.WriteLine(color); // Red color = 5; // Compilation error!

Enumerations Live Demo

Destructor

Destructor Remarks Destructors are used to destruct instances of classes. Destructors cannot be defined in structs. They are only used with classes. A class can only have one destructor. Destructors cannot be inherited or overloaded. Destructors cannot be called. They are invoked automatically. A destructor does not take modifiers or have parameters.

Destructor Remarks Empty destructors should not be used. When a class contains a destructor, an entry is created in the Finalize queue. When the destructor is called, the garbage collector is invoked to process the queue. If the destructor is empty, this just causes a needless loss of performance. The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application.

Destructor Remarks If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits. Syntax of Destructor: ~class_Name(){ // Destructor Statement; }