using System;
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.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 写写吧
{
//interface myinterface
//{
// string STR
// {
// get;
// set;
// }
// void outMethod();
//}
//class interfaceuse : myinterface
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();
}
}
}
执行结果是一样 为什么要用接口呢?
最初你的需求是用A.do() D=A as d
后来你的需求变了 需要用 B.do()方法 这时你只要把最初的部份改成 D=B as d 就可以了 后面的执行代码都不需要改变
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)
{
//''''''''''''''''''
// ''''''''''''''''''
// ''''''''''''''''''
// ''''''''''''''''''
}
}
}按照2楼的思想,我改进了书上的列子,请各位大侠帮我实现一下