Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology

Similar presentations


Presentation on theme: "1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology"— Presentation transcript:

1 1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology andres@dlit.edu.tw http://www.cse.dlit.edu.tw/~andres

2 2 Chapter 7 Classes and Methods

3 3 Chapter 7 Topics l Abstraction and encapsulation in OOD l Designing the public interface for a class l Designing a class constructor l Data lifetime l Declaring methods l Parameter passing l Collecting classes in a namespace

4 7.5 Declaring Methods l Method call l Method declaring: n Heading n Statement Sequence (chapter 2)

5 Two Parts of Method Declaration Private Function square(number As Integer) _ As Integer Return number * number End Function Heading Statement Sequence

6 6 MethodDeclaration Heading Statement Sequence Method Declaration Syntax

7 7 What is in a statement sequence? 'Method heading 0 or more statements here 'Method closing

8 8 Statement Sequence Statement... Statement Sequence Syntax

9 9 What is in a method heading? Private Function Inits() As String Modifiers Method type Identifier Parameter list Result Type

10 10 Heading Syntax Heading Modifiers MethodType Identifier ( Identifier As _ TypeName, Identifier As TypeName... As TypeName )

11 11 Heading Syntax (Cont.) l Modifiers n Public n Private l MethodType n Sub –A method that dose not return a value is called a Sub. n Function –A method that returns a value is called a Function. l Identifier n Method name

12 12 Parameter List l is the means used for a method to share information with the block containing the call. l Parameter declaration n Enclosed in parentheses n optional list n Accepted by the method n With commas separating them n Consists of –Identifier –Type name

13 13 Examples Sub doNothing() Public Sub skipValue() Function trimax(num1 As Integer, num2 As Integer, num3 As Integer) As Integer Private Function Square(number As Integer) As Integer Public Function cube(number As Double) As Double

14 14 Parameters and Arguments l The parameter list in the method heading provides one mechanism by which the method call and the method exchange information. l Parameter: n An identifier declared between the parameters in a method heading. l Argument: n An expression listed in the call to a method.

15 15 Parameter Passing l Arguments are matched to parameters by their relative position within the two lists. Public Sub someMethod (param1 As Integer, param2 As Double, param3 As String) someMethod( argmentA * 2, ardumentB, argumentC) l Parameter Passing: n The transfer of data between the argument and parameters in a method call.

16 16 'Heading Public Shared Function power (base As Integer, _ exponent As Integer) As Integer Parameter Passing (Cont.) number = power ( 2, 5 ) 'Call

17 Method Calls A method is called by using the name of the method followed by ( ) enclosing a list of arguments. Console.WriteLine("Done") a = Math.Max(y,2) value = Math.Sqrt(x) A method call temporarily transfers control to the called method to perform its task.

18 18 MethodName( Argument List ) The argument list is used to communicate values to the method by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the method. In VB.NET, arguments should be used only for sending data into a method. Method Calls (Cont.)

19 19 Method Calls (Cont’) l A method call temporarily transfers control to the called method’s code. l When the method’s code has finished executing, control is transferred back to the calling block.

20 When a method is called, Temporary memory is set up ( for its primitive data type arguments and any local variables, and also for the method’s name if method is not a sub). Then the flow of control passes to the first statement in the method’s body. The called method’s body statements are executed until one of these occurs: Return statement (with or without a return value), or, The method (a sub) is closed. Then control goes back to where the method was called.

21 A VB.NET method can return l in its identifier at most 1 value of the type specified (called the result type) in its heading. l But, a sub method cannot return any value in its identifier.

22 22 Return Syntax ReturnStatement Return Expression

23 Questions l Why is a method used? To do a task (implement responsibilities). l Can one method call another method? Yes l Can a method even call itself? Yes, that is called recursion. It is very useful and requires special care in writing so that an infinite number of calls are not made.

24 24 Primitive and Reference Types letter title book Dim letter As Char Dim title As String Dim book As String letter = 'J'c title = “Programming with VB” book = title 2002 “Programming with VB” Memory Location 2002 2002 ‘J’

25 Parameters in VB.NET l With primitive data types (such as Integer, Double, Boolean), the parameter receives a copy of the value of the argument. Operations on the parameter do not affect the argument. l With reference types (such as String and other classes) the parameter receives a copy of the address where the object is stored. Making changes in the fields of the object referred to by the parameter does affect the argument object. Doing so is poor programming practice. l You can modify an argument to be passed as a reference type by placing the keyword ByRef before the parameter

26 26 Passing a simple type as a parameter (Passing by Value) 321.89 Double arg1 321.89 Double arg1 321.89 Double arg1 321.89 Double param1 91773.112 Double param1 Argument Parameter Call During Method Execution Return Copy Param1 = 91773.112

