您的位置:程序门 -> .net技术 -> c#



怎样实现多个参数(多态?),比如一个函数调用的时候可以写一个,也可以写两个参数


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎样实现多个参数(多态?),比如一个函数调用的时候可以写一个,也可以写两个参数
发表于:2007-07-09 15:35:06 楼主
比如文章标题,有三个参数:长度,颜色,加粗。有时候只需要其中的一个设置,怎样实现这样的功能呢?我以前事分成两格函数来写的,但觉得不合理,有高手指点一下吗?十分感谢,代码如下:

        protected   string   formatstringl(string   title)
        {
                if   (title.length   >   15)
                {
                        title   =   title.substring(0,   15)   +   ".. ";
                }
                return   title;
        }

        protected   string   formatspecial(string   title,   string   titlecolor,   bool   isbold)
        {
                if   (title.length   >   15)
                {
                        title   =   title.substring(0,   15)   +   ".. ";
                }
                if   (titlecolor   !=   " "   ¦ ¦   titlecolor   !=   null)
                {
                        title   =   " <font   color= ' "   +   titlecolor   +   " '> "   +   title   +   " </font> ";
                }
                if   (isbold   ==   true)
                {
                        title   =   " <b> "   +   title   +   " </b> ";
                }
                return   title;
        }
发表于:2007-07-09 15:37:111楼 得分:0
用param关键字声明参数...
发表于:2007-07-09 15:39:412楼 得分:0
这样应该更好一些吧。。代码的重用率比较高。呵呵
protected   string   formatspecial(string   title)
{
if   (title.length   >   15)
{
        title   =   title.substring(0,   15)   +   ".. ";
}
return   title;
}

protected   string   formatspecial(string   title,   string   titlecolor,   bool   isbold)
{
              formatspecial(title);

        if   (titlecolor   !=   " "   ¦ ¦   titlecolor   !=   null)
        {
                title   =   " <font   color= ' "   +   titlecolor   +   " '> "   +   title   +   " </font> ";
        }
        if   (isbold   ==   true)
        {
                title   =   " <b> "   +   title   +   " </b> ";
        }
        return   title;
}
发表于:2007-07-09 15:42:573楼 得分:0
是params:
如下:

protected   string   formatspecial(params   string[]   args)
{

}

可以如下调用:
formatspecial( "1 ");
formatspecial( "1 ",   "2 ");
formatspecial( "1 ",   "2 ",   "3 ");

楼主的那种定义显然是合理的,可读性好...
但这种方式明显不合适...


发表于:2007-07-09 15:46:454楼 得分:0
方法的重载呗
发表于:2007-07-09 15:52:025楼 得分:0
楼主的写法已经挺好的了,还可以采用commandos(孤独求胜)的写法
不建议采用楼上的写法
发表于:2007-07-09 15:52:276楼 得分:0
2楼正解。
发表于:2007-07-09 15:54:257楼 得分:0
三个都给默认值,需要哪个传哪个,不用的就用默认值代替
发表于:2007-07-09 16:01:268楼 得分:0
protected   string   formatstringl(string   title)
                {
                        formatspecial(title,   null,   false);
                }

                protected   string   formatspecial(string   title,   string   titlecolor,   bool   isbold)
                {
                        if   (title.length   <=   15)
                        {
                                return   title;
                        }
                        title   =   title.substring(0,   15)   +   ".. ";

                        if   (titlecolor   !=   null   &&   !string.isnullorempty(titlecolor))
                        {
                                title   =   " <font   color= ' "   +   titlecolor   +   " '> "   +   title   +   " </font> ";
                        }
                        if   (isbold   ==   true)
                        {
                                title   =   " <b> "   +   title   +   " </b> ";
                        }
                        return   title;
                }
发表于:2007-07-09 16:02:569楼 得分:0
protected   string   formatstringl(string   title)
                {
                        formatspecial(title,   null,   false);
                }

                protected   string   formatstringl(string   title,   string   titlecolor,   bool   isbold)
                {
                        if   (title.length   <=   15)
                        {
                                return   title;
                        }
                        title   =   title.substring(0,   15)   +   ".. ";

                        if   (titlecolor   !=   null   &&   !string.isnullorempty(titlecolor))
                        {
                                title   =   " <font   color= ' "   +   titlecolor   +   " '> "   +   title   +   " </font> ";
                        }
                        if   (isbold   ==   true)
                        {
                                title   =   " <b> "   +   title   +   " </b> ";
                        }
                        return   title;
                }
发表于:2007-07-09 16:05:3110楼 得分:0
抱歉,以再写错,嘎嘎,下面这样就已经可以了,函数重载

protected   string   formatstringl(string   title)
                {
                        formatstringl(title,   null,   false);
                }

                protected   string   formatstringl(string   title,   string   titlecolor,   bool   isbold)
                {
                        if   (title.length   <=   15)
                        {
                                return   title;
                        }
                        title   =   title.substring(0,   15)   +   ".. ";

                        if   (titlecolor   !=   null   &&   !string.isnullorempty(titlecolor))
                        {
                                title   =   " <font   color= ' "   +   titlecolor   +   " '> "   +   title   +   " </font> ";
                        }
                        if   (isbold   ==   true)
                        {
                                title   =   " <b> "   +   title   +   " </b> ";
                        }
                        return   title;
                }
发表于:2007-07-09 16:56:0411楼 得分:0
谢谢各位的热心解答,我改了一下,可以实现三格方式的任意组合,代码分享一下:

                //格式化标题
                public   static   string   str(string   title)
                {
                        str(title,   null,   false);  
                        return   title;
                }

                public   static   string   str(string   title,   string   color)   {
                        str(title,   color,   false);
                        return   title;
                }

                public   static   string   str(string   title,   bool   isbold)
                {
                        str(title,   null,   isbold);
                        return   title;
                }

                public   static   string   str(string   title,   string   color,   bool   isbold)
                {
                        if   (title.length   >   15)
                        {
                                title   =   title.substring(0,   15)   +   ".. ";
                        }
                        if   (!string.isnullorempty(color))
                        {
                                title   =   " <font   color= ' "   +   color   +   " '> "   +   title   +   " </font> ";
                        }
                        if   (isbold   ==   true)
                        {
                                title   =   " <b> "   +   title   +   " </b> ";
                        }
                        return   title;
                }
发表于:2007-07-09 17:37:4612楼 得分:0
哎,你已经想到了,我就不说了,我还想说,还不如写三个单独的函数呢。
发表于:2007-07-09 17:49:0813楼 得分:0
to:fuda_1985

为什么你说不如写成三个单独的函数呢?

有什么更好的方法呢
发表于:2007-08-01 14:45:3514楼 得分:0
晕。这哪是多态。。。。用函数重载吧!!!!!
发表于:2007-08-01 14:52:3815楼 得分:0
重载就可以了~~
发表于:2007-08-01 17:06:5916楼 得分:0
方法重载


快速检索

热门点击