阿康的博客

我只要一滴水,神却给了我一个海洋,叫我如果取舍捏。
一个简单的类实现
我们平时做网站,常会用到一个表的显示,我们来看一个一个表格

一般它们的左边和顶部的单元格样式和内容会有不同,不如我们自已做一个表格的类
用来显示数据吧
再看一个真实的表格

大家一看就明白,这个表格可以说是一个最有代表性的了
它只有两种<tr> 一个是顶部的第一个标题栏,还有一个就是下面的重复单元了
我们就构造一个TABLE类,这个类,由两种<TR>类构成 还可以再构造TD类,但那样做太浪费了

第一类TR 类 head_tr
using System;
using System.Collections;

namespace new_job
{
    
/// <summary>
    
/// tr 和 table td 一起用于生成表格。
    
/// </summary>

    public class head_tr
    
{
        
public string attr; //这个<tr>的属性 应刻实现为属性 我愉懒了
        private int count_td; //<td>的数目
        private ArrayList td_text; //这个列表用来保存TD的内容
        private ArrayList td_attr; //这个用来保存TD的属性
        public head_tr(string in_attr,int in_num,string[] in_td_attr,string[] in_td_text)        //构造函数 这是一个完整的构造函数
        {
            
this.attr = " " + in_attr; //属性前加一空格,下同
            this.td_text = new ArrayList();
            
this.td_attr = new ArrayList();
            
this.count_td = in_num;
            
for(int i = 0; i < in_num; i++)
            
{
                
this.td_text.Add(in_td_text[i]);
                
this.td_attr.Add(" " + in_td_attr[i]);
            }

        }

        
public head_tr(int in_num,string in_attr) //也是构造有in_num个TD的TR 但指定tr属性
        {
            
this.td_text = new ArrayList();
            
this.td_attr = new ArrayList();
            
this.count_td = in_num;
            attr 
= " " + in_attr;
            
for(int i = 0; i < in_num; i++)
            
{
                
this.td_text.Add("");
                
this.td_attr.Add("");
            }

        }

        
public head_tr(int in_num) //也是构造有in_num个TD的TR 但指定tr属性
        {
            
this.td_text = new ArrayList();
            
this.td_attr = new ArrayList();
            
this.count_td = in_num;
            
this.attr = "";
            
for(int i = 0; i < in_num; i++)
            
{
                
this.td_text.Add("");
                
this.td_attr.Add("");
            }

        }


        
public string this[int index] //为了方便可以构造索引
        {
            
get
            
{
                
if(index >0 && index<count_td)
                
{
                    
return ((string)td_text[index]);
                }

                
else
                
{
                    
return null;
                }


            }

            
set
            
{
                
if(index>0 && index<count_td)
                
{
                    td_text[index] 
= value;
                }

            }


        }

        
public string to_str()            //输出TR的串
        {
            
string ret_str;
            ret_str 
= "<tr" + attr + ">";    //属性前有空格不用加了:-)
            for (int i=0; i < count_td; i++)
            
{
                ret_str 
+= "<td" + ((string)td_attr[i]) + ">" + ((string)td_text[i]) + "</td>";
            }

            ret_str 
+= "</tr>\n";
            
return ret_str;
        }


    }

}

这是一个完整的head_tr 这个很好实现 它功作的很好,以下是测试代码
using System;
namespace new_job
{
    
public class child
    
{
        
public static void Main(String[] s)
        
{
            head_tr tr1 
= new head_tr(4);
            head_tr tr2 
= new head_tr(4,"class=\"bluefont\"");
            
string[] tdstyle = new string[4]{"width=\"30%\"","width=\"40%\"","width=\"50%\"","width=\"60%\""};
            
string[] tdcon = new string[4]{"学号","性别","年龄","生日"};
            head_tr tr3 
= new head_tr("class=\"smalltr\"",4,tdstyle,tdcon);
            Console.WriteLine(tr1.to_str());
            Console.WriteLine(tr2.to_str());
            Console.WriteLine(tr3.to_str());
        }

    }

}

posted on 2005-08-08 19:46  我的博客  阅读(435)  评论(1)    收藏  举报