Polymorphism of virtual objects and objects

Source: Internet
Author: User

Virtual memebers:

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Namespace  Leleapplication2 {  Class  Person {  Public   String Firstname { Get ; Set  ;}  Public   String Secondname { Get ; Set  ;}  Public   Virtual   Void  Display () {console. writeline (  "  Person-{0}, {1}  "  , Firstname, secondname );} Public   Virtual   Int Calculateyearincome ( Int  Year ){  If (Year < 2000  ){  Return   50000  ;}  Return   10000  ;}}  Class Employee: person {  Public   Int Yearincome { Get ; Set  ;}  Public   Override   Void  Display () {console. writeline (  "  Employee-{0}, {1}, {2}  "  , Firstname, secondname, yearincome );} Public   Override   Int Calculateyearincome ( Int  Year ){  Return   1000  ;}}  Class  Program {  Static   Void Main ( String  [] ARGs) {list <Person> mylist =New List <person> (); Person p1 = New  Person (); p1.firstname = "  Nelson  "  ; P1.secondname = "  Laquest  "  ; Employee E1 = New  Employee (); e1.firstname = "  Whoa  "  ; E1.secondname = "  Hey  "  ; E1.yearincome = 23  ; Mylist. Add (P1); mylist. Add (E1 );  Foreach ( VaR Item In Mylist) {item. Display (); console. writeline (  "  Year income of 2004-> {0}  " , Item. calculateyearincome ( 2004  );} Console. readkey ();}  Static   String Getstring ( String  Prompt) {console. Write (  "  {0}>  " , Prompt );  Return  Console. Readline ();}}} 

 

 

Polymorphism:

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Namespace  Leleapplication2 {  Class Person {  Public   String Firstname { Get ; Set  ;}  Public   String Secondname { Get ; Set  ;}  Public   Virtual   Void  Display () {console. writeline ( "  Person-{0}, {1}  "  , Firstname, secondname );}}  Class  Employee: person {  Public   Int Yearincome { Get ; Set  ;}  Public   Override   Void Display () {console. writeline (  "  Employee-{0}, {1}, {2}  "  , Firstname, secondname, yearincome );}}  Class  Program {  Static   Void Main ( String  [] ARGs) {list <Person> mylist = New List <person> (); For ( Int I = 0 ; I < 3 ; I ++ ) {Mylist. Add (getpersoninput ());}  Foreach ( VaR Item In  Mylist) {item. Display ();} console. readkey ();}  Static  Person getpersoninput (){  While ( True  ) {Console. writeline (  "  [1]-person \ n [2]-Employee  "  );  Int Choice = Int  . Parse (console. Readline ());  If (Choice = 1  ) {Person = New Person (); person. firstname = Getstring ( "  First name  "  ); Person. secondname = Getstring ( "  Second name  "  );  Return  Person ;}  Else   If (Choice = 2 ) {Employee = New  Employee (); employee. firstname = Getstring ( "  First name  "  ); Employee. secondname = Getstring ( "  Second name  "  ); Console. writeline (  "  Year of income> "  ); Employee. yearincome = Int  . Parse (console. Readline ());  Return  Employee ;}}}  Static   String Getstring ( String  Prompt) {console. Write (  "  {0}>  " , Prompt );  Return  Console. Readline ();}}} 

 

 

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.