-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_06_OO_Encapsulation.cs
87 lines (74 loc) · 1.77 KB
/
_06_OO_Encapsulation.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
/*
# Encapsulation is the wrapping up of data and information inside a class,
and offer methods/properties to access them.
# It is achieved by declaring attributes (and some methods) as private and using
methods/properties to access them.
# There are four access modifiers that can be used to control the visibility:
public: Anywhere
private: Only inside the class
protected: Only inside the class and sub-classes
internal: Only inside the same Assembly (EXE, DLL or Project)
# Class Content:
# 1. Review Namespace
# 2. Create a new class Car
*/
namespace OurCompany.LearnCoding.OOP.Encapsulation;
public class Car
{
private string vin;
private string maker;
private string model;
public Car(string vin, string maker, string model)
{
SetVIN(vin);
SetMaker(maker);
SetModel(model);
}
private void SetVIN(string vin)
{
if (string.IsNullOrEmpty(vin))
{
throw new Exception("VIN is required");
}
this.vin = vin;
}
public string GetVIN()
{
return vin;
}
public void SetMaker(string maker)
{
if (string.IsNullOrEmpty(maker))
{
throw new Exception("Maker is required");
}
this.maker = maker;
}
public string GetMaker()
{
return maker;
}
public void SetModel(string model)
{
if (string.IsNullOrEmpty(model))
{
throw new Exception("Model is required");
}
this.model = model;
}
public string GetModel()
{
return model;
}
}
public class CarApp
{
public static void Main(string[] args)
{
Car car1 = new Car("1234ABC", "Toyota", "Rav4");
Console.WriteLine($"VIN: {car1.GetVIN()}; Maker: {car1.GetMaker()}; Model: {car1.GetModel()}");
//car1.SetVin() // Not accessible
//Car car2 = new Car("345BCD", "", null);
}
}