接口正确实现 [英] Interface correct implementation

查看:75
本文介绍了接口正确实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道是否有人可以提供帮助。我正在编写一个应用程序,并想检查我是否正确使用接口。



我有一个包含许多属性的界面。



  public   interface 详情{
string 名称{ get ; set ;}
int 年龄{获取; 设置;}
}





我还有另外两个实现此接口的类,然后包含自己独特的属性。



我有另一个界面:

  public   interface  UserManager {
void CreateUser(详细用户);
}



当我来实施CreateUser时,我是否需要施放ie



< pre lang =C#> public void CreateUser(详细用户){
TestClass1 = user as TestClass1;
Console.Write(user.Name + + user.Age + + user.TestClass1Property);
}





  public   void  CreateUser(详细用户){
TestClass2 = user as TestClass2;
Console.Write(user.Name + + user.Age + + user.TestClass2Property);
}





这是正确的做法吗?



我尝试了什么:



我已经尝试了以上内容它似乎工作正常,我只想检查这是一个好习惯我已经这样做了吗?

解决方案

您应该遵循接口的命名约定并用I作为前缀: IDetail 等。



您不应将用作 -cast。或者更好:您的类/接口设计不应该要求它。如果它确实需要它,那么接口的整个点就会丢失在类设计上:接口用于类之间的松耦合,你的演员重新引入了与可能失败的附加陷门的强耦合。


F-ES Sitecore是对的...但也错了,有点。

你不能使用这段代码:

  public   void  CreateUser(详细用户){
TestClass1 = user as TestClass1;
Console.Write(user.Name + + user.Age + + user.TestClass1Property);
}

因为TestClass1Property不是Detail Interface的成员,并且因为传入的对象必须是Detail的成员,但不必是TestClass1的成员,无法通过用户变量访问任何TestClass1特定字段,属性,事件或方法。如果你尝试,你会得到编译错误。

你需要做的是:

  public   interface  IDetail 
{
string 名称{ get ; set ; }
int 年龄{ get ; set ; }
}
public class TestClass1:IDetail
{
public int TestClass1Property { get ; set ; }
}
...
public void CreateUser(IDetail) user)
{
TestClass1 tc = user as TestClass1;
if (tc!= null
{
Console。写(user.Name + + user.Age + + tc.TestClass1Property);
}
}



 ... 
Console.Write(tc.Name + < span class =code-string> + tc.Age + < span class =code-string> + tc.TestClass1Property);
...


  public   void  CreateUser(详细用户){
TestClass1 = user as TestClass1;
Console.Write(user.Name + + user.Age + + user.TestClass1Property);
}





上面的代码意味着该方法允许任何实现Detail的类传递给函数,但是as表示虽然可以传入任何实现Details的类,但如果它不是TestClass1,则as将计算为null。所以,不,你应该做任何演员或使用as。



  public   void  CreateUser(详细用户){
Console.Write(user.Name + + user.Age + + user.TestClass1Property);
}





编辑:上面的代码是垃圾,我没有看到你试图访问TestClass1Property关闭用户,请忽略它。



此外,您应在界面前加上I,因此IDetail而不是Detail。这是正确的约定,当你处理一个接口而不是一个具体的类时,它就显而易见了。


Hi,
I was wondering if any one could help. I'm writing an application and wanted to check if I am using interfaces correctly.

I have an interface which contains a number of properties.

public interface Detail {
  string Name {get;set;}
  int Age {get;set;}
}



I have two other classes that implement this interface which then contains there own unique properties.

I have another interface:

public interface UserManager {
   void CreateUser(Detail user);
}


When I come to implement CreateUser, do i need to cast ie

public void CreateUser(Detail user){
TestClass1 = user as TestClass1;
Console.Write(user.Name + " " + user.Age + " "+  user.TestClass1Property);
}



public void CreateUser(Detail user){
TestClass2 = user as TestClass2;
Console.Write(user.Name + " " + user.Age + " "+  user.TestClass2Property);
}



Is this the correct approach?

What I have tried:

I have tried the above and it appears to work ok, I just want to check is this good practice the way I have done this?

解决方案

You should follow the naming convention for interfaces and prefix them with an "I": IDetail, etc.

You shouldn't use the as-cast. Or better: Your class/interface design shouldn't require it. If it does require it then the whole point of interfaces is lost on your class design: Interfaces serve the purpose of loose coupling between classes, your cast re-introduces strong coupling with the added trapdoor of potentially failing.


F-ES Sitecore is right...but also wrong, a little.
You can't use this code:

public void CreateUser(Detail user){
    TestClass1 = user as TestClass1;
    Console.Write(user.Name + " " + user.Age + " "+  user.TestClass1Property);
    }

Because TestClass1Property is not a member of the Detail Interface, and since the object you pass in must be a member of Detail, but doesn't have to be a member of TestClass1, you can't access any TestClass1 specific fields, properties, event, or methods via the user variable. And you will get a compilation error if you try.
What you would need to do is:

       public interface IDetail
            {
            string Name { get; set; }
            int Age { get; set; }
            }
        public class TestClass1 : IDetail
            {
            public int TestClass1Property { get; set; }
            }
...
        public void CreateUser(IDetail user)
            {
            TestClass1 tc = user as TestClass1;
            if (tc != null)
                {
                Console.Write(user.Name + " " + user.Age + " " + tc.TestClass1Property);
                }
            }

Or

...
                Console.Write(tc.Name + " " + tc.Age + " " + tc.TestClass1Property);
...


public void CreateUser(Detail user){
TestClass1 = user as TestClass1;
Console.Write(user.Name + " " + user.Age + " "+  user.TestClass1Property);
}



The above code means that the method allows any class that implements Detail to be passed to the function, however your "as" means that while any class that implements Details can be passed in, if it isn't TestClass1 then the "as" will evaluate to null. So, no, you should do any casting or use "as".

public void CreateUser(Detail user){
Console.Write(user.Name + " " + user.Age + " "+  user.TestClass1Property);
}



Edit: The above code is rubbish, I didn't see you were trying to access TestClass1Property off of user so ignore it.

Also you should prefix your interfaces with "I" so "IDetail" rather than "Detail". It is proper convention and makes it obvious when you're dealing with an interface and not a concrete class.


这篇关于接口正确实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