A a = new A(); D d = a as D; d.outMethod(); 可以实现 类A
换一种写法可能更好理解 class Program { static void Main(string[] args) { test((new A()).GetType().ToString()); test((new B()).GetType().ToString()); test((new C()).GetType().ToString()); } public test(string type); {
D d = System.Activator.CreateInstance(System.Type.GetType(type)) as D; D.outMethod(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace interfaceoo { class interfaceuse { string str = "接口实现"; public string STR { get { return str; } set { str = value; } } public void outMethod() { Console.WriteLine(this.STR); } } class Program { static void Main(string[] args) { interfaceuse interfaceuser = new interfaceuse(); interfaceuser.outMethod(); } } }改成这样 结果是一样疑惑
{
Fck(); // 此处省略一个元音字母
Pay();
}风尘女只在乎来的人是不是实现了 IWhoremaster 接口,不在乎他具体是哪个人,于是我们说,这个风尘女是在针对接口编程
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace interfaceoo
{
interface myinterface
{
string STR
{
get;
set;
}
void outMethod();
}
class interfaceuse : myinterface
{
string str = "接口实现";
public string STR
{
get { return str; }
set { str = value; }
}
public void outMethod()
{
Console.WriteLine(this.STR);
}
} class Program
{
static void Main(string[] args)
{
interfaceuse interfaceuser = new interfaceuse();
interfaceuser.outMethod();
}
}
}
实现了 上述接口,有什么作用呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace interfaceoo
{
class interfaceuse
{
string str = "接口实现";
public string STR
{
get { return str; }
set { str = value; }
}
public void outMethod()
{
Console.WriteLine(this.STR);
}
} class Program
{
static void Main(string[] args)
{
interfaceuse interfaceuser = new interfaceuse();
interfaceuser.outMethod();
}
}
}
改成这样 结果是一样疑惑
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace interfaceoo
{
interface D
{
string STR
{
get;
set;
}
void outMethod();
}
class A : D
{
string str = "接口实现";
public string STR
{
get { return str + "A"; }
set { str = value; }
}
public void outMethod()
{
if (Console.ReadLine().ToString() == "1")
Console.WriteLine(this.STR);
}
}
class B : D
{
string str = "接口实现";
public string STR
{
get { return str + "B"; }
set { str = value; }
}
public void outMethod()
{
if (Console.ReadLine().ToString() == "2")
Console.WriteLine(this.STR);
}
}
class C : D
{
string str = "接口实现";
public string STR
{
get { return str + "C"; }
set { str = value; }
}
public void outMethod()
{
if(Console.ReadLine().ToString() == "3")
Console.WriteLine(this.STR);
Console.Read();
}
} class Program
{
static void Main(string[] args)
{
//开始的业务需要用A的实现
//你可以写
A a=new A();D d=a as D;test(d);
// 当你的业务需要用B实现时你只要改成 B b=new B();D d=b as D;test(d);
//就可以了不需要改动test()方法
// ''''''''''''''''''''''
// '''''''''''''''''''''''''' }
public test(D d);
{
// '''''''''''''''''''''' //这里你具体的业务需求代码
d.outMethod();
// '''''''''''''''''''''' }
}
}
接口定义的是规范,这是它类Class在概念上的本质区别,当我们谈接口的时候,谈的是一个事物应该具备的动作和属性,而谈到类的时候,就已经具体到了事物。
可以实现 类A
{
static void Main(string[] args)
{
test((new A()).GetType().ToString());
test((new B()).GetType().ToString());
test((new C()).GetType().ToString());
}
public test(string type);
{
D d = System.Activator.CreateInstance(System.Type.GetType(type)) as D;
D.outMethod();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace interfaceoo
{
class interfaceuse
{
string str = "接口实现";
public string STR
{
get { return str; }
set { str = value; }
}
public void outMethod()
{
Console.WriteLine(this.STR);
}
} class Program
{
static void Main(string[] args)
{
interfaceuse interfaceuser = new interfaceuse();
interfaceuser.outMethod();
}
}
}改成这样 结果是一样疑惑
这个就OK的!
接口也就明白了
若干设计模式也就明白了所以 请好好认识多态<大话设计模式> 后面一些附录