C# 学习笔记(Method) - 05
2006-04-24 13:03
302 查看
Function and Method are synonymous in C# Encapsured in some class and struct Doesn't support global method Value and Reference Parameters In both case, a copy of parameters passes from caller to called method Value Type: a copy of the data Reference Type: a copy of the reference ref Method Parameter This keyword tells the C# compiler that the arguments being passed point to the same memory as the variables in the calling method. Limitation: Initialize the passed arguments efore calling the method The ref parameters are prefixed with an ampersand(&) stind. popping indirect values off the stack. out Method Parameter Doesn't need the calling code to initialize the passed arguments The arguments must be modified in the method. Method Overloading Enable C# programmer to use same method name multiple times with only the passed parameters changed. Each method's parameters list must be different, it doesn't count if a method's return type and access modifier are different The buttom line is that that must be difference in the parameter list Compiler consider ref and out equivalent to each other. Overloading Constructor default constructor: Every instantiation must have a matching constructor if the code is to compile, even if the constructor doesn't do anything. Inheritance and Overloading An overloaded method is considered an overload even if the overloaded versions of the function exist in a base class rather than the current class. Variable Method Parameters Params: Specify a variable number of a method parameters by using params keyword and specify an array in the method's parameter's list. Virtual Method (method overriding) Use the new keyword with the derived class's method definition, the fact base calss and derived class have identical signature is pure coincidence and of no consequence Polymorphism --- enable to define the method multiple times throughout the class hierarchy so that the runtime can call the apropriate version of the method for specific object being used. early binding late binding virtual keyword on the base class override keyword on the derived class callvirt. Calls virtual method of obj.(and nonvirtual) Virtual function table -- VTBL newslot. new slot in the object's VTBL virtual. a virtual method in the non-polymorphism, both the base and the drived class are not virtual The override function must have the same access level as virtual function it override. A virtual member can't be declared as private. To declare a virtual protected member is legal but dumb combine new and virtual when declaring a class function Calling Virtual Method from Constructors Static methods A static method is a method that exists in a class as whole, rather than in specific instance of the class. Class.Method syntax to call the method. This syntax is necessary even if the user has a reference to an instance of the class Access to class member A static method can access any static memeber within the class, but it can't access an instance member. Nonstatic method can access both static and nonstatic members. Static Constructor Only one static constructor can't take parameters can't access instance member -- include this pointer a static consturctor executed before the first instance of a class is created access modifiers are not allowed on static constructors you can provide a nonstatic constructor with the same signature as the static constructor, both the constructors will be called and the static version being called first. |
相关文章推荐
- (原创)c#学习笔记10--定义类成员01--成员定义05--重构成员
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术05--运算符重载
- (原创)c#学习笔记05--变量的更多内容01--类型转换03--使用Convert命令进行显式转焕
- (原创)c#学习笔记09--定义类05--类库项目
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量02--结构
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量03--数组
- (原创)c#学习笔记05--变量的更多内容03--字符串的处理
- (原创)c#学习笔记10--定义类成员05--部分方法定义
- .Net学习笔记----2015-07-21(C#基础复习05,里氏转换)
- (原创)c#学习笔记05--变量的更多内容01--类型转换01--隐式转换
- (原创)c#学习笔记05--变量的更多内容01--类型转换02--显示转换
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量01--枚举
- (原创)c#学习笔记06--函数05--函数的重载
- Visual C# 技术内幕学习笔记 第二章 C#基础(1) 几个概念
- C#.Net 学习笔记(一)
- C#学习笔记之条形码和二维码
- [学习笔记]学C#遇到的几个问题
- boost.asio 学习笔记05——asio的windows实现
- The Key of C# 学习笔记(你今天学到了什么2004-10)
- C#学习笔记:控件的Location属性值的设置与获取