zoukankan      html  css  js  c++  java
  • property 与 attribute 的区别?

    一个是属性,用于存取类的字段,一个是特性,用来标识类,方法等的附加性质。

    属性:

    class TimePeriod
    {
        private double seconds;
    
        public double Hours
        {
            get { return seconds / 3600; }
            set { seconds = value * 3600; }
        }
    }
    
    class Program
    {
        static void Main()
        {
            TimePeriod t = new TimePeriod();
    
            // Assigning the Hours property causes the 'set' accessor to be called.
            t.Hours = 24;
    
            // Evaluating the Hours property causes the 'get' accessor to be called.
            System.Console.WriteLine("Time in hours: " + t.Hours);
        }
    }
    // Output: Time in hours: 24
    View Code

    特性:

    using System;
    using System.Reflection;
    
    namespace CustomAttrCS
    {
        // An enumeration of animals. Start at 1 (0 = uninitialized).
        public enum Animal
        {
            // Pets.
            Dog = 1,
            Cat,
            Bird,
        }
    
        // A custom attribute to allow a target to have a pet.
        public class AnimalTypeAttribute : Attribute
        {
            // The constructor is called when the attribute is set.
            public AnimalTypeAttribute(Animal pet)
            {
                thePet = pet;
            }
    
            // Keep a variable internally ...
            protected Animal thePet;
    
            // .. and show a copy to the outside world.
            public Animal Pet
            {
                get { return thePet; }
                set { thePet = Pet; }
            }
        }
    
        // A test class where each method has its own pet.
        class AnimalTypeTestClass
        {
            [AnimalType(Animal.Dog)]
            public void DogMethod() { }
    
            [AnimalType(Animal.Cat)]
            public void CatMethod() { }
    
            [AnimalType(Animal.Bird)]
            public void BirdMethod() { }
        }
    
        class DemoClass
        {
            static void Main(string[] args)
            {
                AnimalTypeTestClass testClass = new AnimalTypeTestClass();
                Type type = testClass.GetType();
                // Iterate through all the methods of the class.
                foreach (MethodInfo mInfo in type.GetMethods())
                {
                    // Iterate through all the Attributes for each method.
                    foreach (Attribute attr in
                        Attribute.GetCustomAttributes(mInfo))
                    {
                        // Check for the AnimalType attribute.
                        if (attr.GetType() == typeof(AnimalTypeAttribute))
                            Console.WriteLine(
                                "Method {0} has a pet {1} attribute.",
                                mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
                    }
                }
            }
        }
    }
    
    /*
     * Output:
     * Method DogMethod has a pet Dog attribute.
     * Method CatMethod has a pet Cat attribute.
     * Method BirdMethod has a pet Bird attribute.
     */
    View Code
  • 相关阅读:
    【3y】从零单排学Redis【青铜】
    【Java】几道常见的秋招面试题
    【Java】广州三本秋招经历
    两个月的Java实习结束,继续努力
    外行人都能看懂的SpringCloud,错过了血亏!
    【Java】留下没有基础眼泪的面试题
    【Java】几道让你拿offer的知识点
    Java多线程打辅助的三个小伙子
    数据库两大神器【索引和锁】
    Linux网络管理
  • 原文地址:https://www.cnblogs.com/binyao/p/4899116.html
Copyright © 2011-2022 走看看