网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C#学习笔记(4).
.C# 3.0语言详解之基本的语言增强.
.C# 2.0 套接字编程实例初探.
.C# 3.0新特性初步研究 Part5:匿名.
.数据结构与算法(C#实现)系列---演.
.数据结构与算法(C#实现)系列---广.
.C#中结构与类的区别.
.关于两代语言.C/C++,java/c#.
.C#语言初级入门(1).
.挑战C#学习的最快速度.
.c#里面的namespace基础(二).
.SUNWEN教程之----C#进阶(二).
.c#学习笔记(1).
.C#的多线程机制初探(1) .
.在c#中执行sql语句时传递参数的小.
.C# 3.0新特性初步研究 Part3:使用.
.C#,自然的进步.
.Excel 2007单元格及内容的合并、.
.C#正则表达式应用范例.
.C# Namespace.

SUNWEN教程之----C#进阶(九)

发表日期:2003-12-29


大家好,我是SUNWEN,现在是五月四号23:15,再过十五分钟就要熄灯了.所以要抓紧时间,先开个头,明天继续.

现在我要说的是C#中的用户自定义转换(User-Defined Conversions),其中用到了前面说的struct的知识,就是结构呀,忘了吗?好,没忘就好.从我们以下的课程我们可以看到结构的用处(刚才我还在想它有什么用,呵呵).用class声明的是一个类,而用struct声明的可以看作是一个类型,对,就是像C#自带的int,short,long那样的类型了.

C#中可以允许我们对结构(struct)和类(class)进行转换,所以我们可以在其中定义一些转换.但是,C#规定,所有的转换声明都必须在显示(explicit)和隐示(implicit)中选择一个.比方说,我们用这个语句的时候
int a=10;
System.Console.println(a):
就用到了int的隐示的转换toString.如果是(String)a,就叫做显示.所以,显/隐之差就在于是否表现出来.大家现在肯定还是一头雾水,等到明天我把例子写出来再分析一下就清楚了,要熄灯了,我先走一步了!


喔~~~~~终于起来了,五月五日8:45.下面给出例子,在这个例子中,一个名为RomanNumeral的类型被声明,然后对他实施了好几种转换.

000: // UserConversions\conversion.cs
001: using System;
002:
003: struct RomanNumeral
004: {
005: public RomanNumeral(int value)
006: {
007: this.value = value;
008: }
009: static public implicit operator RomanNumeral(int value)
010: {
011: return new RomanNumeral(value);
012: }
013: static public explicit operator int(RomanNumeral roman)
014: {
015: return roman.value;
016: }
017: static public implicit operator string(RomanNumeral roman)
018: {
019: return("Conversion not yet implemented");
020: }
021: private int value;
022: }
023:
024: class Test
025: {
026: static public void Main()
027: {
028: RomanNumeral numeral;
029:
030: numeral = 10;
031:
032: // 显式地从numeral到int的转换033: Console.WriteLine((int)numeral);
034:
035: // 隐示地转换到string036: Console.WriteLine(numeral);
037:
038: // 显示地转换到int,然后显示地转换到short040: short s = (short)numeral;
041:
042: Console.WriteLine(s);
043:
044: }
045: }
这个例子子的输出是:

10
Conversion not yet implemented
10
注意009和013的operator操作符,它是一个转换操作符.static public explicit operator int(RomanNumeral roman),记住这样的形式,它就代表了一个转换.再看第033行,因为在前面int这个转换被声明成了explicit,即显示地,所以,在使用这个转换时,必须用括号.

下面再给出一个例子,这个例子声明了两个结构,RomanNumeral和BinaryNumeral,然后在它们之间进行转换.

000: // UserConversions\structconversion.cs
001: using System;
002:
003: struct RomanNumeral
004: {
005: public RomanNumeral(int value) { this.value = value; }
006: static public implicit operator RomanNumeral(int value)
007: {return new RomanNumeral(value);}
008: static public implicit operator
009: RomanNumeral(BinaryNumeral binary)
010: {return new RomanNumeral((int)binary);}
011: static public explicit operator int(RomanNumeral roman)
012: {return roman.value;}
013: static public implicit operator string(RomanNumeral roman)
014: {return("Conversion not yet implemented");}
015: private int value;
016: }
017:
018: struct BinaryNumeral
019: {
020: public BinaryNumeral(int value) {this.value = value;}
021:
022: static public implicit operator BinaryNumeral(int value)
023: {return new BinaryNumeral(value);}
024: static public implicit operator string(BinaryNumeral binary)
025: {return("Conversion not yet implemented");}
026: static public explicit operator int(BinaryNumeral binary)
027: {return(binary.value);}
028:
029: private int value;
030: }
031:
032: class Test
033: {
034: static public void Main()
035: {
036: RomanNumeral roman;
037: roman = 10;
038: BinaryNumeral binary;
039: binary = (BinaryNumeral)(int)roman;
040: roman = binary;
041: Console.WriteLine((int)binary);
042: Console.WriteLine(binary);
043: }
044: }
这个例子的输出是:

10
Conversion not yet implemented
注意,第039行并没有直接由RomanNumeral转化成BinaryNumeral,因为没有直接的转换提供.所以先把RomanNumeral转换成int,再转成BinaryNumeral.其余的东西跟上面的例子是一样的(至少我这么认为),如果上面的例子理解了,下面的就好了.

OK,又完了一节,学了这么多,大家有什么感觉呢,欢迎和我交流,mrfat@china.com

 

上一篇:SUNWEN教程之----C#进阶(八) 人气:10566
下一篇:SUNWEN教程之----C#进阶(十) 人气:10811
浏览全部SUNWEN教程的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