开发者

How to create object using Interface in Specific Case

开发者 https://www.devze.com 2023-01-20 03:26 出处:网络
I have question connected with interfaces and abstract classes. I\'ll give to you simple example, that could explain what I want to do. So, Lets start.

I have question connected with interfaces and abstract classes. I'll give to you simple example, that could explain what I want to do. So, Lets start.

public interface A
{
 string param1 { set; get;}
 string param1 { set; get;}
 A CreateObject(string p1,string p2);
}

public class MyClass1 : A
{
 public string param1 { set; get; }
 public string param2 { set; get; }
 public A CreateObject(string p1,string p2)
 {
  var obj = new MyClass1();
  obj.param1 = p1;
  obj.param2 = p2;
  return obj;
 }

}
public class MyClass2 : A
{
 public string param1 { set; get; }
 public string param2 { set; get; }
 public A CreateObject(string p1,string p2)
 {
  var obj = new MyClass2();
  obj.param1 = p1;
  obj.param2 = p2;
  return obj;
 }

}


// I have little problem with this function
public List<A> GetNodes(int count)
{
  var lst_Objects = new List<a>();
                for(int i=0; i<count; i++)
                {
           string Param1 = GetParam1();
           string Param2 = GetParam2();
                        lst_Objects.Add(new A.CreateObject(Param1,Param2); // but it defenitly doesn't work(wrong way)
       开发者_Go百科         }
             return lst_Objects;
}

I have problems with GetNodes function. Tip: MyClass1 and MyClass2 is Entity objects, and because of this reason I can not create abstract class, and use some generic to resolve this problem.

I will grateful for your ideas


You didn't mention A.CreateObject as static while other concrete class MyClass1 / MyClass2 are left as static. Use

lst_Objects.Add(MyClass1.CreateObject(Param1,Param2);

or

lst_Objects.Add(MyClass2.CreateObject(Param1,Param2);

instead.

Also to mention, you need to make sure you define A.CreateObject in both of the classes, otherwise you need to make both of them abstract which is not what you want. Rather remove the nonstatic method CreateObject from interface A.


Your interface declares a CreateObject method, which neither of your classes implement. Your classes implement a static CreateObject, which does not satisify the interface. Interfaces cannot declare static members.


I have bad solution. I'll create class like this:

public class Universal: A
{
 public string param1 { set; get; }
 public string param2 { set; get; }
 public static A CreateObject(string p1,string p2)
 {
  var obj = new MyClass1();
  obj.param1 = p1;
  obj.param2 = p2;
  return obj;
 }

Function GetNodes will have appearance like this:

public List<A> GetNodes(int count)
{
  var lst_Objects = new List<a>();
                for(int i=0; i<count; i++)
                {
                        string Param1 = GetParam1();
                        string Param2 = GetParam2();
                        lst_Objects.Add(Universal.CreateObject(Param1 ,Param2)); 
                }
             return lst_Objects;
}

This function will return List of objects which have type A(which I easily convert into myclass1 or myclass2 object.

Is it good Idea?

Thank for your attention.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号