27 27 Passing a simple and reference type (Passing by Value and Reference) 321.89 Double arg1 Addr317401 Double arg2 “Visual Basic.NET” Memory Address 317401 321.89 Double param1 Addr317401 Double param2 Argument Parameter Simple type Reference type Copy Param1 = 91773.112

28 28 Examples Module Module1 Sub Main() Dim x, y, z As Integer x = 4 y = 6 sum1(x, y, z) Console.WriteLine("z = " & z) x = 2 y = 3 z = sum2(x, y) Console.WriteLine("z = " & z) Console.ReadLine() End Sub Sub sum1(ByVal a As Integer, ByVal b As Integer, ByRef c As Integer) c = a + b End Sub Function sum2(ByVal a As Integer, ByVal b As Integer) dim c as integer c = a + b return c End Function End Module 4 x 6 yz 4 a 6 bc 102 x 3 y 5 z 2 a 3 b 5 c

29 29 Examples Module Module1 Function SwapByRef(ByRef a As Integer, ByRef b As Integer) Dim t t = a a = b b = t End Function Sub Main() Dim x, y As Integer x = 12 : y = 56 SwapByRef(x, y) Console.WriteLine("x = " & x) Console.WriteLine("y = " & y) Console.ReadLine() End Sub End Module

30 30 Examples Module Module1 Sub SwapByVal(ByVal a As Integer, ByVal b As Integer) Dim t t = a a = b b = t End Sub Sub Main() Dim x, y As Integer x = 12 : y = 56 SwapByVal(x, y) Console.WriteLine("x = " & x) Console.WriteLine("y = " & y) Console.ReadLine() End Sub End Module

31 31 Methods for Comparing Strings Method Parameter Returns Operation Performed Name Type EqualsString Boolean CompareToString Integer Tests for equality of string contents. Returns 0 if equal, a positive integer if the string in the parameter comes before the string associated with the method and a negative integer if the parameter comes after it.

32 32 String methods Method Parameter Returns Operation Performed Name Type ToLower none String toUpper none String Returns a new identical string, except the characters are all lowercase. Returns a new identical string, except the characters are all uppercase.

33 33 Examples Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load Dim A, B As Integer A = 10 : B = 15 Label1.Text = " 參考呼叫前 " & " A=" & A & " B=" & B & _ vbNewLine & vbNewLine CallByRef(A, B) Label1.Text += " 參考呼叫後 " & " A=" & A & " B=" & B End Sub Sub CallByRef(ByRef X As Integer, ByRef Y As Integer) X += 3 : Y += 2 Label1.Text += " 參考呼叫中 " & " X=" & X & " Y=" & Y & _ vbNewLine & vbNewLine End Sub

34 34 Examples Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load Dim A, B As Integer A = 10 : B = 15 Label1.Text = " 傳值呼叫前 " & " A=" & A & " B=" & B & _ vbNewLine & vbNewLine CallByVal(A, B) Label1.Text += " 傳值呼叫後 " & " A=" & A & " B=" & B End Sub Sub CallByVal(ByVal X As Integer, ByVal Y As Integer) X += 3 : Y += 2 Label1.Text += " 傳值呼叫中 " & " X=" & X & " Y=" & Y & _ vbNewLine & vbNewLine End Sub

35 35 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim tot1, tot2 As Integer tot1 = Sum(1, 10) Label1.Text = "1 加到 10 的總合為 " & tot1 & _ vbNewLine & vbNewLine tot2 = Sum(5, 12) Label1.Text += "2 加到 12 的總合為 " & tot2 End Sub Function Sum(ByVal vStart As Integer, ByVal vEnd As Integer) As Integer Dim i, total As Integer For i = vStart To vEnd total += i Next Return total ' 此敘述可改寫成 Sum = total End Function

36 36 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Sum(1, 10) Sum(5, 12) End Sub Function Sum(ByVal vStart As Integer, ByVal vEnd As Integer) Dim i, total As Integer For i = vStart To vEnd total += i Next Label1.Text += vStart & " 加到 " & vEnd & _ " 的總合為 " & total & vbNewLine & vbNewLine End Function

37 37 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Sum(1, 10) Call Sum(5, 12) End Sub Sub Sum(ByVal vStart As Integer, ByVal vEnd As Integer) Dim i, total As Integer For i = vStart To vEnd total += i Next Label1.Text += vStart & " 加到 " & vEnd & _ " 的總合為 " & total & vbNewLine & vbNewLine End Sub


Download ppt "1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology"

Similar presentations


Ads by Google