类属性的定义与定义字段有些相似之处.但结构代码要多出不少.属性一般与一个私有字段相关联,以控制对这个字段的访问,此时get块可以直接返回该字段的值.
看代码示例:
- class MyBase
- {
- private int age;
- public int Age
- {
- get
- {
- return age;
- }
- set
- {
- age = value;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- MyBase MyObj = new MyBase();
- MyObj.Age = 20;
- Console.WriteLine(MyObj.Age);
- Console.ReadKey();
- }
- }
Read the rest of this entry »
C#编程, catch, throw, try, 定义类, 属性
C#编程:定义类成员-定义方法
接着总结定义类成员-定义方法.定义方法通常有四个关键字.来决定方法的使用区域.
关键字:virtual, abstract:, override, extern, static
1.virtual:方法可以重写
2.abstract:只用于抽象类中重写
3.override:方法重写了一个基类的方法
4.extern:方法定义放在其它地方
定义方法实例:
- class MyClass
- {
- public string GetString()
- {
- return "here is a string.";
- }
- }
使用此方法:先实例化对象
- static void Main(string[] args)
- {
- MyClass MyObj = new MyClass();
- Console.WriteLine(MyObj.GetString());
- Console.ReadKey();
- }
Read the rest of this entry »
abstract, C#编程, extern, override, virtual, 定义方法, 定义类, 继承