0

请帮我重构以下代码场景

我有两节课

class Example1
{
    public string username {private get;set;}
    public string password {private get;set;}
    public  obj[] callfunction(string value) {//code}
}
class Example2
{
    public string UserName {private get;set;}
    public string Password {private get;set;}
    public List<ObjectDefinition> function1(string option) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(option)
        //Other codes
    }
    public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(other)
        //Other codes
    }
    public List<ObjectDefinition> function3(string option) 
    {
        Example1 obj = new Example1();
        obj.username = this.UserName ;
        obj.password = this.Password;
        obj.callfunction(option)
        //Other codes
    }
    //so on
}

所以我的问题是声明这个重复对象声明的最佳方式。

4

2 回答 2

2

添加要创建的函数Example1

class Example2
{
    public string UserName {private get;set;}
    public string Password{private get;set;}
    public List<ObjectDefinition> function1(string option) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    public List<ObjectDefinition> function3(string option) 
    {
        var example1 = CreateExample1(option);
        example1.callfunction(option); 
        //Other codes
    }
    //so on

    public Example1 CreateExample1(string option)
    {
        Example1 obj=new Example1();
        obj.username =this.UserName ;
        obj.password=this.Password;
        return obj;     
    }
}
于 2013-10-04T12:30:31.650 回答
1

如何定义一个函数,该函数将返回带有所需用户名和密码的 Example1 实例:

class Example1
{
   public string username {private get;set;}
   public string password{private get;set;}
   public  obj[] callfunction(string value){//code}
}
class Example2
{
   public string UserName {private get;set;}
   public string Password{private get;set;}
   public List<ObjectDefinition> function1(string option) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(option)
    //Other codes
   }
   public List<ObjectDefinition> function2(string option,string other,string other_more_params) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(other)
    //Other codes
   }
   public List<ObjectDefinition> function3(string option) 
   {
    Example1 obj=GetExample1Instance();
    obj.callfunction(option)
    //Other codes
   }

   private Example1 GetExample1Instance()
   {
    return new Example1 { username = this.UserName, password = this.Password }
   }
   //so on
}

或者您可以考虑简单地将 Example1 的实例作为 Example2 中的成员(假设所需的功能支持这一点)。

于 2013-10-04T12:36:25.380 回答