zoukankan      html  css  js  c++  java
  • 特性与元数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace chap2_1_7
    {
        class Program
        {
            static void Main(string[] args)
            {
                HR hr = new HR();
                Employee employee = new Employee();
                hr.ToSalary(employee);
                Console.ReadKey();
            }
        }
        public enum TransferSourceType//转账类型
        { 
            Salary,
            Reimburse,
            Loan
        }
        [AttributeUsage(AttributeTargets.Parameter)]
        public class TransferSource : Attribute//转账元数据
        {
            public TransferSourceType TransferType { get; set; }
        }
        public partial class Employee//员工实体
        {
            public void PaySalary([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            { 
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:"+toNumber);
            }
            public void PayReimburse([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            {
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:" + toNumber);
            }
            public void PayLoan([TransferSource(TransferType = TransferSourceType.Salary)] int toNumber)
            {
                //直接汇入员工银行卡
                Console.WriteLine("收到工资:" + toNumber);
            }
        }
        public class HR
        {
            public void ToSalary(Employee employee)
            {
                var transferSource = typeof(Employee).GetMethod("PaySalary").GetParameters()[0].GetCustomAttributes(false)[0] as TransferSource;
                switch (transferSource.TransferType)
                {
                    case TransferSourceType.Salary:
                        {
                            employee.PaySalary(6000);//发工资
                        }break;
                    case TransferSourceType.Reimburse:
                        {
                            employee.PayReimburse(500);//报销                        
                        }break;
                    case TransferSourceType.Loan:
                        {
                            employee.PayLoan(20000);//借款
                        }break;
                }
            }
        }
    }
  • 相关阅读:
    Android应用性能优化
    打造高质量Android应用:Android开发必知的50个诀窍
    毕向东day23--java基础-网络总结
    《编写高质量代码:改善Java程序的151个建议》
    最新java数组的详解
    主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException
    《Head First设计模式(中文版)》
    码表由来:ascll码-Gbk2312-GBK-Unicode-UTF-8
    《Java程序性能优化:让你的Java程序更快、更稳定》
    LeetCode 147. 对链表进行插入排序
  • 原文地址:https://www.cnblogs.com/sulong/p/4917100.html
Copyright © 2011-2022 走看看