【笔记】《C#大学教程》- 第8章 基于对象的编程
2016-03-10 14:37
393 查看
1.类成员的默认访问修饰语是private
2.一个类可以包含重载构造函数提供多种途径来初始化对象。
3.对类属性的setter getter:
4.析构函数:
5.静态类成员:
(1). static成员不能访问非static成员, 非static 类成员可以访问static 成员。
(2). static方法没有this引用。
6.const 和readonly
(1). const成员默认为static 类型因此不需要额外添加static声明,const成员必须在声明时初始化;
(2). readonly成员可以在类构造函数中初始化,不能在初始化之前调用;
(3). 在构造函数中初始化一个static readonly成员时,必须使用static 构造函数;
(4). 二者一旦初始化之后无法修改值。
7.索引器:
提供类似数组中括号的索引读写值的方法
8.通过新建类库项目生成dll提供别的项目引用。
using System; namespace TimeTest1.src { public class Time1:Object { private int hour; private int minute; private int second; public Time1() { SetTime(0, 0, 0); } public void SetTime( int hourValue, int minuteValue, int secondValue ) { hour = (hourValue >= 0 && hourValue < 24) ? hourValue : 0; minute = (minuteValue >= 0 && minuteValue < 60) ? minuteValue : 0; second = (secondValue >= 0 && secondValue < 60) ? secondValue : 0; } public string ToUniversalString() { return String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second); } public string ToStandardString() { return String.Format("{0}:{1:D2}:{2:D2} {3}", ((hour == 12 || hour == 0) ? 12 : hour % 12), minute, second, (hour < 12 ? "AM" : "PM")); } } }
using System.Windows.Forms; using TimeTest1.src; namespace TimeTest1 { class Program { static void Main(string[] args) { Time1 time = new Time1(); string output; output = "初始24小时制时间为:" + time.ToUniversalString() + "初始标准时间为:" + time.ToStandardString(); time.SetTime(13, 27, 6); output += "初始24小时制时间为:" + time.ToUniversalString() + "初始标准时间为:" + time.ToStandardString(); MessageBox.Show(output, "Testing Class Time1"); } } }
2.一个类可以包含重载构造函数提供多种途径来初始化对象。
3.对类属性的setter getter:
public int Hour { get { return hour; } set { hour = ((value >= 0 && value < 24) ? value : 0); } }
4.析构函数:
~Time1() { }
//强制垃圾回收 System.GC.Collect(); //等待垃圾回收完成 System.GC.WaitForPendingFinalizers();
5.静态类成员:
(1). static成员不能访问非static成员, 非static 类成员可以访问static 成员。
(2). static方法没有this引用。
//在A类中定义 private static int count; public static int Count { get { return count; } } //调用 A.Count;
6.const 和readonly
(1). const成员默认为static 类型因此不需要额外添加static声明,const成员必须在声明时初始化;
(2). readonly成员可以在类构造函数中初始化,不能在初始化之前调用;
(3). 在构造函数中初始化一个static readonly成员时,必须使用static 构造函数;
(4). 二者一旦初始化之后无法修改值。
public const double PI = 3.14159; public readonly int radius; public Constants(int radiusValue) { radius = radiusValue; }
7.索引器:
提供类似数组中括号的索引读写值的方法
private int x = 0; private int y = 0; public double this[int index] { get { return index == 0 ? x : y; } set { if (index == 0) x = value; else y = value; } } public double this[string name] { get { return name == "x" ? x : y; } set { if ("name" == "x") x = value; else y = value; } }
8.通过新建类库项目生成dll提供别的项目引用。
相关文章推荐
- set 命令特殊用法
- 理解C#编程中的静态类和静态成员以及密封类
- 简单讲解Lua中的垃圾回收机制
- You must SET PASSWORD before executing this statement的解决方法
- 批处理 Set 命令详解 让你理解set命令第1/2页
- ASP中set与dim的区别(自己的理解)
- 深入C#中get与set的详解
- C++中const的用法详细总结
- C语言中static的作用及C语言中使用静态函数有何好处
- sql Set IDENTITY_INSERT的用法
- 从汇编看c++中函数里面的static关键字的使用说明
- C++中的const和constexpr详解
- C++的static关键字及变量存储位置总结
- C++ 中const和复合类型
- PHP中new static() 和 new self() 的区别介绍
- set_include_path在win和linux下的区别
- JS中如何设置readOnly的值
- C#静态static的用法实例分析
- jquery中常用的SET和GET
- Flex 编程注意之性能优化、垃圾回收的一些总结