Advertisement
ivandrofly

C# 'Base' keyword Simple Example

Mar 26th, 2015
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5.     class Person
  6.     {
  7.         private string _firstName;
  8.         protected string _lastName;
  9.         public Person(string firstName, string lastName)
  10.         {
  11.             this._firstName = firstName;
  12.             this._lastName = lastName;
  13.         }
  14.  
  15.         public string GetName()
  16.         {
  17.             return string.Format("Name: {0}, Last-Name: {1}", this._firstName, this._lastName);
  18.         }
  19.     }
  20.  
  21.     class Employee : Person
  22.     {
  23.         private int _stuffNum;
  24.         public Employee(string firstName, string lastName, int stuffNum)
  25.             : base(firstName, lastName)
  26.         {
  27.             _stuffNum = stuffNum;
  28.         }
  29.  
  30.         public string GetEmployeeInfo()
  31.         {
  32.             Console.WriteLine("this: " + this._lastName);
  33.             Console.WriteLine("Base: " + base._lastName);
  34.             return base.GetName() + ", " + this._stuffNum;
  35.         }
  36.     }
  37.  
  38.     class Program
  39.     {
  40.         static void Main()
  41.         {
  42.             var p1 = new Person("Ivandro", "Ismael");
  43.             var e1 = new Employee("John", "Paul", 10);
  44.             Console.WriteLine(p1.GetName());
  45.             Console.WriteLine(e1.GetEmployeeInfo());
  46.             Console.ReadLine();
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement