zoukankan      html  css  js  c++  java
  • C# 高级编程 里面的委托例子。

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        public class Currency

        {

            public uint Dollars;

            public ushort Cents;

            public Currency()

            {

            }

            public Currency(uint dollars,ushort cents)

            {

                this.Dollars = dollars;

                this.Cents = cents;

            }

            public override string ToString()

            {

                return string.Format("{0:c}.{1,-2:00}", Dollars, Cents);

            }

            public static string GetCurrencyUint()

            {

                return "人民币";

            }

            public static explicit operator Currency(float value)

            {

                checked

                {

                    uint dollars = (uint)value;

                    ushort cents =(ushort) ((value - dollars)*100);

                    return new Currency(dollars, cents);

                }

            }

            public static implicit operator float(Currency value)

            {

                return value.Dollars + value.Cents / 100.0f;

            }

            public static implicit operator Currency(uint value)

            {

                return new Currency(value, 0);

            }

            public static implicit operator uint(Currency value)

            {

                return value.Dollars;

            }

        }

        delegate string GetAString();

        

        class Program

        {

            static void Main(string[] args)

            {

                int x = 40;

                GetAString firstStringMethod = x.ToString;

                Console.WriteLine("String is {0}",firstStringMethod());

             

                Currency balance = new Currency(34, 50);

                firstStringMethod = balance.ToString;

                Console.WriteLine("String is {0}", firstStringMethod());

                firstStringMethod = new GetAString(Currency.GetCurrencyUint);

                Console.WriteLine("String is {0}",firstStringMethod());

            }

        }

    }

    //

     // /* 以下是运行的结果. String is 40 String is ¥34.50 String is 人民币请按任意键继续. . . */

  • 相关阅读:
    转 c#性能优化秘密
    转 the best for wcf client
    迁移到简书通知
    docker初体验
    PyQT 单词弹幕桌面生成!
    Python 常见排序查找算法-二分法,归并,冒泡,插入
    Python 将小姐姐画在Excel上
    TensorFlow基于神经网络实现井字棋
    TensorFlow(八) TensorFlow图像识别(KNN)
    TensorFlow(七) 地址匹配
  • 原文地址:https://www.cnblogs.com/fat_li/p/1863315.html
Copyright © 2011-2022 走看看