《C#面向对象程序设计 教学课件 ppt 郑宇军 CSCH4》由会员分享,可在线阅读,更多相关《C#面向对象程序设计 教学课件 ppt 郑宇军 CSCH4(43页珍藏版)》请在金锄头文库上搜索。
1、1,第4章 类和对象,成员概述,2,成员概述,class ComplexNumber public double a=0; public double b=0; ,ComplexNumber c1 = new ComplexNumber();,声明,创建对象,成员概述,3,成员种类 数据成员 函数成员 嵌套成员,成员概述,成员访问限制 private 私有,4,class Student private string name; private void Write() Console.WriteLine(name); ,Student s1 = new Student(); Console.
2、WriteLine(s1.name); s1.Write();,成员概述,成员访问限制 private 私有 public 公有,5,class Student public string name; public void Write() Console.WriteLine(name); ,Student s1 = new Student(); Console.WriteLine(s1.name); s1.Write();,成员概述,成员访问限制 private 私有 public 公有 protected 保护,6,class Student protected string name;
3、protected void Write() Console.WriteLine(name); ,class Graduate : Student public void Output() Console.WriteLine(name); Write(); ,成员概述,成员访问限制 private 私有 public 公有 protected 保护 internal 内部,7,namespace Asm1 internal class Student ,namespace Asm2 class Library public void Get() Asm1.Student s1 = new As
4、m1.Student(); ,成员概述,成员访问限制 private 私有 public 公有 protected 保护 internal 内部,8,namespace Asm1 public class Student ,namespace Asm2 class Library public void Get() Asm1.Student s1 = new Asm1.Student(); ,成员概述,静态成员与非静态成员 实例成员(非静态),9,class Student public string name; ,Student s1 = new Student(); Console.Wri
5、teLine(s1.name);,成员概述,静态成员与非静态成员 实例成员(非静态) 静态成员,10,class Student public string name; public static string school; ,Student s1 = new Student(); Console.WriteLine(s1.name); Console.WriteLine(Student.school);,成员概述,静态成员与非静态成员 实例成员(非静态) 静态成员,11,class Student public string name; public static string schoo
6、l; public static void Write() Console.WriteLine(name); ,成员概述,字段 一般字段,12,public class Circle public double r; ,Circle c1 = new Circle(); Console.WriteLine(c1.r); c1.r = 10;,成员概述,字段 一般字段 常量字段,13,public class Circle public double r; public const double PI = 3.14; ,Circle c1 = new Circle(); Console.Writ
7、eLine(c1.r); c1.r = 10; Console.WriteLine(Circle.PI); Circle.PI = 3.1416;,成员概述,字段 一般字段 常量字段 只读字段,14,public class Circle public double r; public readonly double PI = 3.14; ,Circle c1 = new Circle(); Console.WriteLine(c1.r); c1.r = 10; Console.WriteLine(c1.PI); c1.PI = 3.1416;,方法,15,方法声明,public int Ma
8、x(int x, int y) return x = y ? x : y; ,返回类型,方法名,参数列表,执行体,方法,参数类型 值传递,16,public void Swap(int x, int y) int z = x; x = y; y = z; ,int a = 10; int b = 20; Swap(a, b);,方法,参数类型 值传递,17,public void Swap(int x, int y) int z = x; x = y; y = z; ,int a = 10; int b = 20; Swap(a, b);,方法,参数类型 值传递 引用传递,18,public
9、void Swap(ref int x, ref int y) int z = x; x = y; y = z; ,int a = 10; int b = 20; Swap(a, b);,方法,参数类型 值传递 引用传递,19,public void Swap(ref int x, ref int y) int z = x; x = y; y = z; ,int a = 10; int b = 20; Swap(a, b);,方法,参数类型 值传递 引用传递 输出型参数,20,public int SumProd(int x, int y, out int a) a = x * y; retu
10、rn x + y; ,方法,参数类型 值传递 引用传递 输出型参数 数组型参数,21,public int Sum(params int x) int s = 0; for(int i=0; ix.Length; i+) s += xi; return s; ,int x = 1, 3, 5, 7, 9; int s1 = Sum(x); int s2 = Sum(2, 4, 6, 8, 10);,类的特殊方法,构造函数,22,public class Student public int age = 18; ,Student s1 = new Student(); s1.age = 20;,
11、类的特殊方法,构造函数 无参构造函数,23,public class Student public int age; public Student() age = 18; ,Student s1 = new Student(); s1.age = 20;,类的特殊方法,构造函数 无参构造函数 带参构造函数,24,public class Student public int age; public Student(int i) age = i; ,Student s1 = new Student(20);,类的特殊方法,构造函数 无参构造函数 带参构造函数 静态构造函数,25,public c
12、lass Student public int age; public static string school; static Student() school = “华中理工大学“; ,类的特殊方法,构造函数 无参构造函数 带参构造函数 静态构造函数 析构函数,26,public class Student public string name; Student() Console.Write(“学生对象0销毁”, name); ,类的特殊方法,属性 get set,27,public class Student private string name; public string Nam
13、e get return name; set name = value; ,类的特殊方法,索引函数 get set,28,public class School private Student students; public Student thisint index get return studentsindex; set studentsindex = value; ,类的特殊方法,操作符,29,int x = 2; int y = 3; int z = x + y;,预定义操作,自定义操作,ComplexNumber c1 = new ComplexNumber(2,4); Comp
14、lexNumber c2 = new ComplexNumber(3,6); ComplexNumber c3 = c1 + c2;,类的特殊方法,操作符,30,public static ComplexNumber operator + (ComplexNumber c1, ComplexNumber c2) return new ComplexNumber(c1.a+c2.a, c1.b+c2.b); ,类的特殊方法,操作符 一元操作符 二元操作符,31,+ - ! (T) ture false,+ - * / % & | = != = ,this 对象引用,this.,32,public
15、 class ComplexNumber private double a; private double b; public ComplexNumber(double a, double b) this.a=a; this.b=b; public void Output() Console.Write(“0+1i“, this.a, this.b=b); ,常用类型,Object ToString() GetType(),33,object o1 = new object(); console.WriteLine(o1.GetType(); o1 = “abc“; Console.WriteLine(o1.GeType();,System.Object,System.String,常用类型,Object ToString() GetType() Equals(),34,object o1 = new object(); Student s1 = new Student(); console.Write(o1 = s1); o1 = s1; Console.Write(o1 = s1);,false,true,常用类型,String 创建对象,35,string s1 = “Visual C