动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   程序开发 → 阅读文章

 C# 程序员参考--用户定义的转换教程

作者来源: 
阅读 数 78 人次 , 2006-5-10 10:11:00 

本教程展示如何定义转换以及如何在类或结构之间使用转换。

示例文件

请参见“用户定义的转换”示例以下载和生成本教程中讨论的示例文件。

教程

C# 允许程序员在类或结构上声明转换,以便可以使类或结构与其他类或结构或者基本类型相互进行转换。转换的定义方法类似于运算符,并根据它们所转换到的类型命名。

在 C# 中,可以将转换声明为 implicit(需要时自动转换)或 explicit(需要调用转换)。所有转换都必须为 static,并且必须采用在其上定义转换的类型,或返回该类型。

本教程介绍两个示例。第一个示例展示如何声明和使用转换,第二个示例演示结构之间的转换。

示例 1

本示例中声明了一个 RomanNumeral 类型,并定义了与该类型之间的若干转换。

// conversion.cs
using System;

struct RomanNumeral
{
    public RomanNumeral(int value) 
    { 
       this.value = value; 
    }
    // Declare a conversion from an int to a RomanNumeral. Note the
    // the use of the operator keyword. This is a conversion 
    // operator named RomanNumeral:
    static public implicit operator RomanNumeral(int value) 
    {
       // Note that because RomanNumeral is declared as a struct, 
       // calling new on the struct merely calls the constructor 
       // rather than allocating an object on the heap:
       return new RomanNumeral(value);
    }
    // Declare an explicit conversion from a RomanNumeral to an int:
    static public explicit operator int(RomanNumeral roman)
    {
       return roman.value;
    }
    // Declare an implicit conversion from a RomanNumeral to 
    // a string:
    static public implicit operator string(RomanNumeral roman)
    {
       return("Conversion not yet implemented");
    }
    private int value;
}

class Test
{
    static public void Main()
    {
        RomanNumeral numeral;

        numeral = 10;

// Call the explicit conversion from numeral to int. Because it is
// an explicit conversion, a cast must be used:
        Console.WriteLine((int)numeral);

// Call the implicit conversion to string. Because there is no
// cast, the implicit conversion to string is the only
// conversion that is considered:
        Console.WriteLine(numeral);
 
// Call the explicit conversion from numeral to int and 
// then the explicit conversion from int to short:
        short s = (short)numeral;

        Console.WriteLine(s);
    }
}

输出

10
Conversion not yet implemented
10

示例 2

本示例定义 RomanNumeralBinaryNumeral 两个结构,并演示二者之间的转换。

// structconversion.cs
using System;

struct RomanNumeral
{
    public RomanNumeral(int value) 
    {
        this.value = value; 
    }
    static public implicit operator RomanNumeral(int value)
    {
        return new RomanNumeral(value);
    }
    static public implicit operator RomanNumeral(BinaryNumeral binary)
    {
        return new RomanNumeral((int)binary);
    }
    static public explicit operator int(RomanNumeral roman)
    {
         return roman.value;
    }
    static public implicit operator string(RomanNumeral roman) 
    {
        return("Conversion not yet implemented");
    }
    private int value;
}

struct BinaryNumeral
{
    public BinaryNumeral(int value) 
    {
        this.value = value;
    }
    static public implicit operator BinaryNumeral(int value)
    {
        return new BinaryNumeral(value);
    }
    static public implicit operator string(BinaryNumeral binary)
    {
        return("Conversion not yet implemented");
    }
    static public explicit operator int(BinaryNumeral binary)
    {
        return(binary.value);
    }

    private int value;
}

class Test
{
    static public void Main()
    {
        RomanNumeral roman;
        roman = 10;
        BinaryNumeral binary;
        // Perform a conversion from a RomanNumeral to a
        // BinaryNumeral:
        binary = (BinaryNumeral)(int)roman;
        // Performs a conversion from a BinaryNumeral to a RomanNumeral.
        // No cast is required:
        roman = binary;
        Console.WriteLine((int)binary);
        Console.WriteLine(binary);
    }
}

输出

10
Conversion not yet implemented

代码讨论

  • 在上个示例中,语句
    binary = (BinaryNumeral)(int)roman;

    执行从 RomanNumeralBinaryNumeral 的转换。由于没有从 RomanNumeralBinaryNumeral 的直接转换,所以使用一个转换将 RomanNumeral 转换为 int,并使用另一个转换将 int 转换为 BinaryNumeral

  • 另外,语句
    roman = binary;

    执行从 BinaryNumeral RomanNumeral 的转换。由于 RomanNumeral 定义了从 BinaryNumeral 的隐式转换,所以不需要转换。

  
 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:C# 程序员参考--运算符重载教程
· 下一篇:C# 程序员参考--索引属性教程
· EJB轻松进阶之八
· 详解Java规则引擎与其API
· C# 程序员参考--条件方法教程
· Eclipse开发工具使用指南
· RSA中UML建模元素的扩展与定制


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077