标签:dispose 自动调用 names pre sel length string 关键字 pretty
using关键字有什么用?什么是IDisposable?
using可以声明namespace的引入,还可以实现非托管资源的释放,实现了IDisposiable的类在using中创建,using结束后会自动调用该对象的Dispose方法,释放资源。加分的补充回答:using其实等价于try……finally,用起来更方便。
里氏转换
(1).子类可以赋值给父类
(2).如果父类中装的是子类对象,那么可以将这个父类强制转换为子类对象.
得出结论:子类对象可以调用父类的成员,但是父类只能调用自己的成员.
注:子类可以转换成父类有什么意义?
有时候,有的地方需要一个父类对象,这个时候,我们可以使用这个父类的一个子类
来代替.(因为子类可以赋值给父类.)
对于里氏转换的判断
is 表示类型转换,如果能够转换成功,则返回一个true,否则返回一个false
is的使用方法:
if (p1 is Tether)
{
Tether t1 = (Tether)p1;
t1.TetherSayHello();
}
else
{
Console.WriteLine("转换失败");
}
as 表示类型转换,如果能够转换转换成功,则返回对应的对象,否则返回一个null.
as的使用方法:
Student s3 = p1 as Student;
s3.StudentSayHello();
namespace _02.里氏装换的练习
{
class Program
{
static void Main(string[] args)
{
/*
自己定义一个父类Person,两个子类Teacher和Student类.
Person写一个有参数的构造方法.
*/
/*
自己试试,子类可以自动转父类,父类转子类要通过强转.
为了不出错,可以先通过is判断或用as转换.
*/
Person p1;
Student s1 = new Student("张三", 18, ‘男‘);
if (s1 is Person) //使用is进行转换
{
p1 = s1;
Console.WriteLine("转换成功");
}
else
{
Console.WriteLine("转换失败");
}
Person p2;
Teacher t1 = new Teacher("小兰", 16, ‘女‘);
p2 = t1 as Person; //使用as进行转换
if (p2 != null)
{
Console.WriteLine("转换成功");
}
else
{
Console.WriteLine("转换失败");
}
Console.ReadKey();
}
}
public class Person
{
public Person(string name, int age, char gender)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
} //Name属性定义结束
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
} //Age属性定义结束
public char Gender
{
get
{
return _gender;
}
set
{
_gender = value;
}
} //_gender属性定义结束
private int _age;
private char _gender;
public void PersonSayHello()
{
Console.WriteLine("我是父类");
}
} //Person 类定义结束
public class Student:Person
{
public Student(string name, int age, char gender):base(name,age, gender)
{
}
public void StudentSayHello()
{
Console.WriteLine("我是学生");
}
}//Student 类定义结束
public class Teacher : Person
{
public Teacher(string name, int age, char gender) : base(name, age, gender)
{
}
public void TeacherSayHello()
{
Console.WriteLine("我是老师");
}
}
}
namespace _03.子类赋值给父类的练习01
{
class Program
{
static void Main(string[] args)
{
Person[] p1 = new Person[10];
Random r = new Random();
int random = 0; //保存随机数
for (int i = 0; i < p1.Length; i++)
{
random=r.Next(1, 7); //从1`6随机产生一个数
switch (random)
{
case 1:
p1[i] = new Student();
break;
case 2:
p1[i] = new Teacher();
break;
case 3:
p1[i] = new MeiLv();
break;
case 4:
p1[i] = new YeShou();
break;
case 5:
p1[i] = new ShuaiGuo();
break;
case 6:
p1[i] = new Person ();
break;
}
}
for (int i = 0; i < p1.Length; i++)
{
if (p1[i] is Student)
{
((Student)p1[i]).StudentSayHello();
}
else if (p1[i] is Teacher)
{
((Teacher)p1[i]).ThacherSayHello();
}
else if (p1[i] is MeiLv)
{
((MeiLv)p1[i]).MeiLvSayHello();
}
else if (p1[i] is Person)
{
p1[i].PersonSayHello();
}
else if (p1[i] is YeShou)
{
((YeShou)p1[i]).YeShouSayHello();
}
else if (p1[i] is ShuaiGuo)
{
((ShuaiGuo)p1[i]).ShuaiGuoSayHello();
}
}
Console.ReadKey();
}
}
public class Person
{
public void PersonSayHello()
{
Console.WriteLine("我是人类");
}
}
public class Student : Person
{
public void StudentSayHello()
{
Console.WriteLine("我是学生");
}
}
public class Teacher : Person
{
public void ThacherSayHello()
{
Console.WriteLine("我是老师");
}
}
public class YeShou : Person
{
public void YeShouSayHello()
{
Console.WriteLine("我是野兽");
}
}
public class ShuaiGuo:Person
{
public void ShuaiGuoSayHello()
{
Console.WriteLine("我是帅锅");
}
}
public class MeiLv:Person
{
public void MeiLvSayHello()
{
Console.WriteLine("我是美女");
}
}
}
标签:dispose 自动调用 names pre sel length string 关键字 pretty
原文地址:http://www.cnblogs.com/HelloZyjS/p/6034818.html