C#入门经典第十一章笔记(章节作业反馈)
版权声明:本文为博主原创文章,未经博主允许不得转载。
1、前期知识准备
①C#中值类型有:整数、浮点型、布尔形、结构体、枚举和可空类型。
②可以简单理解装箱为:由值类型转换为引用类型,或者由值类型转换为可实现的接口类型。拆箱为装箱的反过程。
③装箱的过程是在堆中生成一个原始的栈中A的引用副本B,这两者之间是不会互相影响的。同样,拆箱后也会在栈里面分配一个堆的副本C,这两者(B和C)也不会互相影响。
④常用的集合有两种List<T>和Dictionary.这两种类型都是强类型化。即不用类型转换就可以运用。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { public class Test { public string Name { get; set; } public Int32 Age { get; set; } public Test() { } public Test(string name, Int32 age) { this.Name = name; this.Age = age; } public void Say() { Console.WriteLine(Name+" has " +Age+ " and Say hello!"); } } static void Main() { List<Test> people = new List<Test>(); people.Add(new Test("Jack", 32)); people.Add(new Test("Rose", 30)); for (int i = 0; i < people.Count; i++) { people[i].Say(); } Console.ReadKey(); } } }
结果如下图

⑤System.Collection名称空间提供几个接口,提供了基本的集合功能:IEnumerable可以迭代集合中的项;ICollection(继承于IEnumerable)可以获取集合中项的个数,并把项复制到一个简单的类型中;IList;IDictionary。
⑥定义集合可以用到CollectionBase类和DictonaryBase类。CollectionBase有接口IEnumerable,ICollection(提供Count属性)和ILst(提供Clear()和RemoveAt())。IEnumerable接口提供GetEnumerator()方法并返回IEnunberator(注、此方法提供Current属性,用于获取对象引用。提供MoveNext()方法)。
2、练习开始
(1)创建集合类People,是下述Persion类的集合,该集合通过字符串来访问,索引符是人名(此处简化难度,用CollectionBase代替DictioaryBase)。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Person { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } } }
创建集合类,并提供索引,需要用到CollectionBase,并用到索引器:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class People: CollectionBase,ICloneable { public void Add(Person person) { List.Add(person); } public void Remove(Person person) { List.Remove(person); } public Person this[int indix] { get { return (Person)List[indix]; } set { List[indix] = value; } } } }
CollectionBase提供List属性,用于添加Person对象,索引符可以看作一个特殊的属性,这里,索引符可以看作是People的Person属性。
(2)、扩展上一题的Person类,重载>、<、>=和<=运算符,比较Person实例的Age属性。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Person:ICloneable { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public Person() { } public Person(string name, int age) { this.age = age; this.name = name; } public static bool operator >(Person lper, Person rper) { return lper.age > rper.age; } public static bool operator <(Person lper, Person rper) { return lper.age < rper.age; } } }
(3)在People类上实现ICloneable接口,提供深度复制功能
深度复制实质是建立一个对象,将需要克隆的对象的成员都赋值给新的对象。ICloneable接口提供MemberwiseClone()方法,可以提供浅层复制,按照微软要求,用接口提供的Clone()方法来深度和浅度复制。
Person类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Person:ICloneable { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public Person() { } public Person(string name, int age) { this.age = age; this.name = name; } public static bool operator >(Person lper, Person rper) { return lper.age > rper.age; } public static bool operator <(Person lper, Person rper) { return lper.age < rper.age; } public object Clone() { return MemberwiseClone(); } } }
People类(包含迭代器):
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class People: CollectionBase,ICloneable { public void Add(Person person) { List.Add(person); } public void Remove(Person person) { List.Remove(person); } public Person this[int indix] { get { return (Person)List[indix]; } set { List[indix] = value; } } public new IEnumerator GetEnumerator() { return (from Person pe in List select pe.Age).GetEnumerator(); } public object Clone() { People peop = new People(); foreach (var source in List) { peop.Add((Person) ((Person) source).Clone()); } return peop; } } }
(5)给People类添加一个迭代器,在foreach里面获取成员年龄
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { People peo = new People(); peo.Add(new Person("Jack", 55)); peo.Add(new Person("Rose", 43)); peo.Add( new Person("Roll", 4)); peo.Add(new Person("Ro", 43)); Console.WriteLine(peo[0]>peo[1]); People people1; people1 = (People) peo.Clone(); Console.WriteLine("(preClone)Jack's age is:"+peo[0].Age); Console.WriteLine("(AfterClone)Jack's age is:" + people1[0].Age); Console.WriteLine("Chang the age of peo"); peo[0].Age = 12; Console.WriteLine("(preClone)Jack's age is:" + peo[0].Age); Console.WriteLine("(AfterClone)Jack's age is:" + people1[0].Age); Console.WriteLine("迭代所有的AGE"); foreach (var p in people1) { Console.WriteLine(p); } Console.ReadKey(); } } }
上面代码第一、将person对象添加到people集合;第二、完成符号重载;第三、深度复制;第四、完成迭代。
结果如下:

浙公网安备 33010602011771号