Download presentation
Presentation is loading. Please wait.
1
More Object Oriented Programming
Lecture 15 More Object Oriented Programming Richard Gesick
2
Topics Static members “this” Parameters by value, ref, out
3
The static Modifier Remember that static methods (also called class methods) that can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object
4
Static Variables Static variables are also called class variables
Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static int count; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class share static variables Changing the value of a static variable in one object changes it for all others Local variables cannot be static
5
Static Methods class Helper public static int triple (int num) {
int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5);
6
Static Methods Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference static variables or local variables
7
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference is used in a method called tryMe If tryMe is invoked as follows, the this reference refers to obj1: obj1.tryMe(); But in this case, the this reference refers to obj2: obj2.tryMe();
8
The this reference The this reference can also be used to distinguish the parameters of a constructor from the corresponding instance variables with the same names public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; }
9
Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2
10
Reference Assignment Before After
For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2
11
Aliases Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different reference variables Aliases can be useful, but should be managed carefully Changing the object’s state (its variables) through one reference changes it for all of its aliases
12
Parameters There are three main types of parameters:
Value - passes a copy (value) of the variable to the method. This is the default. Reference - passes a reference to the actual variable. Marked with "ref", use this when you want to pass a value in and have any change to that value be persistent when the method is complete Out - passes a reference to the actual variable. Marked with "out", use this when you want the method to generate a value and place it for later use in the actual variable (persists when the method is complete)
13
Example 1 and the output is ?
static void Main() { int a = 42; Console.WriteLine (a); B (a); } static void B (int x) x += 9; Console.WriteLine (x);
14
Example 2 and the output is ?
static void Main() { int a = 42; Console.WriteLine (a); B (ref a); } static void B (ref int x) x += 9; Console.WriteLine (x);
15
Example 3 and the output is ?
static void Main() { int a; B (out a); Console.WriteLine (a); } static void B (out int x) x = 9; Console.WriteLine (x);
16
Example 4 and the output is ?
class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (myZ); Console.WriteLine (myZ.y); } static void B (Z x) x.y += 9; Console.WriteLine (x.y);
17
Example 5 and the output is ?
class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (ref myZ); Console.WriteLine (myZ.y); } static void B (ref Z x) x = new Z(); x.y = 1; Console.WriteLine (x.y);
18
Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type. Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.