8.7.3 Methods
来源:互联网 发布:马拉多纳 知乎 编辑:程序博客网 时间:2024/05/27 20:52
8.7.3 Methods
A method is a member that implements a computation or action that can be
performed by an object or class.
Methods have a (possibly empty) list of formal parameters, a return value
(unless the method.s return-type is
void), and are either static or non-static. Static methods are accessed
through the class. Non-static methods,
which are also called instance methods, are accessed through instances of
the class. The example
using System;
public class Stack
{
public static Stack Clone(Stack s) {.}
public static Stack Flip(Stack s) {.}
public object Pop() {.}
public void Push(object o) {.}
public override string ToString() {.}
.
}
class Test
{
static void Main() {
Stack s = new Stack();
for (int i = 1; i < 10; i++)
s.Push(i);
Stack flipped = Stack.Flip(s);
Stack cloned = Stack.Clone(s);
Console.WriteLine("Original stack: " + s.ToString());
Console.WriteLine("Flipped stack: " + flipped.ToString());
Console.WriteLine("Cloned stack: " + cloned.ToString());
}
}
shows a Stack that has several static methods (Clone and Flip) and several
instance methods (Pop, Push,
and ToString).
Methods can be overloaded, which means that multiple methods may have the
same name so long as they
have unique signatures. The signature of a method consists of the name of
the method and the number,
modifiers, and types of its formal parameters. The signature of a method
does not include the return type.
The example
using System;
class Test
{
static void F() {
Console.WriteLine("F()");
}
static void F(object o) {
Console.WriteLine("F(object)");
}
static void F(int value) {
Console.WriteLine("F(int)");
}
static void F(ref int value) {
Console.WriteLine("F(ref int)");
}
Chapter 8 Language Overview
35
static void F(int a, int b) {
Console.WriteLine("F(int, int)");
}
static void F(int[] values) {
Console.WriteLine("F(int[])");
}
static void Main() {
F();
F(1);
int i = 10;
F(ref i);
F((object)1);
F(1, 2);
F(new int[] {1, 2, 3});
}
}
shows a class with a number of methods called F. The output produced is
F()
F(int)
F(ref int)
F(object)
F(int, int)
F(int[])
A method is a member that implements a computation or action that can be
performed by an object or class.
Methods have a (possibly empty) list of formal parameters, a return value
(unless the method.s return-type is
void), and are either static or non-static. Static methods are accessed
through the class. Non-static methods,
which are also called instance methods, are accessed through instances of
the class. The example
using System;
public class Stack
{
public static Stack Clone(Stack s) {.}
public static Stack Flip(Stack s) {.}
public object Pop() {.}
public void Push(object o) {.}
public override string ToString() {.}
.
}
class Test
{
static void Main() {
Stack s = new Stack();
for (int i = 1; i < 10; i++)
s.Push(i);
Stack flipped = Stack.Flip(s);
Stack cloned = Stack.Clone(s);
Console.WriteLine("Original stack: " + s.ToString());
Console.WriteLine("Flipped stack: " + flipped.ToString());
Console.WriteLine("Cloned stack: " + cloned.ToString());
}
}
shows a Stack that has several static methods (Clone and Flip) and several
instance methods (Pop, Push,
and ToString).
Methods can be overloaded, which means that multiple methods may have the
same name so long as they
have unique signatures. The signature of a method consists of the name of
the method and the number,
modifiers, and types of its formal parameters. The signature of a method
does not include the return type.
The example
using System;
class Test
{
static void F() {
Console.WriteLine("F()");
}
static void F(object o) {
Console.WriteLine("F(object)");
}
static void F(int value) {
Console.WriteLine("F(int)");
}
static void F(ref int value) {
Console.WriteLine("F(ref int)");
}
Chapter 8 Language Overview
35
static void F(int a, int b) {
Console.WriteLine("F(int, int)");
}
static void F(int[] values) {
Console.WriteLine("F(int[])");
}
static void Main() {
F();
F(1);
int i = 10;
F(ref i);
F((object)1);
F(1, 2);
F(new int[] {1, 2, 3});
}
}
shows a class with a number of methods called F. The output produced is
F()
F(int)
F(ref int)
F(object)
F(int, int)
F(int[])
- 8.7.3 Methods
- 17.5.3 Virtual methods
- 17.5.3 Virtual methods
- 4.4.3 Framework Methods
- 3 Decomposition Methods
- METHODS
- Methods
- Methods
- Refactoring Notes-Refactoring Methods(3)
- SpringSecurity笔记3-Securing Methods
- ruby methods block moudle 3
- python(3)methods & tips~
- Week4-3Classic parsing methods
- Linux: 3 methods to set environment variables
- 3 Methods to Change SQL Server Password
- Kernel Methods (3) Kernel Linear Regression
- Unity3D save ScreenShots by 3 methods
- Methods, Not Methodology (3): Knowing Everything at the Beginning?
- 8.6 Statements
- 8.6 Statements(cont')
- Gemini
- 8.7 Classes
- 8.7.1 Constants & 8.7.2 Fields
- 8.7.3 Methods
- 8.7.4 Properties
- 8.7.5 Events
- 8.7.6 Operators
- 8.7.7 Indexers
- 8.7.8 Instance constructors
- 8.7.9 Destructors
- 8.7.10 Static constructors
- 8.7.11 Inheritance