zoukankan      html  css  js  c++  java
  • c#中结构的转化示例

    c#中结构的转化示例

    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);
        }

    }

    邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。
    助人等于自助!  3w@live.cn
  • 相关阅读:
    ccmenu里的位置
    【luogu P3346】诸神眷顾的幻想乡(广义 SAM)
    Snow的追寻(线段树)(LCA)
    【bzoj 4303】数列 / T4(K-D tree)
    选课 / T3(组合数)(容斥)
    随机游走 / T1(期望)(树形DP)
    【luogu P3898】期望异或 / T3 / 大新闻(数位DP)(数学)
    【luogu P7295】Paint by Letters P(前缀和)(欧拉公式)(bfs)(对偶图)
    【luogu P7294】Minimum Cost Paths P(二分)(单调栈)(斜率)
    【luogu P7293】Sum of Distances P(线段树)(图论)
  • 原文地址:https://www.cnblogs.com/downmoon/p/1019250.html
Copyright © 2011-2022 走看看