扣响C#之门笔记-第八章
8.1 以对象为成员
1.类的成员不光是可以使int,double等基本类型,也可以是其他类的对象;
class Program { static void Main(string[] args) { date d = new date(1992, 5, 18, new Time(12, 20, 5)); } } class date { int year; int month; int day; Time t; //(1)含有其他对象成员的类 public date(int year, int month, int day, Time t) { this.year = year; this.month = month; this.day = day; this.t = t; } } class Time { int houer; int minute; int second; public Time(int houer, int minute, int second ) { this.houer = houer; this.minute = minute; this.second = second; } }
8.2 静态常量
(1)静态变量就是用satic 关键字修饰的变量
(2)静态变量只能使用类名引用不能使用对象调用
class Program { static void Main(string[] args) { S s = new S(); //s.number=5;// error (2)静态变量只能使用类名引用不能使用对象调用 s.setSnumber(); Console.WriteLine(S.number); S.number = 4; Console.WriteLine(S.number); Console.ReadKey(); } } class S { public static int number; //(1)静态变量就是用satic 关键字修饰的变量 public S() { number = 0; } public void setSnumber() { number = 5; } }
(3)静态方法就是用satic 关键字修饰的方法
(4)静态方法只能使用类名引用,不能使用对象引用
class Program { static void Main(string[] args) { A.print(); A a = new A(); //a.print(); error (2)静态方法只能使用类名引用,不能使用对象引用 } } class A { public static void print() //(1)静态方法就是用satic 关键字修饰的方法 { Console.WriteLine("print"); Console.ReadKey(); } }
8.3常量
8.3.1 const常量
(1)const 常量定义方法:访问权限+ const +类型+常量名=初始值;
(2)const 常量必须在定义时候同时赋值;
(3)const 常量是隐式静态的(不能用static关键字修饰),因此只能使用类名引用
class Program { static void Main(string[] args) { Console.WriteLine(circle.PI);//(3)const 常量是隐式静态的(不能用static关键字修饰),因此只能使用类名引用 Console.ReadKey(); } } class circle { // public const double PI ; (2)const 常量必须在定义时候同时赋值; // PI=3.14; public const double PI = 5;//(1)const 常量定义方法:访问权限+ const +类型+常量名=初始值; }
8.3.2 readonly常量
(1)readonly 常量定义方法:同const
(2)readonly 常量是非静态常量,每个对象可以有不同的值,可以在构造函数中初始化(也可在定义时候赋值);
(3)由于readonly 常量是非静态的,和普通变量一样,使用对象引用,不能使用类名引用
class Program { static void Main(string[] args) { hotel h1 = new hotel(20); hotel h2 = new hotel(30); Console.WriteLine(h1.number);//(3)由于readonly 常量是非静态的,和普通变量一样,使用对象引用,不能使用类名引用 Console.WriteLine(h2.number); //Console.WriteLine(hotel.number); //error Console.ReadKey(); } } } class hotel { public readonly int number=0;//(1)readonly 常量定义方法 public hotel(int number)//(2)readonly 常量是非静态常量,每个对象可以有不同的值,可以在构造函数中初始化(也可在定义时候赋值); { this.number = number; }
8.4 重载
8.4.1 函数重载
(1)函数名相同,参数个数或者参数类型不同 构成重载
(2)函数重载调用的原则是“最佳匹配”,系统或调用参数最匹配的那个函数
class Program { static void Main(string[] args) { //(2)函数重载调用的原则是“最佳匹配”,系统或调用参数最匹配的那个函数 Console.WriteLine(calculate.add(1,1)); Console.WriteLine(calculate.add(1.1, 1)); Console.WriteLine(calculate.add(1, 1,1)); Console.ReadKey(); } } class calculate { public static int add(int a, int b) { return a+b; } public static double add(double a, double b)//(1)函数名相同,参数个数或者参数类型不同 构成重载 { return a + b; } public static int add(int a, int b,int c) { return a + b+c; } }
8.4.2构造函数重载
一个类可以有多个重载的构造函数;当创建对象时候,根据参数不同,分别调用不同的构造函数;
class Program
{
static void Main(string[] args)
{
new cat();
new cat("kitty");
new cat("kitty",3);
}
}
//一个类可以有多个重载的构造函数;当创建对象时候,根据参数不同,分别调用不同的构造函数;
class cat
{
string name;
int age;
public cat()
{
}
public cat(string name)
{
this.name = name;
}
public cat(string name, int age)
{
this.name = name;
this.age = age;
}
}
8.4.3 运算符重载
(1)运算符重载方法: 使用oprator关键字,必须定义为静态
public static + 返回值类型+ operator + 运算符+(操作数类型1 操作数1,.....)
(2)比如当重载了运算符 +,就不必定义+= 等符合运算符,因为系统会自动自动分解的
class Program { static void Main(string[] args) { complex c1 = new complex(1, 1); complex c2 = new complex(2, 3); complex c = c1 + c2; Console.WriteLine("{0}i+{1}j",c.a,c.b); Console.ReadKey(); //(2)比如当重载了运算符 +,就不必定义+= 等符合运算符,因为系统会自动自动分解的 } } class complex { public int a; public int b; public complex(int a,int b) { this.a = a; this.b = b; } //(1)运算符重载方法: 使用oprator关键字,必须定义为静态 // public static + 返回值类型+ operator + 运算符+(操作数类型1 操作数1,.....) public static complex operator +(complex z1, complex z2) { return new complex(z1.a + z2.a, z1.b + z2.b); } public static complex operator -(complex z1, complex z2) { return new complex(z1.a - z2.a, z1.b - z2.b); } }
8.5 this 关键字
(1)在类内部,可以直接使用类的成员变量,也可以通过this引用符使用变量;
(2)如果函数中局变量与成员变量(字段)重名,那么在函数内部,成员字段会被屏蔽掉(函数只看到局部变量),此时需要加this才能访问到成员变量;
class Program { static void Main(string[] args) { time t = new time(1,1,1); Console.WriteLine(t.hour); Console.ReadKey(); } } //(1)在类内部,可以直接使用类的成员变量,也可以通过this引用符使用变量; class time { public int hour; public int minute; public int second; public time(int hour,int minute,int second) { // (2)如果函数中局变量与成员变量(字段)重名,那么在函数内部, //成员字段会被屏蔽掉(函数只看到局部变量),此时需要加this才能访问到成员变量; this.hour = hour; // hour = hour; 会有告警,关键是成员变量根本没有赋值到 this.minute = minute; this.second = second; } }
8.6 索引器:
(1)索引可以使我们可以像数组一样访问类的成员,定义方法如下:

(2)索引的结构和属性相似,也是get和set访问器,get访问器用于获取成员变量的值,set 访问器用于设置成员变量的值
(3)索引器的使用方法和数组使用方法一致;
class Program { static void Main(string[] args) { chinsenumber c= new chinsenumber(); //(3)索引器的使用方法和数组使用方法一致; c["一"] = 1; c["二"] = 2; c["三"] = 3; c["四"] = 4; Console.WriteLine(c["一"]); Console.WriteLine(c["二"]); Console.WriteLine(c["三"]); Console.WriteLine(c["四"]); Console.ReadKey(); } } class chinsenumber { public int one; public int two; public int threre; public int four; //(1)索引可以使我们可以像数组一样访问类的成员,定义方法如下: public int this[string index] // { //(2)索引的结构和属性相似,也是get和set访问器,get访问器用于获取成员变量的值,set 访问器用于设置成员变量的值 set { switch (index) { case "一": one = value; break; case "二": two = value; break; case "三": threre = value; break; case "四": four = value; break; default: break; } } get { switch (index) { case "一": return one; break; case "二": return two; break; case "三": return threre; break; case "四": return four; break; default: return 0; break; } } } }
8.7 值类型和引用类型
8.7.1
(1)内存中有块区域叫做栈(stack),用来存储整形,实型,布尔,字符等值类型
(2)栈的工作方法和“米缸”一样,对数据的操作总是发生在顶部,最后入栈的总是最先弹出,最先入栈的最后弹出;
因此先入栈的数据作用域总是比后入栈的长,后入栈的数作用域嵌套在先入栈的数据作用域内

8.7.2 引用类型
(1)C#将成员变量存储在堆(heap)上;
(2)堆的工作模式与栈完全不同:要经过两个过程
(a)第一步:在堆中划分一块空间存储成员变量,并调用构造函数初始化成员变量值;
(b)第二步:在栈中划分一块空间存储对象在堆中的首地址;

8.7.3
(1)实际程序中,可能有多个引用符同时指向同一个对象,当一个引用符退出作用域时,系统就会将从栈空间将引用符删除;
当指向对象的所有的引用符都被删除后,该对象就被加入垃圾回收的名单,垃圾回收器适时清楚该对象;
(2).NET使用的是托管堆,与普通堆不同,垃圾回收机清除一个对象,会移动堆中的其他对象,使他们连续排列在堆的底部,并用一个堆指针指向堆的空闲空间,当系统创建新对象时候,系统可以根据地址找到空闲空间(效率高,切不会造成地址不连续)
8.8 引用符和对象的区别
(1)引用符是对象在堆中的地址;
(2)当对象没有实例化时候,堆的值就是null,当实例化后,堆的值就是对象的地址;
8.9 对象数组
申明对象数组和普通数组一样;
class Program { static void Main(string[] args) { //(1)申明对象数组方法和普通数组一样 cat[] c = new cat[5]; cat[] d = { new cat(), new cat() }; cat[] f = new cat[] { new cat(), new cat() }; } } class cat { }
8.10 类视图
查看类视图
【视图】


class Program { static void Main(string[] args) { } } class A { int a; } class B:A { int b; } class C : B { int d; } class D: B { int d; }
浙公网安备 33010602011771号