c#练手特性

208 阅读1分钟

摘自深入理解c#

using System.Collections;
using System.Collections.Generic;

namespace ConsoleApp2
{
    public class Productc01
    {
        string name;
        public string Name { get { return name; } }
        decimal price;
        public decimal Price { get { return price; } }
        public Productc01 (string name,decimal price)
        {
            this.name = name;
            this.price = price;
        }
        public static ArrayList GetSampleProducts()
        {
            ArrayList list = new ArrayList();
            list.Add(new Productc01("West Side Story", 9.99m));
            list.Add(new Productc01("Assassins", 14.99m));
            list.Add(new Productc01("Frogs", 13.99m));
            list.Add(new Productc01("Sweeney Todd", 10.99m));
            return list;
        }
        public override string ToString()
        {
            return string.Format("{0}:{1}", name, price);
        }
    }
    public class Productc02
    {
        string name;
        public string Name
        {
            get { return name; }
            private set { name = value; }
        }
        decimal price;
        public decimal Price
        {
            get { return price; }
            private set { price = value; }
        }
        public Productc02(string name,decimal price)
        {
            Name = name;
            Price = price;
        }
        public static List<Productc02> GetSampleProducts()
        {
            List<Productc02> list = new List<Productc02>();
            list.Add(new Productc02("West Side Story", 9.99m));
            list.Add(new Productc02("Assassins", 14.99m));
            list.Add(new Productc02("Frogs", 13.99m));
            list.Add(new Productc02("Sweeney Todd", 10.99m));
            return list;
        }
        public override string ToString()
        {
            return string.Format("{0}:{1}", name, price);
        }
    }



    public class Productc03
    {
        public string Name { get; private set; }
        public decimal Price { get; private set; }
        public Productc03(string name,decimal price)
        {
            Name = name;
            Price = price;
        }
        Productc03() { }
        public static List<Productc03> GetSampleProducts()
        {
            return new List<Productc03>
            {
                new Productc03{Name="West side story",Price=9.99m}
            };
        }
        public override string ToString()
        {
            return string.Format("{0}:{1}", Name, Price);
        }
    }
    public class Productc04
    {
        readonly string name;
        public string Name { get { return name; } }
        readonly decimal price;
        public decimal Price { get { return price; } }
        public Productc04(string name,decimal price)
        {
            this.name = name;
            this.price = price;
        }
        public static List<Productc04> GetSampleProducts()
        {
            return new List<Productc04>
            {
                new Productc04(name:"West side story",price:9.99m)
            };
        }
        public override string ToString()
        {
            return string.Format("{0}:{1}", Name, Price);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Test" + Productc01.GetSampleProducts()[0].ToString());
            Console.WriteLine("Test"+Productc02.GetSampleProducts()[0].ToString());
            Console.WriteLine("Test"+Productc03.GetSampleProducts()[0].ToString());
            Console.WriteLine("Test" + Productc04.GetSampleProducts()[0].ToString());
        }
    }
 }