1. 接口类作为参数,很值得思考哦
Static tesxt(IMA a)//这样a的类型可以A和B
{
a.test();
}
Public interface IMA
{
Void test();
}
Class A:IMA
{
}
Class B:IMA
{
}
2. 尽量不要修改接口函数,这样便于扩充类库的功能.
3. <23种设计模式>,针对常见的开发,写出的23种软件模式
4. 模式应用的思路(遍历器的实现,接口)
使用的过程
C c=new C();
foreach (object item in c)
{
Console.WriteLine(item.ToString());
}
==1===
定义的过程
class C:IEnumerable
{
public IEnumerator GetEnumerator()
{
return new B();
}
}
class B : IEnumerator
{
int iIndex = -1;
int[] i = { 1, 2, 3, 4, 5, 6 };
public bool MoveNext()
{
iIndex++;
if ((iIndex >= 0) && (iIndex < i.Length))
{
return true;
}
return false;
}
public void Reset()
{
iIndex = -1;
}
public Object Current
{
get
{
return i[iIndex];
}
set
{
i[iIndex] =int.Parse( value.ToString());
}
}
}
//关于 IEnumerable金额扣的实现
class C:IEnumerable
{
public IEnumerator GetEnumerator()
{
return new B();
}
}
class B : IEnumerator
{
int iIndex = -1;
//int[] i = { 1, 2, 3, 4, 5, 6 };
int a=0,b=10,c=20;
public bool MoveNext()
{
iIndex++;
if ((iIndex >= 0) && (iIndex <3))
{
return true;
}
return false;
}
public void Reset()
{
iIndex = -1;
}
public Object Current
{
get
{
switch (iIndex)
{
case 0: return a;
case 1: return b;
case 2: return c;
default: return null;
}
}
}
}
从这个角度应该可以看出他的优点了
5. 抽象类的使用
moenyBase b = new A2();
showMoney(b);
static void showMoney(moenyBase b)
{
Console.WriteLine(b.iGetMoney());
Console.ReadLine();
}
abstract class moenyBase
{
public abstract int iGetMoney();
}
class A1 : moenyBase
{
int iMoney = 100;
public override int iGetMoney()
{
return iMoney;
}
}
class A2 : moenyBase
{
int iMoney = 1000;
public override int iGetMoney()
{
return iMoney;
}
}
6. 反射的概念和应用
a) 初始化文件的使用过程
配置文件的内容设定
配置文件的使用情况
using System.Configuration;
Console.WriteLine(ConfigurationManager.AppSettings[0].ToString());
Console.WriteLine(ConfigurationManager.AppSettings[“set1”].ToString());
很方便的哦.
b) 反射的应用代码
Assembly a = Assembly.LoadFrom(@"C:/ ClassLibrary1.dll");
Type t = a.GetType( ConfigurationManager .AppSettings["test"].ToString () );
object o= Activator.CreateInstance(t);
Console.WriteLine(o.GetType());
注意加载字符串的第一行,有一个@什么意思呢?
注意反射过程中函数户名是唯一的,因此不能实现重载这个问题了.重载的要考虑type的参数了.
c) 反射的使用方法
Type t = typeof(A1);
object o = Activator.CreateInstance(t);
MethodInfo m = t.GetMethod("iGetMoney");
object[]oo={10,20};
m.Invoke(o,oo);
7.反射将系统的耦合度降到了最低,但是性能上有所损失
8.构造当中,进行父类的构造
Class myException:Exception
{
Public myException(string s):base(s);//后面是没有类型的哦
}
9.C#当中利用类进行传递委托传递,就叫回调
10.事件
class Program
{
static void Main(string[] args)
{
int max = int.Parse(Console.ReadLine());
M_Class c = new M_Class();
c.m_dg += new mydelegate(c_m_dg);//给这个类定义一个事件,这个事件的具体实现在c_m_dg中
c.test(max);//开始执行这个函数,这里面包含了一个事件的信息
}
static void c_m_dg(string s)
{
Console.WriteLine(s);
}
public delegate void mydelegate(string s);//定义一个接口
public class M_Class
{
public event mydelegate m_dg;//用一个接口定义一个事件
public void test(int max)
{
for (int i = 1; i <= max; i++)
{
System.Threading.Thread.Sleep(1000);
double d = (double)i / (double)max;
m_dg((d * 100).ToString() + "%");//使用这个事件,但是这个事件的函数功能的实现实在外部实现的,内部和外部的实现过程
}
}
}