Visual C# 2012 for Programmers ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
More C++ Classes Systems Programming. Systems Programming: C++ Classes 2 Systems Programming: 2 C++ Classes  Preprocessor Wrapper  Time Class Case Study.
Review of C++ Programming Part II Sheng-Fang Huang.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 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.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Chapter 9 Classes: A Deeper Look, Part I Part II.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
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.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Classes and Objects.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CompSci 230 S Programming Techniques
Classes and Objects: A Deeper Look
Object-Oriented Programming: Classes and Objects
Classes and Objects: A Deeper Look (Chapter 8)
Object-Oriented Programming: Classes and Objects
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 9 Classes: A Deeper Look, Part 1
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Object Oriented Programming in java
Object-Oriented Programming: Classes and Objects
Chapter 8 Classes and Objects: A Deeper Look
More C++ Classes Systems Programming.
Presentation transcript:

Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.

Time1 Class Declaration  Class Time1 (Fig. 10.1) represents the time of day. © by Pearson Education, Inc. All Rights Reserved.

 A class’s public methods are the public services or the public interface that this class provides to its clients.  When instance variables are declared in the class body, they can be initialized using the same initialization syntax as a local variable.  The output of this app appears in Fig © by Pearson Education, Inc. All Rights Reserved.

Method SetTime and Throwing Exceptions  Method SetTime is a public method that declares three int parameters and uses them to set the time.  Lines 16–17 test each argument to determine whether the value is in the proper range, and, if so, lines 19–21 assign the values to the hour, minute and second instance variables.  The hour value must be greater than or equal to 0 and less than 24, because universal-time format represents hours as integers from 0 to 23 (e.g., 1 PM is hour 13 and 11 PM is hour 23; midnight is hour 0 and noon is hour 12). © by Pearson Education, Inc. All Rights Reserved.

 Both minute and second values must be greater than or equal to 0 and less than 60.  For values outside these ranges, SetTime throws an exception of type ArgumentOutOfRangeException, which notifies the client code that an invalid argument was passed to the method.  The throw statement creates a new object of type ArgumentOutOfRangeException.  The parentheses following the class name indicate a call to the ArgumentOutOfRangeException constructor. © by Pearson Education, Inc. All Rights Reserved.

 After the exception object is created, the throw statement immediately terminates method SetTime and the exception is returned to the code that attempted to set the time. © by Pearson Education, Inc. All Rights Reserved.

 The Time1Test app class (Fig. 10.2) uses class Time1. © by Pearson Education, Inc. All Rights Reserved.

Notes on the Time1 Class Declaration  The actual data representation used within the class is of no concern to the class’s clients, so fields are normally declared private.  Clients could use the same public methods and properties to get the same results without being aware a change in the internal representation. © by Pearson Education, Inc. All Rights Reserved.

 The access modifiers public and private control access to a class’s variables, methods and properties.  The primary purpose of public methods is to present to the class’s clients a view of the services the class provides.  Clients of the class need not be concerned with how the class accomplishes its tasks.  A class’s private variables, properties and methods are not directly accessible to the class’s clients.  Figure 10.3 demonstrates that private class members are not directly accessible outside the class.  Members of a class—for instance, properties, methods and instance variables—have private access by default. © by Pearson Education, Inc. All Rights Reserved.

 Every object can access a reference to itself with keyword this.  When a non-static method is called, the method’s body implicitly uses keyword this to refer to the object’s instance variables and other methods.  As you’ll see in Fig. 10.4, you can also use keyword this explicitly in a non-static method’s body. © by Pearson Education, Inc. All Rights Reserved.

 If the constructor ’ s parameter names are identical to the class ’ s instance-variable names, so they hide the corresponding instance variables.  You can use the this reference to refer to hidden instance variables explicitly.  If a member is not hidden, the this keyword is implied, but can be included explicitly. © by Pearson Education, Inc. All Rights Reserved.

 Overloaded constructors enable objects of a class to be conveniently initialized in different ways.  To overload constructors, simply provide multiple constructor declarations with different signatures. Class Time2 with Overloaded Constructors  Class Time2 (Fig. 10.5) contains overloaded constructors. © by Pearson Education, Inc. All Rights Reserved.

Class Time2’s Parameterless Constructor  Lines 12–15 declare a constructor with three default parameters.  This constructor is also considered to be the class’s parameterless constructor because you can call the constructor without arguments and the compiler will automatically provide the default parameter values. © by Pearson Education, Inc. All Rights Reserved.

Constructor Initializers  Constructor initializers are a popular way to reuse initialization code provided by one of the class ’ s constructors. © by Pearson Education, Inc. All Rights Reserved.

Notes Regarding Class Time2 ’ s Methods, Properties and Constructors  Consider changing the representation of the time to a single int value representing the total number of seconds that have elapsed since midnight.  Only the bodies of the methods that access the private data directly would need to change.  There would be no need to modify the bodies of methods SetTime, ToUniversalString or ToString.  When there is no access modifier before a get or set accessor, the accessor inherits the access modifier preceding the property name. © by Pearson Education, Inc. All Rights Reserved.

Using Class Time2’s Overloaded Constructors  Class Time2Test (Fig. 10.6) creates six Time2 objects to invoke the overloaded Time2 constructors. © by Pearson Education, Inc. All Rights Reserved.

Every class must have at least one constructor. If you do not provide any constructors in a class’s declaration, the compiler creates a default constructor that takes no arguments when it is invoked. The compiler will not create a default constructor for a class that explicitly declares at least one constructor. If you have declared a constructor, but want to be able to invoke the constructor with no arguments, you must declare a parameterless constructor. © by Pearson Education, Inc. All Rights Reserved.

 A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship. Class Date  Class Date (Fig. 10.7) declares instance variables month and day, and auto-implemented property Year (line 11) to represent a date. © by Pearson Education, Inc. All Rights Reserved.

Class Employee  Class Employee (Fig. 10.8) has instance variables firstName, lastName, birthDate and hireDate. © by Pearson Education, Inc. All Rights Reserved.

Class Employee­Test  Class Employee­Test (Fig. 10.9) creates two Date objects to represent an Employee ’s birthday and hire date, respectively. © by Pearson Education, Inc. All Rights Reserved.

Every object you create uses various system resources, such as memory. In many programming languages, these system resources are reserved for the object’s use until they’re explicitly released by the programmer. If all the references to the object that manages the resource are lost before the resource is explicitly released, it can no longer be released. This is known as a resource leak. The Common Language Runtime (CLR) uses a garbage collector to reclaim the memory occupied by objects that are no longer in use. When there are no more references to an object, the object becomes eligible for destruction. © by Pearson Education, Inc. All Rights Reserved.

Every object has a destructor that’s invoked by the garbage collector to perform termination housekeeping before its memory is reclaimed. A destructor’s name is the class name, preceded by a tilde, and it has no access modifier in its header. After an object’s destructor is called, the object becomes eligible for garbage collection—the memory for the object can be reclaimed by the garbage collector. Memory leaks are less likely in C# than languages like C and C++ (but some can still happen in subtle ways). © by Pearson Education, Inc. All Rights Reserved.

Other types of resource leaks can occur, for example if an app fails to close a file that it has opened. A problem with the garbage collector is that it is not guaranteed to perform its tasks at a specified time. For this reason, destructors are rarely used. © by Pearson Education, Inc. All Rights Reserved.

A static variable is used when only one copy of a particular variable should be shared by all objects of a class. A static variable represents classwide information—all objects of the class share the same piece of data. The declaration of a static variable begins with the keyword static. © by Pearson Education, Inc. All Rights Reserved.

The scope of a static variable is the body of its class. A class’s public static members can be accessed by qualifying the member name with the class name and the member access (. ) operator, as in Math.PI. A class’s private static class members can be accessed only through the methods and properties of the class. static class members exist even when no objects of the class exist—they’re available as soon as the class is loaded into memory at execution time. To access a private static member from outside its class, a public static method or property can be provided. © by Pearson Education, Inc. All Rights Reserved.

Class Employee  Class Employee (Fig ) declares private static variable count and public static property Count.  If a static variable is not initialized, the compiler assigns a default value to the variable.  EmployeeTest method Main (Fig ) instantiates two Employee objects. © by Pearson Education, Inc. All Rights Reserved.

Class EmployeeTest  string objects in C# are immutable—they cannot be modified after they are created. Therefore, it is safe to have many references to one string.  String-concatenation operations result in a new string object containing the concatenated values. The original string objects are not modified.  C# does not guarantee when, or even whether, the garbage collector will execute.  When the garbage collector does run, it is possible that no objects or only a sub set of the eligible objects will be collected. © by Pearson Education, Inc. All Rights Reserved.

 A method declared static cannot access non- static class members directly, because a static method can be called even when no objects of the class exist.  The this reference cannot be used in a static method. © by Pearson Education, Inc. All Rights Reserved.