Advertisement
Levi0227

2. félév 2. óra (otthon)

Mar 4th, 2024
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.24 KB | Source Code | 0 0
  1. namespace Inheritance
  2. {
  3.     internal class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             //Person person01 = new Person("Primoz", "Roglic");
  8.             //Console.WriteLine(person01); // = person01.ToString()
  9.  
  10.             //Employee employee01 = new Employee("Tadej", "Pogacar", 5000);
  11.             //Console.WriteLine(employee01.Name);
  12.             //Console.WriteLine(employee01);
  13.  
  14.             //Console.WriteLine(employee01.GetEmailAddress());
  15.  
  16.             //Person person02 = new Employee("Wout", "Van art", 4000);
  17.             //Console.WriteLine(person02.FirstName);
  18.             //Console.WriteLine(person02.LastName);
  19.  
  20.             //if (person02 is Employee)
  21.             //{
  22.             //    //Employee employee02 = person02 as Employee;
  23.  
  24.             //    (person02 as Employee).GetEmailAddress();
  25.             //}
  26.  
  27.             //Employee employee03 = person01 as Employee; // null
  28.             //if (employee03 != null)
  29.             //{
  30.             //    Console.WriteLine(employee03.GetEmailAddress());
  31.             //}
  32.  
  33.             //Console.WriteLine((person02 as Employee)?.GetEmailAddress() ?? "Not an employee");
  34.             //Console.WriteLine((employee03 as Employee)?.GetEmailAddress() ?? "Not an employee");
  35.  
  36.  
  37.             //CEO ceo = new CEO("Gerant", "Thomas");
  38.             //Console.WriteLine(ceo.GetEmailAddress());
  39.  
  40.             //Employee employee04 = new CEO("Attila", "Valter");
  41.             //Console.WriteLine(employee04.GetEmailAddress()); // késői kötés => ha nincsen override a CEO metódusnál, akkor korai kötés
  42.  
  43.             //object obj = new Person("Tom", "Pidcock");
  44.  
  45.  
  46.  
  47.  
  48.             //Person person01 = new Person("Primoz", "Roglic");
  49.             //Person person02 = new Person("Primoz", "Roglic");
  50.  
  51.             //if (person01 == person02)
  52.             //{
  53.             //    Console.WriteLine("Same");
  54.             //}
  55.             //else { Console.WriteLine("Different"); }
  56.  
  57.             //if (person01.Equals(person02))
  58.             //{
  59.             //    Console.WriteLine("SAME");
  60.             //}
  61.             //else { Console.WriteLine("Different"); }
  62.  
  63.  
  64.  
  65.  
  66.             Person[] persons = { new Employee("Primoz", "Roglic", 3000), new CEO("Attila", "Valter"), new Student("Hanna", "Hallgato")};
  67.             foreach (Person currentPerson in persons)
  68.             {
  69.                 Console.WriteLine(currentPerson.GetEmailAddress());
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75. ---------------------------------------
  76. Osztályok
  77. ---------------------------------------
  78.  
  79. using System;
  80. using System.Collections.Generic;
  81. using System.Linq;
  82. using System.Text;
  83. using System.Threading.Tasks;
  84.  
  85. namespace Inheritance
  86. {
  87.     abstract class Person : object
  88.     {
  89.         protected string firstName;
  90.         protected string lastName;
  91.  
  92.         public string FirstName { get { return firstName; } set { firstName = value; } }
  93.         public string LastName { get { return lastName; } set { lastName = value; } }
  94.         public string Name => $"{firstName} {lastName}";
  95.  
  96.         public Person(string firstName, string lastName)
  97.         {
  98.             this.firstName = firstName;
  99.             this.lastName = lastName;
  100.         }
  101.  
  102.         public abstract string GetEmailAddress(); // virtual
  103.  
  104.         public override string ToString()
  105.         {
  106.             return Name;
  107.         }
  108.  
  109.         public override bool Equals(object? obj)
  110.         {
  111.             if (!(obj is Person)) return false;
  112.  
  113.             Person other = obj as Person;
  114.  
  115.             return firstName == other.firstName && lastName == other.lastName;
  116.         }
  117.     }
  118. }
  119.  
  120. ---------------------------------------
  121.  
  122. using System;
  123. using System.Collections.Generic;
  124. using System.Linq;
  125. using System.Text;
  126. using System.Threading.Tasks;
  127.  
  128. namespace Inheritance
  129. {
  130.     internal class Employee : Person
  131.     {
  132.         int salary;
  133.  
  134.         public int Salary { get => salary; set => salary=value;}
  135.  
  136.         public override string GetEmailAddress() => $"{firstName.ToLowerInvariant()}.{lastName.ToLowerInvariant()}@company.com";
  137.  
  138.         public Employee(string firstName, string lastName, int salary) : base(firstName, lastName)
  139.         {
  140.             this.salary = salary;
  141.         }
  142.  
  143.         public override string ToString()
  144.         {
  145.             return base.ToString() + " (" + GetEmailAddress() + ")";
  146.         }
  147.     }
  148. }
  149.  
  150. ---------------------------------------
  151.  
  152. using System;
  153. using System.Collections.Generic;
  154. using System.Linq;
  155. using System.Text;
  156. using System.Threading.Tasks;
  157.  
  158. namespace Inheritance
  159. {
  160.     sealed class CEO : Employee
  161.     {
  162.         public CEO(string firstName, string lastName) : base(firstName, lastName, 10000)
  163.         {
  164.         }
  165.  
  166.         public override string GetEmailAddress() => "ceo@company.com";
  167.     }
  168. }
  169.  
  170. ---------------------------------------
  171.  
  172. using System;
  173. using System.Collections.Generic;
  174. using System.Linq;
  175. using System.Text;
  176. using System.Threading.Tasks;
  177.  
  178. namespace Inheritance
  179. {
  180.     internal class Student : Person
  181.     {
  182.         public Student(string firstName, string lastName) : base(firstName, lastName)
  183.         {
  184.         }
  185.  
  186.         public override string GetEmailAddress()
  187.         {
  188.             return $"{firstName}.{lastName}@university.com";
  189.         }
  190.     }
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement