C# 创建接口实例的需要是什么?
我正在研究界面,并逐渐认识到我们无法创建界面的Instance。我已经讨论了stackoverflow上已经提出的许多问题,但正如我的心境所说,我希望所有这些问题都有更基本的内容 正如我所做的这个例子,我隐式地实现了一个接口,并使用了这个方法,就好像这个方法是类本身一样C# 创建接口实例的需要是什么?,c#,.net,C#,.net,我正在研究界面,并逐渐认识到我们无法创建界面的Instance。我已经讨论了stackoverflow上已经提出的许多问题,但正如我的心境所说,我希望所有这些问题都有更基本的内容 正如我所做的这个例子,我隐式地实现了一个接口,并使用了这个方法,就好像这个方法是类本身一样 namespace MyConsoleProject { class Program : Iinterface { public int myfunc(int a, int b)
namespace MyConsoleProject
{
class Program : Iinterface
{
public int myfunc(int a, int b)
{
return a * b;
}
static void Main(string[] args)
{
Program obj = new Program();
int result = obj.myfunc(10, 20);
Console.WriteLine(result);
Console.ReadLine();
}
}
interface Iinterface
{
int myfunc(int a, int b);
}
}
我对此没问题,但看看我调用接口的方式。我创建了一个
类并调用接口的函数。首先,我不清楚我调用的是接口方法还是类方法。因为类具有相同的方法和主体。为什么我需要界面呢
我经历的最重要的一件事是:
我什么时候需要创建这样的接口实例
Iinterface objinterface=new Program ();
我们不创建接口的实例。您仍在此处创建该类的实例。接口仅提供方法和属性的声明。但是,类有责任在实现它的同时提供定义 现在我们什么时候使用接口?了解松耦合和后期绑定。下面是一个简单的示例,演示我们可以使用接口的情况
/// <summary>
/// This class should have a method that returns price of pet food. This class should be able to handle different pet animals
/// which as of now, is unknown
/// </summary>
class PetFood
{
public decimal GetFoodPrice(string petType)
{
IPetAnimal petAnimal = null;
if (petType == "Dog")
{
petAnimal = new Dog();
}
else if (petType == "Cat")
{
petAnimal = new Cat();
}
else
{
MessageBox.Show("This animal is not supported yet.");
return 0M;
}
return petAnimal.GetFoodPrice();
}
}
interface IPetAnimal
{
decimal GetFoodPrice();
}
class Dog : IPetAnimal
{
public decimal GetFoodPrice()
{
return 10M;
}
}
class Cat : IPetAnimal
{
public decimal GetFoodPrice()
{
return 5M;
}
}
//
///这个类应该有一个返回宠物食品价格的方法。这门课应该能够处理不同的宠物动物
///目前还不清楚
///
宠物食品
{
公共十进制GetFoodPrice(字符串类型)
{
IPetAnimal petAnimal=null;
如果(宠物类型==“狗”)
{
petAnimal=新狗();
}
否则如果(petType==“Cat”)
{
petAnimal=新猫();
}
其他的
{
Show(“这个动物还不受支持。”);
返回0米;
}
返回petAnimal.GetFoodPrice();
}
}
接口IPetAnimal
{
十进制GetFoodPrice();
}
犬类:伊佩塔尼马尔
{
公共十进制GetFoodPrice()
{
返回10米;
}
}
类别猫:IPetAnimal
{
公共十进制GetFoodPrice()
{
返回5M;
}
}
我们不创建接口的实例。您仍在此处创建该类的实例。接口仅提供方法和属性的声明。但是,类有责任在实现它的同时提供定义
现在我们什么时候使用接口?了解松耦合和后期绑定。下面是一个简单的示例,演示我们可以使用接口的情况
/// <summary>
/// This class should have a method that returns price of pet food. This class should be able to handle different pet animals
/// which as of now, is unknown
/// </summary>
class PetFood
{
public decimal GetFoodPrice(string petType)
{
IPetAnimal petAnimal = null;
if (petType == "Dog")
{
petAnimal = new Dog();
}
else if (petType == "Cat")
{
petAnimal = new Cat();
}
else
{
MessageBox.Show("This animal is not supported yet.");
return 0M;
}
return petAnimal.GetFoodPrice();
}
}
interface IPetAnimal
{
decimal GetFoodPrice();
}
class Dog : IPetAnimal
{
public decimal GetFoodPrice()
{
return 10M;
}
}
class Cat : IPetAnimal
{
public decimal GetFoodPrice()
{
return 5M;
}
}
//
///这个类应该有一个返回宠物食品价格的方法。这门课应该能够处理不同的宠物动物
///目前还不清楚
///
宠物食品
{
公共十进制GetFoodPrice(字符串类型)
{
IPetAnimal petAnimal=null;
如果(宠物类型==“狗”)
{
petAnimal=新狗();
}
否则如果(petType==“Cat”)
{
petAnimal=新猫();
}
其他的
{
Show(“这个动物还不受支持。”);
返回0米;
}
返回petAnimal.GetFoodPrice();
}
}
接口IPetAnimal
{
十进制GetFoodPrice();
}
犬类:伊佩塔尼马尔
{
公共十进制GetFoodPrice()
{
返回10米;
}
}
类别猫:IPetAnimal
{
公共十进制GetFoodPrice()
{
返回5M;
}
}
请注意,您不能创建接口的实例。只有一个实现它的对象
如果您创建这样的对象,objinterface
将仅具有接口公开的属性和方法。你不能使用其余的,除非你投下它
我发现有两种方法很有用:
1) 您可以使用该接口在某种程度上定义希望对象履行的契约
2) 您希望限制对象的外部使用,从而将其用作如下参数。这还允许传入实现接口的任何对象:
public void MyMethod(Iinterface obj)
或者像这样发obj的时候
public Iinterface MyMethod()
通过这种方式,您可以限制使用正在传递的内容(有些)。请注意,您无法创建接口实例。只有一个实现它的对象 如果您创建这样的对象,
objinterface
将仅具有接口公开的属性和方法。你不能使用其余的,除非你投下它
我发现有两种方法很有用:
1) 您可以使用该接口在某种程度上定义希望对象履行的契约
2) 您希望限制对象的外部使用,从而将其用作如下参数。这还允许传入实现接口的任何对象:
public void MyMethod(Iinterface obj)
或者像这样发obj的时候
public Iinterface MyMethod()
通过这种方式,你可以限制使用你正在传递的内容(某种程度上)。我举了一个真实单词的例子来解释界面的含义:界面就像我们对现实的想象,但每个人都有自己对现实的想象,所有的想象都是真实的!
现实有很多维度,但每个人都意识到其中的一些维度。我举了一个真实世界的例子来解释界面的含义:界面就像我们对现实的想象,但每个人都有自己对现实的想象,所有的想象都是真实的!
现实有很多维度,但每个人都意识到其中的一些维度。假设您有这些类和一个接口:
public interface IDoesSound {
void Sound();
}
public class Dog : IDoesSound {
public void Sound() {
Console.WriteLine("Dog goes Woof!");
}
}
public class Cat : IDoesSound {
public void Sound() {
Console.WriteLine("Cat goes Mew!");
}
}
这个Main()
:
IDoesSound animal
向编译器保证,分配给animal
的对象将具有IDoesSound
中声明的所有方法和属性,在这种情况下,只需void Sound()
。正因为如此,没有什么能阻止我们拥有IDoesSound animal=new Dog()
艾尔
foreach(IWeapon w in myCharacter.Weapons) {
w.Speed = w.Speed * 0.9;
w.Strength = w.Strength * 0.9;
w.Vibrate();
}
public class GasRoom {
public void ApplyGasEffect(IList<IWeapon> weapons) {
foreach(IWeapon w in weapons) {
w.Speed = w.Speed * 0.9;
w.Strength = w.Strength * 0.9;
w.Vibrate();
}
}
}
myGasRoom.ApplyGasEffect(myCharacter.Weapons)
//Give the player two weapons to start with.
myCharacter.Weapons = new List<IWeapons>();
IWeapon b = new Bazooka();
myCharacter.Weapons.Add(w);
IWeapon r = new Rocket();
myCharacter.Weapons.Add(r);
//Or you can do it all in one line like this:
//myCharacter.Weapons = new List<IWeapons>() { new Bazooka(), new Rocket() };
//////// Here goes some other code for the game //////
//////// until the player enters the gas room ////////
myGasRoom.ApplyGasEffect(myCharacter.Weapons)