Implicit conversion and implicit conversion
Rys conversion:
Person p = new Student ();
If the subclass can be assigned to the parent class, we should declare a parent class type to point to the subclass object.
If the parent class is a subclass object, you can consider converting the parent class into a subclass object using
Is returns true if the conversion is successful.
If the as conversion succeeds, the corresponding object is returned to receive the ss
Students ss = p as students; declares a students object.
Is usage:
If (p is students)
{
(Students) p). studentsSayHello ();
}
Else
{
Cw ("failed ");
}
1 namespace _ Rys conversion 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 /// 1, Rys conversion 8 /// 1) sub-classes can be assigned to the parent class: if a parent class is required as a parameter, we can assign a sub-class to replace 9 // Student s = new Student (); 10 Person p = new Student (); // s; 11 12 // string str = string. join ("|", new string [] {"1", "2", "3", "4"}); 13 // Console. writeLine (str); 14 // Console. readKey (); 15 16 // 2). If the parent class contains a subclass object, you can convert this parent class to a subclass object. 17 18 // is usage 19 // if (p is Student) 20 // {21 // Student ss = (Student) p; 22 // ss. studentSayHello (); 23 //} 24 // else25 // {26 // Console. writeLine ("Conversion failed"); 27 //} 28 // as usage 29 Student t = p as Student; 30 t. studentSayHello (); 31 Console. readKey (); 32} 33} 34 35 public class Person36 {37 public void PersonSayHello () 38 {39 Console. writeLine ("My parent class"); 40} 41} 42 public class Student: Person43 {44 public void StudentSayHello () 45 {46 Console. writeLine ("My student"); 47} 48} 49 public class Teacher: Person50 {51 public void TeacherSayHello () 52 {53 Console. writeLine ("I am a teacher"); 54} 55} 56 57 58}