Charater 6

6.16 this关键字

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var compare = new Compare();
  6. Console.WriteLine(compare.compareValue(10));
  7. }
  8. }
  9. class Compare
  10. {
  11. int myValue = 10;
  12. public int compareValue(int myValue)
  13. {
  14. return myValue > this.myValue ? myValue : this.myValue;
  15. // 参数 字段 参数 字段
  16. }
  17. }

6.17 索引器 indexer[]

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var employee = new Employee();
  6. employee[0] = "Hongyong";
  7. employee[1] = "Zhao";
  8. employee[2] = "Suzhou";
  9. Console.WriteLine("{0} {1} lives in {2}.",employee[0],employee[1],employee[2]);
  10. }
  11. }
  12. class Employee
  13. {
  14. public string lastName;
  15. public string firstName;
  16. public string cityOfBirth;
  17. public string this[int index] //indexer格式:修饰符+返回类型+this[int index]
  18. {
  19. get
  20. {
  21. switch (index)
  22. {
  23. case 0:return lastName;
  24. case 1:return firstName;
  25. case 2:return cityOfBirth;
  26. default:
  27. throw new ArgumentOutOfRangeException("index");
  28. }
  29. }
  30. set
  31. {
  32. switch (index)
  33. {
  34. case 0: lastName = value;
  35. break;
  36. case 1:firstName = value;
  37. break;
  38. case 2:cityOfBirth = value;
  39. break;
  40. default:
  41. throw new ArgumentOutOfRangeException("index");
  42. }
  43. }
  44. }
  45. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var empy1 = new Employee();
  6. Console.WriteLine("{0} {1}",empy1[0],empy1[1]);//默认值为0
  7. empy1[0] = 123;
  8. empy1[1] = 234;
  9. Console.WriteLine("{0} {1}", empy1[0], empy1[1]);
  10. }
  11. }
  12. class Employee
  13. {
  14. int temp0;
  15. int temp1;
  16. public int this[int index]
  17. {
  18. get { return (0 == index) ? temp0 : temp1; }
  19. set
  20. {
  21. if (0 == index)
  22. {
  23. temp0 = value;
  24. }
  25. else
  26. temp1 = value;
  27. }
  28. }
  29. }

6.18 访问器的访问修饰符 public protected private

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var albert = new Albert();
  6. string a = albert.Person("zhaohongyong");
  7. Console.WriteLine(a);
  8. }
  9. }
  10. class Albert
  11. {
  12. public string Name { get; private set; } //只允许在类中进行设置属性
  13. public string Person(string name)
  14. {
  15. Name = name;
  16. return Name;
  17. }
  18. }

6.20 分部方法 partial

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var alert = new Alert();
  6. alert.Add(3, 5);
  7. }
  8. }
  9. partial class Alert
  10. {
  11. partial void PrintSum(int x, int y);//定义分部方法
  12. public void Add(int x,int y)
  13. {
  14. PrintSum(x, y);
  15. }
  16. }
  17. partial class Alert
  18. {
  19. partial void PrintSum(int x, int y) //实现部分
  20. {
  21. Console.WriteLine("Sum is {0}",x+y);
  22. }
  23. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var alert = new Alert();
  6. alert.Calc(3, 5);
  7. }
  8. }
  9. partial class Alert
  10. {
  11. partial void PrintCalc(int x, int y);
  12. public void Calc(int x ,int y)
  13. {
  14. PrintCalc(x, y);
  15. }
  16. }
  17. partial class Alert
  18. {
  19. partial void PrintCalc(int x, int y)
  20. {
  21. Console.WriteLine(x + y);
  22. }
  23. }

Charater 7

7.1 类继承 冒号

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var calc = new CalcInter();
  6. calc.x = 8;
  7. calc.y = 2;
  8. Console.WriteLine(calc.CalcDiv(calc.x, calc.y));
  9. }
  10. }
  11. class Calc
  12. {
  13. public int x, y;
  14. public double CalcAdd(int x,int y)
  15. {
  16. return x + y;
  17. }
  18. }
  19. class CalcInter:Calc
  20. {
  21. public int field1, field2;
  22. public double CalcDiv(int x,int y)
  23. {
  24. return x / y;
  25. }
  26. }

7.4 屏蔽基类的成员 new

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var someClass = new SomeClass();
  6. someClass.Method1("someclass test");
  7. Console.WriteLine(someClass.field1 );
  8. var otherClass = new OtherClass();
  9. otherClass.Method1("otherclass test");
  10. Console.WriteLine(otherClass.field1);
  11. }
  12. }
  13. class SomeClass
  14. {
  15. public string field1 = "SomeClass field1";
  16. public void Method1(string value)
  17. {
  18. Console.WriteLine("SomeClass.Method1 :{0}",value);
  19. }
  20. }
  21. class OtherClass : SomeClass
  22. {
  23. new public string field1 = "SomeClass field2";//屏蔽数据成员 new 相同类型+相同名称
  24. new public void Method1(string value) //屏蔽函数成员 new 相同的签名,签名=名称+参数列表,不包括返回类型
  25. {
  26. Console.WriteLine("OtherClass.Method1:{0}",value);
  27. }
  28. }

