C#面向对象11 里氏转换
里氏转换
1.子类可以赋值给父类。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //****** //1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 //Student s = new Student(); //Person p = s; Person p = new Student(); //例如 string str = string.Join("|", new string []{"1","2","3"}); Console.WriteLine(str); Console.ReadKey(); } } public class Person { public void PersonSay() { Console.WriteLine("Person"); } } public class Student:Person { public void StudentSay() { Console.WriteLine("Student"); } } public class Teacher:Person { public void TeacherSay() { Console.WriteLine("Teacher"); } } }
2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //****** //1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //****** //2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。 Student ss = (Student)p; ss.StudentSay(); Console.ReadKey(); } } public class Person { public void PersonSay() { Console.WriteLine("Person"); } } public class Student:Person { public void StudentSay() { Console.WriteLine("Student"); } } public class Teacher:Person { public void TeacherSay() { Console.WriteLine("Teacher"); } } }
3.子类对象可以调用父类的中的成员,但是父类对象永远只能调用自己的成员
4.
is: 表示类型的转换,如果能够转换成功,则返回一个true,否则返回false。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //****** //1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //****** //2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。 //Student ss = (Student)p; //ss.StudentSay(); //is转换 if (p is Student) { Student tt = (Student)p; tt.StudentSay(); } else { Console.WriteLine("NO"); } Console.ReadKey(); } } public class Person { public void PersonSay() { Console.WriteLine("Person"); } } public class Student:Person { public void StudentSay() { Console.WriteLine("Student"); } } public class Teacher:Person { public void TeacherSay() { Console.WriteLine("Teacher"); } } }
as:表示类型的转换,如果能够转换,则返回对应的对象,否则返回null。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //****** //1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //****** //2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。 //Student ss = (Student)p; //ss.StudentSay(); //as转换,看对象PP是否为null,不为null则转换成功 Student pp = p as Student; pp.StudentSay(); Console.ReadKey(); } } public class Person { public void PersonSay() { Console.WriteLine("Person"); } } public class Student:Person { public void StudentSay() { Console.WriteLine("Student"); } } public class Teacher:Person { public void TeacherSay() { Console.WriteLine("Teacher"); } } }
小例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Person [] pers = new Person[5]; Random r = new Random(); for (int i = 0; i < pers.Length;i++ ) { int rnum = r.Next(1, 4);//1-3 switch (rnum) { case 1: pers[i] = new Person(); break; case 2: pers[i] = new Student(); break; case 3: pers[i] = new Teacher(); break; } } for(int j=0;j<pers.Length;j++) { if(pers[j] is Student) { Student tt = (Student)pers[j]; tt.StudentSay(); } else if(pers[j] is Teacher) { Teacher tt = (Teacher)pers[j]; tt.TeacherSay(); } else { pers[j].PersonSay(); } } Console.ReadKey(); } } public class Person { public void PersonSay() { Console.WriteLine("Person"); } } public class Student : Person { public void StudentSay() { Console.WriteLine("Student"); } } public class Teacher : Person { public void TeacherSay() { Console.WriteLine("Teacher"); } } }
*********
在C#语言中,共有五种访问修饰符:public、private、protected、internal、protected internal。作用范围如下表:
访问修饰符 说明
public 公有访问。不受任何限制。
private 私有访问。只限于本类成员访问,子类,实例都不能访问。
protected 保护访问。只限于本类和子类访问,实例不能访问。
internal 内部访问。只限于本项目内访问,其他不能访问。
protected internal 内部保护访问。只限于本项目或是子类访问,其他不能访问
You are never too old to set another goal or to dream a new dream!!!