C#编写高质量代码第六天

总结:

  • 1、如果没有为类型重写Tostring方法,会使用Object的Tostring返回当前类型的名称

  • 2、继承Ifomattable接口可以实现可格式化的ToString ,就是多一个判读条件,然后在内部通过Switch case进行判读

  • 深拷贝和签拷贝的实现方法如下,深拷贝可以通过序列化和反序列的方式最终获得与之前不相干的的对象

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace PromoteDay06
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * 建议13:为类型输出格式化字符串
             * 1、如果没有为类型重新Tostring方法,会使用Object的Tostring返货当前类型的类型名称
             */
            //Person p1 = new Person() { FirstName = "cheng", LastName = "jianhong", IdCode = "20180813" };
            //Console.WriteLine(p1);
            //Console.ReadKey();

            /*
             * 建议14:正确实现浅拷贝和深拷贝
             深拷贝:值类型拷贝后修改不会影响之前的对象的值,引用类型拷贝的是对象的引用,则会对原来的对象进行改变
             浅拷贝:无论是值类型还是引用类型,改变后都不会影响原来对象的值
             1、建议继承ICloneable接口
             */
            Employee emploee = new Employee() { IDCode = "1808", Department = new Department() { Name = "Eric" }, Age = 20 };
            Employee rose = emploee.DeepClone();
            rose.Age = 36;//int类型未值类型不会改变
            rose.IDCode = "66";//string类型为引用类型,但是看成值类型
            rose.Department.Name = "King";//引用类型,则改变原来的
            Console.WriteLine(emploee.Age);
            Console.WriteLine(emploee.IDCode);
            Console.WriteLine(emploee.Department.Name);
            Console.ReadKey();
        }
        /// <summary>
        /// 继承Ifomattable接口实现可格式化的Tostring
        /// </summary>
        public class Person : IFormattable
        {
            public string IdCode { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

            public override string ToString()
            {
                return this.FirstName + this.LastName;
            }

            public string ToString(string format, IFormatProvider formatProvider)
            {
                switch (format)
                {
                    case "CH":
                        return this.ToString();

                    case "EG":
                        return string.Format("{0}{1}", FirstName, LastName);
                    default:
                        return this.ToString();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        [Serializable]
        public class Department
        {
            public string Name { get; set; }
            public override string ToString()
            {
                return this.Name;
            }

        }
        /// <summary>
        /// 继承ICloneable接口实现浅拷贝
        /// </summary>
        //public class Employee : ICloneable
        //{
        //    public string IDCode { get; set; }
        //    public int Age { get; set; }
        //    public Department Department { get; set; }
        //    public object Clone()
        //    {
        //        return this.MemberwiseClone();
        //    }
        //}
        [Serializable]
        public class Employee : ICloneable
        {
            public string IDCode { get; set; }
            public int Age { get; set; }
            public Department Department { get; set; }

            /// <summary>
            /// 自定义深拷贝,序列表内存值的二进制,然后通过反序列化得到object 隐式转换为Employee
            /// </summary>
            /// <returns></returns>
            public Employee DeepClone()
            {
                using (Stream objectStream = new MemoryStream())
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(objectStream, this);
                    objectStream.Seek(0, SeekOrigin.Begin);
                    return bf.Deserialize(objectStream) as Employee;
                }
            }
            /// <summary>
            /// 自定义浅拷贝
            /// </summary>
            /// <returns></returns>
            public Employee ShallowClone()
            {
                return this.Clone() as Employee;
            }
            /// <summary>
            /// 这个是继承接口必须要写的
            /// </summary>
            /// <returns></returns>
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值