1 / 84

Methods: A Deeper Look

This chapter explores the implementation of behaviors of objects through methods, including encapsulation and code reuse. It discusses the difference between instance and static methods, with examples and explanations. The chapter also covers topics like scope of declarations, method overloading, expression-bodied methods and properties, recursion, and passing arguments by value or by reference.

francisc
Download Presentation

Methods: A Deeper Look

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods: A Deeper Look Ch 7

  2. Why Methods • Implementing behaviors of objects • Encapsulation • Code reuse • Once defined, can be called many times • Don't “reinvent the wheel” • Divide and conquer • Divide a large program into smaller pieces

  3. Instance vs. Static Methods • Instance method • Is the default • Depends on contents of a particular object • Called upon a reference to an object • Static method • Declared with the keyword static • Does not depend on contents of any object • Called upon class

  4. Examples of Instance/Static Methods • Instance method Account account1 = new Account(); account1.Deposit(depositAmount); • Static method Console.Write("Enter deposit amt");

  5. Class Math • Included in the .NET Framework Class Library • Contains many math methods, all static • Examples: Math.Sqrt(900.0)

  6. The Main Method static void Main(string[] args) • Starting point of every C# application • Every application has at least one Main • void method does not return a value • Theargs are possible arguments when application is run at command line

  7. Why is Main Declared static • It is called to begin program execution when no object has been instantiated • As a result, it cannot depend on contents of any object

  8. A Method Example (Fig. 7.2)

  9. Declaring the Method

  10. Result

  11. Three Ways to Call a Method • Use a method name by itself to call another method of the same class Maximum(number1, number2, number3); • Call an instance method upon a reference to an object account1.Deposit(depositAmount); • Call a static method upon the class Console.ReadLine()

  12. Scope of Declarations • Parameter of a method • Local variable declared within a block • Local variable declared in the initialization section of a for loop header • Method or field of a class

  13. Scope Example (Fig. 7.8)

  14. Result

  15. Method Overloading • Multiple methods with the same name • But different types, number, or order of parameters • Compiler matches method call to the appropriate method • A method’s name and number, type, and order of parameters form its signature • Return type is irrelevant

  16. Overloading Example (Fig. 7.13)

  17. Expression-Bodied Methods and Properties • Methods that contain only a return statement static int Cube(int x) => x * x * x; • Read-only properties with a get accessor that contains only a return statement public bool IsNotFaultState => State == “WI” || State == “IL”;

  18. Recursion • A method that calls itself • Another way to implement repetition • Recursion or loop • Recursion runs slower, uses more storage • Recursion sometimes more elegant; less coding

  19. Recursion Example (Fig. 7.16)

  20. Recursive Method Definition

  21. Result

  22. Recursive Evaluation of 5!

  23. Using a Loop(Example by Instructor) static long Factorial( long number ) { long result = 1; for (long i = number; i > 1; i--) result *= i; return result; } // end method Factorial

  24. Formal Parameters & Actual Arguments • Parameter • Placeholder for argument from a caller static long Factorial( long number ) • Argument • Actual value passed from caller to callee Console.WriteLine($"{counter}! = {Factorial(counter)}");

  25. Passing Arguments • Value (simple) type vs. reference (complex) type • Pass-by-value vs. pass-by-reference • Default is pass-by-value • Keyword ref or out indicatesby-ref • ref vs. out • ref requires argument be initialized in caller • out requires argument be assigned in callee

  26. Value Type vs. Reference Type • Value Type • Simple type • Variable contains a value of that type • Reference Type • Non-simple type • Class for instantiating objects • Variable contains reference (address) of an object, which must be instantiated with new

  27. Value/Reference Type Examples

  28. Pass-by-Value • Copy of caller's argument passed into callee • Changing the copy does not affect caller's original argument • If the argument is of a value type • Input only • If the argument is of a reference type • It's possible to change content of caller's object referred to by the argument

  29. Pass-by-Reference • Reference (address) of caller's variable passed into callee • Gives the method ability to access and modify caller's original variable • Changing by-ref parameter in a method actually changes caller's original variable

  30. Which Methods Can Swap(Example by Instructor) static void swap1(int n1, int n2) static void swap2(ref int n1, ref int n2) static void swap3(GradeBook g1, GradeBook g2) {// swapping g1 & g2 } static void swap4(GradeBook g1, GradeBook g2) {// swapping g1.CourseName & g2.CourseName } static void swap5(ref GradeBook g1, ref GradeBook g2) {// swapping g1 & g2 } static void swap6(ref GradeBook g1, ref GradeBook g2) {// swapping g1.CourseName & g2.CourseName }

  31. Test the Methods int courseNumber1 = 101; int courseNumber2 = 102; GradeBook gradeBook1 = new GradeBook( "CS101 Intro to C#" ); GradeBook gradeBook2 = new GradeBook( "CS102 Data Structures" ); GradeBook gradeBook1Copy = gradeBook1; GradeBook gradeBook2Copy = gradeBook2;

  32. Test the Methods (Cont) // Call the six methods swap1(courseNumber1, courseNumber2); swap2(ref courseNumber1, ref courseNumber2); swap3(gradeBook1, gradeBook2); swap4(gradeBook1, gradeBook2); swap5(ref gradeBook1, ref gradeBook2); swap6(ref gradeBook1, ref gradeBook2);

  33. After swap1: courseNumber1: 101 courseNumber2: 102 After swap2: courseNumber1: 102 courseNumber2: 101 After swap3: gradeBook1 course name: CS101 Intro to C# gradeBook2 course name: CS102 Data Structures gradeBook1Copy course name: CS101 Intro to C# gradeBook2Copy course name: CS102 Data Structures After swap4: gradeBook1 course name: CS102 Data Structures gradeBook2 course name: CS101 Intro to C# gradeBook1Copy course name: CS102 Data Structures gradeBook2Copy course name: CS101 Intro to C# After swap5: gradeBook1 course name: CS101 Intro to C# gradeBook2 course name: CS102 Data Structures gradeBook1Copy course name: CS102 Data Structures gradeBook2Copy course name: CS101 Intro to C# After swap6: gradeBook1 course name: CS102 Data Structures gradeBook2 course name: CS101 Intro to C# gradeBook1Copy course name: CS101 Intro to C# gradeBook2Copy course name: CS102 Data Structures

  34. Variable Definitions int courseNumber1 = 101; int courseNumber2 = 102; GradeBook gradeBook1 = new GradeBook( "CS101 Intro to C#" ); GradeBook gradeBook2 = new GradeBook( "CS102 Data Structures" ); GradeBook gradeBook1Copy = gradeBook1; GradeBook gradeBook2Copy = gradeBook2;

  35. Memory (After Variable Definitions) CourseName (anonymous) CS101 CourseName (anonymous) CS102 courseNumber2 102 courseNumber1 101 gradeBook2Copy gradeBook1Copy gradeBook2 gradeBook1

  36. Value Type, By-Value // value type, pass-by-value // Caller's variables CANNOT be changed static void swap1(int n1, int n2) { int temp = n2; n2 = n1; n1 = temp; }

  37. Memory (Before Method Call) int courseNumber1 = 101; int courseNumber2 = 102; courseNumber2 102 Main courseNumber1 101

  38. Memory (Passing Arguments) swap1(courseNumber1, courseNumber2); 102 n2 swap1 n1 101 102 courseNumber2 Main 101 courseNumber1

  39. Memory (Swapping) int temp = n2; n2 = n1; n1 = temp; 102 temp 101 n2 swap1 n1 102 102 courseNumber2 Main 101 courseNumber1

  40. Memory (After Method Call) courseNumber2 102 Main courseNumber1 101

  41. Value Type, By-Reference // value type, pass-by-reference // Caller's variables CAN be changed static void swap2(ref int n1, ref int n2) { int temp = n2; n2 = n1; n1 = temp; }

  42. Memory (Before Method Call) courseNumber2 102 Main courseNumber1 101

  43. Memory (Passing Arguments) swap2(ref courseNumber1, ref courseNumber2); n2 swap2 n1 102 courseNumber2 Main 101 courseNumber1

  44. Memory (Swapping) int temp = n2; n2 = n1; n1 = temp; 102 temp n2 swap2 n1 101 courseNumber2 Main 102 courseNumber1

  45. Memory (After Method Call) courseNumber2 101 Main courseNumber1 102

  46. Reference Type, By-Value (1) // reference-type, pass-by-value // Caller's references CANNOT be changed static void swap3(GradeBook gb1, GradeBook gb2) { GradeBook temp = gb2; gb2 = gb1; gb1 = temp; }

  47. Memory (Before Method Call) CourseName (anonymous) CS101 CourseName (anonymous) CS102 gradeBook2Copy gradeBook1Copy Main gradeBook2 gradeBook1

More Related