-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_oop_basics.cs
More file actions
60 lines (49 loc) · 1.27 KB
/
07_oop_basics.cs
File metadata and controls
60 lines (49 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
class Program
{
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
public void HaveBirthday()
{
Age++;
Console.WriteLine($"Happy birthday {Name}! Now {Age} years old.");
}
}
// Inheritance
class Employee : Person
{
public string Job { get; set; }
public Employee(string name, int age, string job) : base(name, age)
{
Job = job;
}
public new void Introduce() // method hiding (or use virtual/override for polymorphism)
{
Console.WriteLine($"Hi, I'm {Name}, a {Job}, {Age} years old.");
}
}
static void oopBasics()
{
Console.WriteLine("=== OOP BASICS ===");
Person p = new Person("Emma", 27);
p.Introduce();
p.HaveBirthday();
Employee emp = new Employee("Bob", 35, "Software Engineer");
emp.Introduce();
}
static void Main(string[] args)
{
oopBasics();
}
}