Implicit conversion and implicit conversion

Source: Internet
Author: User

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}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.