Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Inheritance
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- //Person person01 = new Person("Primoz", "Roglic");
- //Console.WriteLine(person01); // = person01.ToString()
- //Employee employee01 = new Employee("Tadej", "Pogacar", 5000);
- //Console.WriteLine(employee01.Name);
- //Console.WriteLine(employee01);
- //Console.WriteLine(employee01.GetEmailAddress());
- //Person person02 = new Employee("Wout", "Van art", 4000);
- //Console.WriteLine(person02.FirstName);
- //Console.WriteLine(person02.LastName);
- //if (person02 is Employee)
- //{
- // //Employee employee02 = person02 as Employee;
- // (person02 as Employee).GetEmailAddress();
- //}
- //Employee employee03 = person01 as Employee; // null
- //if (employee03 != null)
- //{
- // Console.WriteLine(employee03.GetEmailAddress());
- //}
- //Console.WriteLine((person02 as Employee)?.GetEmailAddress() ?? "Not an employee");
- //Console.WriteLine((employee03 as Employee)?.GetEmailAddress() ?? "Not an employee");
- //CEO ceo = new CEO("Gerant", "Thomas");
- //Console.WriteLine(ceo.GetEmailAddress());
- //Employee employee04 = new CEO("Attila", "Valter");
- //Console.WriteLine(employee04.GetEmailAddress()); // késői kötés => ha nincsen override a CEO metódusnál, akkor korai kötés
- //object obj = new Person("Tom", "Pidcock");
- //Person person01 = new Person("Primoz", "Roglic");
- //Person person02 = new Person("Primoz", "Roglic");
- //if (person01 == person02)
- //{
- // Console.WriteLine("Same");
- //}
- //else { Console.WriteLine("Different"); }
- //if (person01.Equals(person02))
- //{
- // Console.WriteLine("SAME");
- //}
- //else { Console.WriteLine("Different"); }
- Person[] persons = { new Employee("Primoz", "Roglic", 3000), new CEO("Attila", "Valter"), new Student("Hanna", "Hallgato")};
- foreach (Person currentPerson in persons)
- {
- Console.WriteLine(currentPerson.GetEmailAddress());
- }
- }
- }
- }
- ---------------------------------------
- Osztályok
- ---------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Inheritance
- {
- abstract class Person : object
- {
- protected string firstName;
- protected string lastName;
- public string FirstName { get { return firstName; } set { firstName = value; } }
- public string LastName { get { return lastName; } set { lastName = value; } }
- public string Name => $"{firstName} {lastName}";
- public Person(string firstName, string lastName)
- {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- public abstract string GetEmailAddress(); // virtual
- public override string ToString()
- {
- return Name;
- }
- public override bool Equals(object? obj)
- {
- if (!(obj is Person)) return false;
- Person other = obj as Person;
- return firstName == other.firstName && lastName == other.lastName;
- }
- }
- }
- ---------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Inheritance
- {
- internal class Employee : Person
- {
- int salary;
- public int Salary { get => salary; set => salary=value;}
- public override string GetEmailAddress() => $"{firstName.ToLowerInvariant()}.{lastName.ToLowerInvariant()}@company.com";
- public Employee(string firstName, string lastName, int salary) : base(firstName, lastName)
- {
- this.salary = salary;
- }
- public override string ToString()
- {
- return base.ToString() + " (" + GetEmailAddress() + ")";
- }
- }
- }
- ---------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Inheritance
- {
- sealed class CEO : Employee
- {
- public CEO(string firstName, string lastName) : base(firstName, lastName, 10000)
- {
- }
- public override string GetEmailAddress() => "ceo@company.com";
- }
- }
- ---------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Inheritance
- {
- internal class Student : Person
- {
- public Student(string firstName, string lastName) : base(firstName, lastName)
- {
- }
- public override string GetEmailAddress()
- {
- return $"{firstName}.{lastName}@university.com";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement