Classes1

435 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Classes1

  1. 1. Classes Methods and Properties
  2. 2. Introduction to Classes and Objects <ul><li>In object-oriented programming terminology, a class is defined as a kind of programmer-defined type </li></ul><ul><li>From the natural language definition of the word “class”: </li></ul><ul><ul><li>Collection of members that share certain attributes and functionality </li></ul></ul><ul><ul><li>Likewise classes in object-oriented programming </li></ul></ul><ul><li>In object oriented programming languages (like C#, Java) classes are used to combine everything for a concept (like date) </li></ul><ul><ul><li>Data ( state / attributes ) (e.g. date day , month, year ) </li></ul></ul><ul><ul><li>Methods (behavior / tasks) (e.g. display date, increment date ) </li></ul></ul>
  3. 3. An Overview of Object Oriented (OO) Programming <ul><li>An example without OO programming - Calendar display program </li></ul><ul><ul><li>needs several utility functions/methods </li></ul></ul><ul><ul><ul><li>leap year check </li></ul></ul></ul><ul><ul><ul><li>day of week function </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul>day day of week month MonthName leap year year Data Functions . . . <ul><li>Is this structure complex? </li></ul><ul><ul><li>for some yes, for some no </li></ul></ul>
  4. 4. An Overview of Object Oriented (OO) Programming <ul><li>OO version - Calendar display program </li></ul><ul><ul><li>Date concept is developed as a class </li></ul></ul><ul><ul><ul><li>data and methods combined together from the point of view of programmer </li></ul></ul></ul><ul><li>Did you like this? </li></ul><ul><ul><li>for some yes, for some no </li></ul></ul><ul><li>OO approach is more suitable for a human being </li></ul><ul><ul><li>human cognition is mostly based on objects </li></ul></ul>Data (day, month, year) Methods Day of the week Month name …
  5. 5. Introduction to Classes and Objects <ul><li>We define variables of types (like int, double). Similarly, we define objects of classes </li></ul><ul><ul><li>an object is a member of a class </li></ul></ul><ul><li>Why classes and objects? In other words, why object-oriented programming? </li></ul><ul><ul><li>It gives programmers the ability to write programs using off-the-shelf components without dealing with the complexity of those components </li></ul></ul><ul><ul><li>Saves time and effort </li></ul></ul><ul><ul><li>Objects are how real-world entities are represented. </li></ul></ul><ul><li>You may design and implement, and later use your own classes, but we will start with using other-programmers-defined classes </li></ul><ul><ul><li>Examples: we used the Console class </li></ul></ul><ul><ul><li>this is what a programmer generally does </li></ul></ul>
  6. 6. How to Use Classes? <ul><li>The behavior of a class is defined by its methods by which objects of that class are manipulated </li></ul><ul><li>You should know about the methods and what they do </li></ul><ul><ul><li>name of the method </li></ul></ul><ul><ul><li>parameters and parameter types </li></ul></ul><ul><ul><li>return type </li></ul></ul><ul><ul><li>functionality </li></ul></ul><ul><li>You don’t need to know how the method is implemented </li></ul><ul><ul><li>analogy: you can add two int variables using +, but you don’t need to know how computer really adds </li></ul></ul><ul><ul><li>more analogy: you can drive cars, but you don’t need to know how the fuel injection works </li></ul></ul>
  7. 7. The class Dice <ul><li>Computer simulated dice </li></ul><ul><ul><li>not real dice, but have the same functionality </li></ul></ul><ul><ul><ul><li>random number between 1 and “number of sides” </li></ul></ul></ul><ul><ul><li>in this class, we can have dice objects with any number of sides </li></ul></ul><ul><li>State </li></ul><ul><ul><li>number of sides </li></ul></ul><ul><ul><li>roll count </li></ul></ul><ul><li>Methods </li></ul><ul><ul><li>Dice(int sides) </li></ul></ul><ul><li>// constructor – constructs a die with given number of sides </li></ul><ul><ul><li>int Roll() // return the random roll </li></ul></ul><ul><ul><li>int NumSides() // how many sides </li></ul></ul><ul><ul><li>int NumRolls() // # of times this die rolled </li></ul></ul><ul><li>Dice objects will work as pseudo-random number generator Random class from .NET library </li></ul>
  8. 8. Using the class Dice <ul><li>Console.WriteLine(&quot;Rolling {0} sided die.&quot;, cube.NumSides()); </li></ul><ul><li>Console.WriteLine(cube.Roll()); </li></ul><ul><li>Console.WriteLine(cube.Roll()); </li></ul><ul><li>Console.WriteLine(&quot;Rolled {0} times.&quot;, cube.NumRolls()); </li></ul>Dice cube = new Dice(6); // construct six-sided die Dice dodeca = new Dice( 12 ); // construct twelve-sided die See UseDice.cs for full program methods constructor
  9. 9. State and Behavior <ul><li>Behavior of a class is what a class does </li></ul><ul><ul><li>described in verbs </li></ul></ul><ul><ul><ul><li>babies eat, cry </li></ul></ul></ul><ul><ul><ul><li>dice are rolled </li></ul></ul></ul><ul><ul><li>In OO programming terminology, behavior is defined by public methods </li></ul></ul><ul><ul><ul><li>for Dice class, methods are the Dice constructor , NumRolls() , NumSides() and Roll() </li></ul></ul></ul><ul><li>State of a class depends on physical properties </li></ul><ul><ul><li>cars have four wheels, different colors </li></ul></ul><ul><ul><li>dice have a number of sides </li></ul></ul><ul><ul><li>In OO programming, State is defined by private data </li></ul></ul><ul><ul><ul><li>also called member data , instance variables , or data fields </li></ul></ul></ul><ul><ul><ul><li>for Dice class, mySides and myRollCount (see Dice.cs ) </li></ul></ul></ul>
  10. 10. Objects <ul><li>An object is an instance of a class </li></ul><ul><li>When created, in memory a set of private data members are allocated and initialized according to the constructor method </li></ul><ul><ul><li>In other words, each object has a different state </li></ul></ul><ul><li>However, objects share method implementations </li></ul><ul><ul><li>The same function name is used on all objects of the same class </li></ul></ul><ul><li>When a me thod is called on an object, that object’s private data members are accessed and/or modified </li></ul>
  11. 11. Anatomy of the Dice class <ul><li>The class Dice </li></ul><ul><ul><li>Objects: 6-sided dice, 32-sided dice, one-sided dice </li></ul></ul><ul><ul><li>Methods: Roll(), NumSides(), NumRolls() </li></ul></ul><ul><li>A Dice object has state and behavior </li></ul><ul><ul><li>Each object has its own state, just like each int has its own value </li></ul></ul><ul><ul><ul><li>Number of times rolled, number of sides </li></ul></ul></ul><ul><li>All objects in a class share method implementations, but access their own state </li></ul><ul><ul><li>How to respond to NumRolls() ? Return my own # of rolls </li></ul></ul>
  12. 12. What to know? <ul><li>Client programmer (programmer who uses the classes) needs to know the interface </li></ul><ul><ul><li>public methods and constructors </li></ul></ul><ul><ul><ul><li>p arameters, how they behave </li></ul></ul></ul><ul><ul><li>does not need to know private data </li></ul></ul><ul><ul><li>does not need to know how the methods are implemented </li></ul></ul>
  13. 13. From interface to use, the class Dice static void Main(string[] args) { Dice cube = new Dice(6); Dice dodeca = new Dice(12); Console.WriteLine(cube.Roll()); Objects constructed 0 myRollCount my Sides 6 cube 0 myRollCount my Sides 12 dodeca Method invoked 1 myRollCount my Sides 6 cube
  14. 14. Let’s look at the Dice.cs <ul><li>Definition and implementation of the Dice class </li></ul>
  15. 15. Understanding Class Implementations <ul><li>Private data members are global such that they are accessible by all class member function s </li></ul><ul><ul><li>e.g. in the implementation of Roll function, mySides and myRollCount are not defined , but used </li></ul></ul>
  16. 16. Understanding Class Implementations <ul><li>Constructors should assign values to each instance variable </li></ul><ul><ul><li>this is what construction is </li></ul></ul><ul><ul><li>not a rule, but a general programming style </li></ul></ul><ul><li>All data should be private </li></ul><ul><ul><li>Provide propertied or methods as needed </li></ul></ul>
  17. 17. Random class <ul><li>Objects of class Random can produce random byte, int and double values. </li></ul><ul><li>Method Next of class Random generates a random int value. </li></ul><ul><li>The values returned by Next are actually pseudorandom numbers —a sequence of values produced by a complex mathematical calculation. </li></ul><ul><li>The calculation uses the current time of day to seed the random-number generator. </li></ul><ul><li>If you provide Next with two int arguments, it returns a value from the first argument’s value up to, but not including, the second argument’s value. </li></ul><ul><li>The calculation that produces the pseudorandom numbers uses the time of day as a seed value to change the sequence’s starting point. </li></ul><ul><li>You can pass a seed value to the Random object’s constructor. </li></ul><ul><li>Given the same seed value, the Random object will produce the same sequence of random numbers. </li></ul>
  18. 18. Access Modifiers <ul><li>class Dice </li></ul><ul><li>{ </li></ul><ul><li>private int myRollCount; // # times die rolled </li></ul><ul><li>private int mySides; // # sides on die </li></ul><ul><ul><ul><li>public Dice(int sides) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>public int Roll() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>} </li></ul><ul><li>Default is private . (if there is no access modifier) </li></ul>
  19. 19. Access modifiers <ul><li>public </li></ul><ul><ul><li>Methods and Constructors as seen by programmer </li></ul></ul><ul><ul><li>Programmer can use the methods and properties defined in the public section only </li></ul></ul><ul><li>private </li></ul><ul><ul><li>Mostly the data part of the class </li></ul></ul><ul><ul><li>Necessary for internal implementation of class </li></ul></ul><ul><ul><li>Not accessible by programmer </li></ul></ul><ul><li>protected </li></ul><ul><ul><li>we will see this in inheritance </li></ul></ul><ul><li>internal </li></ul><ul><ul><li>Accessible only by methods in the defining assembly </li></ul></ul><ul><li>protected internal </li></ul><ul><ul><li>we will see this in inheritance </li></ul></ul>
  20. 20. Member Data (instance variables) <ul><li>class Dice </li></ul><ul><li>{ </li></ul><ul><li>private int myRollCount; </li></ul><ul><li>private int mySides; </li></ul><ul><li>} </li></ul><ul><li>Will the following code compile? </li></ul><ul><li>Dice cube = new Dice(6); </li></ul><ul><li>Console.WriteLine(&quot;Number of sides: {0}&quot;, </li></ul><ul><li>cube. mySides ); </li></ul><ul><li>How to fix this? </li></ul><ul><li>Console.WriteLine(&quot;Number of sides: {0}&quot;, </li></ul><ul><li>cube. NumSides ()); </li></ul><ul><ul><li>Hiding data (encapsulation): why? </li></ul></ul><ul><ul><ul><li>you can drive cars, but you don’t need to know how the fuel injection works </li></ul></ul></ul><ul><ul><ul><li>when the car’s fuel injection changes, you can still drive that new car </li></ul></ul></ul>
  21. 21. Properties <ul><li>private int myRollCount ; // # times die rolled </li></ul><ul><li>// property to get and set the number of sides </li></ul><ul><li>public int NumRolls </li></ul><ul><li>{ </li></ul><ul><li> get </li></ul><ul><li>{ </li></ul><ul><li>return myRollCount; </li></ul><ul><li> } // end get </li></ul><ul><li>set </li></ul><ul><li>{ </li></ul><ul><li> myRollCount = value; </li></ul><ul><li> } // end set </li></ul><ul><li>} // end property NumRolls </li></ul><ul><li>Does get makes sense? How about set ? </li></ul>
  22. 22. Properties <ul><li>private int mySides; // # sides on die </li></ul><ul><li>// property to get and set the number of sides </li></ul><ul><li>public int NumSides </li></ul><ul><li>{ </li></ul><ul><li> get </li></ul><ul><li>{ </li></ul><ul><li>return mySides; </li></ul><ul><li> } // end get </li></ul><ul><li>set </li></ul><ul><li>{ </li></ul><ul><li>mySides = value; </li></ul><ul><li> } // end set </li></ul><ul><li>} // end property NumSides </li></ul><ul><li>Console.WriteLine(&quot;Number of sides: {0}&quot;, cube. NumSides ); </li></ul><ul><li>Does get makes sense? How about set ? </li></ul>
  23. 23. Autoimplemented Properties <ul><li>// property to get and set the roll count </li></ul><ul><li>public int NumRolls { get; private set; } </li></ul><ul><li>// property to get and set the number of sides </li></ul><ul><li>public int NumSides { get; set; } </li></ul><ul><li>public Dice(int sides) </li></ul><ul><li>{ </li></ul><ul><li> NumRolls = 0; </li></ul><ul><li> NumSides = sides; </li></ul><ul><li>} </li></ul>
  24. 24. Methods <ul><li>The best way to develop and maintain a large application is to construct it from small, simple pieces  divide and conquer </li></ul><ul><li>Methods allow you to modularize an application by separating its tasks into reusable units. </li></ul><ul><ul><li>Reuse the Framework Library, do not reinvent the wheel </li></ul></ul><ul><li>Divide your application into meaningful methods such that it is easier to debug and maintain. </li></ul><ul><li>Methods == Worker analogy: </li></ul>
  25. 25. Methods syntax <ul><li>access_modifier return_type func_name ( parameter list ) </li></ul><ul><li>{ </li></ul><ul><li>statement_1; </li></ul><ul><li>… </li></ul><ul><li>statement_n; </li></ul><ul><li>return return_type; </li></ul><ul><li>} </li></ul><ul><li>(type param 1 , type2 param 2 , …, type param n ) </li></ul><ul><li>public, private </li></ul><ul><li>Examples: </li></ul><ul><li>public int Roll () </li></ul><ul><li>public Dice ( int sides ) </li></ul><ul><li>public static void Main ( string[] args ) </li></ul>
  26. 26. Methods syntax (cont’d) <ul><li>There are three ways to return control to the statement that calls a method . </li></ul><ul><ul><li>Reaching the end of the method. </li></ul></ul><ul><ul><li>A return statement without a value. </li></ul></ul><ul><ul><li>A return statement with a value. </li></ul></ul><ul><li>There could be more than one return in a method. </li></ul><ul><li>At least one return is required in a non-void method. </li></ul>
  27. 27. Method Overloading <ul><li>void DoSomething( int num1, int num2 ); </li></ul><ul><li>void DoSomething( int num1, int num2, int num3 ); </li></ul><ul><li>void DoSomething( float num1, float num2 ); </li></ul><ul><li>void DoSomething( double num1, double num2 ); </li></ul><ul><li>The compiler distinguishes overloaded methods by their signature —a combination of the method’s name and the number, types and order of its parameters. </li></ul><ul><li>Method calls cannot be distinguished by return type  compile error </li></ul><ul><ul><li>int SquareRoot( int num ); </li></ul></ul><ul><ul><li>double SquareRoot( int num ); </li></ul></ul><ul><li>Constructor can be overloaded too. </li></ul>
  28. 28. Scope of Variables <ul><li>The basic scope rules are as follows: </li></ul><ul><ul><li>The scope of a parameter declaration is the body of the method in which the declaration appears. </li></ul></ul><ul><ul><li>The scope of a local-variable declaration is from the point at which the declaration appears to the end of the block containing the declaration. </li></ul></ul><ul><ul><li>The scope of a non-static method, property or field of a class is the entire body of the class. </li></ul></ul><ul><li>If a local variable or parameter in a method has the same name as a field, the field is hidden until the block terminates </li></ul><ul><li>Let’s see an example: scope.cs </li></ul>
  29. 29. Static Methods <ul><li>public class ScopeTest </li></ul><ul><li>{ </li></ul><ul><li>public static void Main(string[] args) </li></ul><ul><li> … </li></ul><ul><li>} </li></ul><ul><li>public static class Math </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><li> public static int Max(int val1, int val2); </li></ul><ul><li> public static int Min(int val1, int val2); </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>You do not need to create an object in memory to use the method, you simply use the class’s name to call the method </li></ul><ul><li>Other methods of the class cannot be called from a static method, only static methods can be called from other static methods </li></ul>
  30. 30. Math class example ( maximum3.cs ) <ul><li>Let’s write a program that finds the maximum of 3 numbers </li></ul><ul><ul><li>Randomly generate the numbers </li></ul></ul><ul><ul><li>Then let’s use the Math class to find the maximum </li></ul></ul>

×