日期:2014-05-18  浏览次数:20467 次

[共400分+来者有分+求助]如何实现类的不同构造函数拥有不同的方法?
我的问题是:
比如:
public   class   a
{
    private   int   _Id;
   
    //构造函数
    public   a()
    {
 
    }

    public   a(int   paramInt)
    {
        this._Id=paramInt
    }
   
    public   int   UpDate()
    {
        //其他代码
        /*我的目的:我想在使用这个类的时候,a=new   a();这个时候只能使用Update()这个方法,而下面这个Delete()方法不能使用.(最好是在a点的时候智能提示不显示Delete()这个方法.*/
    }
    public   int   Delete()
    {
  //其他代码
        /*我的目的:我想在使用这个类的时候,a=new   a(1);这个时候只能使用Delete()
这个方法,而上面那个UpDate()方法不能使用.(最好是在a点的时候智能提示不显示UpDate()这个方法.*/

    }
}


简单的说:
就是:
如果使用a=new   a()实例化的时候,只能使用UpDate()方法(最好是在a点的时候智能提示不显示Delete()这个方法).

如果使用a=new a(1)实例化的时候,只能使用Delete()方法(最好是在a点的时候智能提示不显示UpDate()这个方法).

不知道可以实现不呢?

请各位大哥大姐帮帮忙,3Q!


还有我发个一个同样的帖子200分:
http://community.csdn.net/Expert/topic/5649/5649429.xml?temp=.1706964


来者有分哈!

------解决方案--------------------
曲线救国


public class A
{
private int _Id;
private A _A = null;

public A()
{
_A = new A1();
}

public A(int paramInt)
{
this._Id = paramInt;
_A = new A2(paramInt);
}

public int UpDate()
{
if (_A is A2)
{
throw new Exception( "Update方法不能用 ");
}
}

public int Delete()
{
if (_A is A1)
{
throw new Exception( "Delete方法不能用 ");
}
}
}

public class A1 : A
{
}

public class A2 : A
{
}
------解决方案--------------------
大家都来探讨阿!
------解决方案--------------------
怎么没人呢?

------解决方案--------------------
每个都建类
------解决方案--------------------
类似这样

public a()
{
a(true);
}

public a(bool create)
{
if (create)
UpDate();
else
Delete();
}
------解决方案--------------------
构造函数重载
通过设置不同参数对构造函数重载
比如public a()
{}
也可以
public a(string str1)
{}
还可以public a(string str1, int i)
{}
etc..

------解决方案--------------------
帮顶!
------解决方案--------------------
那你在写够造函数的时候,给UpDate()不带参数,给Delete()带一个int i 的参数可以不,
就是这样
public int UpDate(){}

public int Delete(int i){}


------解决方案--------------------

------解决方案--------------------
Class1 sd = new Class1(true);
Response.Write(sd.Delete());

-------------------------