7.5 基类访问 base

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var otherClass = new OtherClass();
  6. string other = otherClass.Field1; //派生类中字段
  7. Console.WriteLine( other); //派生类中的字段
  8. otherClass.PrintField1();
  9. }
  10. }
  11. class SomeClass
  12. {
  13. public string Field1 = "Field1--In the base class";//基类数据成员
  14. }
  15. class OtherClass : SomeClass
  16. {
  17. new public string Field1 = "Field1-- In the derived class";//屏蔽基类数据成员
  18. public void PrintField1()
  19. {
  20. Console.WriteLine(Field1); //继承类中Fiels1
  21. Console.WriteLine(base.Field1); //基类访问符base
  22. }
  23. }

7.6 使用基类引用

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var derivedClass = new Myderived();
  6. derivedClass.Print();
  7. var mybc = (MyBaseClass)derivedClass;//将derivedClass变量赋值给mybc,类型转换为基类类型,可见范围缩小。
  8. mybc.Print();
  9. }
  10. }
  11. class MyBaseClass
  12. {
  13. public void Print()
  14. {
  15. Console.WriteLine("This is the base class");
  16. }
  17. }
  18. class Myderived:MyBaseClass
  19. {
  20. new public void Print()
  21. {
  22. Console.WriteLine("This is the derived class");
  23. }
  24. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var derivedClass = new Myderived();
  6. derivedClass.Print();
  7. var mybc = (MyBaseClass)derivedClass;
  8. mybc.Print();
  9. }
  10. }
  11. class MyBaseClass
  12. {
  13. virtual public void Print()
  14. {
  15. Console.WriteLine("This is the base class");
  16. }
  17. }
  18. class Myderived:MyBaseClass
  19. {
  20. override public void Print()
  21. {
  22. Console.WriteLine("This is the derived class");
  23. }
  24. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var derivedClass = new Myderived();
  6. var secondDerived = new SecondDerived();
  7. derivedClass.Print();
  8. var mybc = (MyBaseClass)secondDerived;
  9. mybc.Print();
  10. }
  11. }
  12. class MyBaseClass
  13. {
  14. virtual public void Print()
  15. {
  16. Console.WriteLine("This is the base class");
  17. }
  18. }
  19. class Myderived:MyBaseClass
  20. {
  21. override public void Print()
  22. {
  23. Console.WriteLine("This is the derived class");
  24. }
  25. }
  26. class SecondDerived:Myderived
  27. {
  28. override public void Print()
  29. {
  30. Console.WriteLine("this is the second derived class");
  31. }
  32. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var secondDrived = new SecondDerived();
  6. secondDrived.Print(); //屏蔽了父类的方法
  7. var myBaseClass = (MyBaseClass)secondDrived;
  8. myBaseClass.Print(); //通过链条传递到最后拥有override的方法中
  9. }
  10. }
  11. class MyBaseClass
  12. {
  13. virtual public void Print()
  14. {
  15. Console.WriteLine("This is the base class");
  16. }
  17. }
  18. class Myderived:MyBaseClass
  19. {
  20. override public void Print()
  21. {
  22. Console.WriteLine("This is the derived class");
  23. }
  24. }
  25. class SecondDerived:Myderived
  26. {
  27. new public void Print()
  28. {
  29. Console.WriteLine("this is the second derived class");
  30. }
  31. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var myDerived = new Myderived();
  6. var myBase = (MyBaseClass)myDerived;
  7. Console.WriteLine(myDerived.MyInt);
  8. Console.WriteLine(myBase.MyInt);
  9. }
  10. }
  11. class MyBaseClass
  12. {
  13. private int myInt = 5;
  14. virtual public int MyInt
  15. {
  16. get { return myInt; }
  17. }
  18. }
  19. class Myderived:MyBaseClass
  20. {
  21. private int myInt = 15;
  22. override public int MyInt
  23. {
  24. get { return myInt; }
  25. }
  26. }
  27. class SecondDerived:Myderived
  28. {
  29. private int myInt = 5;
  30. override public int MyInt
  31. {
  32. get { return myInt; }
  33. }
  34. }

7.11抽象类:抽象类

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var Derived = new DerivedClass();
  6. Derived.MyInt = 56;
  7. Derived.PrintStuff("helloworld");
  8. Derived.TriangLeSideCount = 4;
  9. Derived.SideLength = 5;
  10. Console.WriteLine(Derived.MyInt);
  11. }
  12. }
  13. abstract class AbClass
  14. {
  15. public int SideLength = 10;//数据成员不可以声明为abstract
  16. public int TriangLeSideCount = 3;
  17. abstract public int MyInt { get; set; } //抽象属性
  18. abstract public void PrintStuff(string s); //抽象方法
  19. }
  20. class DerivedClass:AbClass
  21. {
  22. public override void PrintStuff(string s)
  23. {
  24. Console.WriteLine(s);
  25. }
  26. private int _myInt;
  27. public override int MyInt { get { return _myInt; } set { _myInt = value; } }
  28. }