7月3日课堂笔记

复习
   1,什么是序列化,如何进行序列化
      将对象转换成2进制数据
   1,标记  [Serializeable]
   2 FileStream(可以换成网络流,内存流,就不需要FileStream了)
   3 new BinaryFormatter
   4 调用Serialize     反序列化是Deserialize
   2文件读取常用的类有哪些
   FileStream
   File.ReadAllLines/WriteAllLins
   FIle.ReadALlText
   StreamReader/StreamWriter
   FileInfo
   DirectoryInfo
-----   ------------------------------------------------
   索引器就是一个带有参数的属性
   原理如下
           public int Num
        {
            get;
            set;
        }

        int[] nums = { 1, 11, 111, 2, 22, 222, 3, 33, 333 };

        public int this[int index]
        {
            get {return nums[index];}
            set { nums[index] = value; }
        }

        public string this[string key]
        {
            get { return "没有使用索引参数"; }
        }

        public string this[int i, string s, char c, double d]
        {
            get { return string.Format("{0}{1}{2}{3}", i, s, c, d); }
        }

        public int Length
        {
            get { return nums.Length; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            Dictionary<int, int> dic = new Dictionary<int, int>();

            MyClass m = new MyClass();
            for(int i = 0; i < m.Length; i ++)
            {
                Console.WriteLine(m[i]);
                Console.WriteLine(m[""]);
                Console.WriteLine(m[10,"a123b",'男',10.123]);
            }
            Console.ReadKey();
        }
    }
}
=====================================================
面试题什么是深考与浅考
深考:深度拷贝(绝对拷贝)
深考:将对象中的字段、以及对象中所关联的所有对象引用全部拷贝过来,(只要new给了字段 就是深考)拷贝过来以后 跟被拷贝的对象没有任何关系

浅考:浅度拷贝(非绝对拷贝)
浅考:只将对象中的字段拷贝过来 其他关联的对象引用不拷贝(不new),只指向被拷贝的对象
Class MyClass
{
    public int Num;
}
Main
{
    MyClass m1=new MyClass();
 m1.Num=100;
 
 MyClass m2=m1;
 //上面的只是指向同一内存,不是深考与先考
 //所谓拷贝就是Copy,就是复制
 MyClass m3=new MyClass();
 m3.Num=m1.Num;  //此时叫做拷贝
---------------------------------------------------------------------------
只针对对象MyClass2,将其数据拷贝了一份,对其中引用类型直接复制过来
没有考虑其引用类型的拷贝就是浅拷贝
    class MyClass1
    {
        public int Num;
    }

    class MyClass2
    {
        public int Num;
        public MyClass1 C;
    }
 MyClass2 m1=new MyClass();
 m1.Num=10;
 m1.C=new MyClass1();
 m1.C.Num=20;
 
 MyClass2 m2=new MyClass2();
 m2.Num=m1.Num;
 m2.C=m1.C;
-----------深拷贝------------------------------------------------------------
    m2.C=new MyClass1();
 m2.C.Num=m1.C.num;  把里面的所有数据都复制
------------------------------------------------------------------------------
object中提供的受保护方法
    class MyClone
    {
        public int Num;
        public string str;
        public double dNum;
        public MyClass1 C;

        public MyClone Clone()
        {
            // 由object提供的方法完成浅拷贝
            return (MyClone)base.MemberwiseClone();   
                   ---object提供的浅拷贝方法-----
        }

        public MyClone DeepoClone()//浅拷贝的基础上进行深拷贝
        {
            MyClone m = (MyClone)MemberwiseClone();
            m.C = new MyClass1();
            m.C.Num = this.C.Num;
            return m;
        }
------------------------------------------
拷贝就是new一个对象,将数据赋值过来
将对象直接变成二进制数据,直接Copy而键值数据,将其在返回成对象
1,序列化将对象转换成二进制数据,原对象和新对象没关系
2,深考中拷贝的源对象与新的对象什么关系
[Serializeable]
    using(FileStream file=new FileStream(path,FileMode.Create,FileAccess.Write))
 {
      BinaryFormatter bf=new BinaryFormatter();
   bf.Serialize(file,待序列化的类);
 }
 using(FileStream read=new FileStream(path,FileMode>open,FileAccess.Read))
 {
       BinaryFormatter bf1=new BinaryFormatter();
    类=(类)bf1.Deserialize(reader);
 }
 此方法很麻烦,还要在硬盘上创建文件
--------
简单方法 用序列化实现深拷贝
MyClone m3=null;
MemoryStream stream=new MemoryStream();     //创建其支持存储区为内存的流
using(stream)
{
   BinaryFormatter bin=newBinaryFormatter();
   bin.Serialize(stream,m1);
  
   stream.Position=0;
   m3=(MyClone)bin.Deserialize(stream);
}
-----------------------==========
获得当前路径
string path=Directory.GetCurrentDirectory();//I不建议使用,容易被改动
-----
string s=Assembly.GetExecutingAssembly().FullName;   获得当前程序版本信息
string s=Assembly.GetExecutingAssembly().Location;  获得当前程序所在全路径
-------------
小说阅读器
递归  自己调自己(递推表达式Fn与Fn-1的关系)

 

posted @ 2012-07-04 01:58  我的名字很长很长  阅读(140)  评论(0编辑  收藏  举报