Använd vänsterpil och högerpil för att navigera

Eller Ctrl+P för att skriva ut till PDF (eller på papper)

Inkapsling (encapsulation)

Klasser som skyddar sina fält från ogiltiga tillstånd

Klasser med properties

  1. class Building {
  2. public double Volume;
  3. public double Temperature;
  4. }
  5. Building good = new Building {
  6. Volume = 30,
  7. Temperature = 20
  8. };
  9. Building bad = new Building {
  10. Volume = 80,
  11. Temperature = 50
  12. };

Vad är fel?

  1. Building b = new Building {
  2. Volume = -20,
  3. Temperature = -500
  4. };

Validering med if-sats

  1. double volume = double.Parse(Console.ReadLine());
  2. double temperature = double.Parse(Console.ReadLine());
  3. if (volume >= 0 && temperature >= -273.15) {
  4. Building b = new Building {
  5. Volume = volume,
  6. Temperature = temperature
  7. };
  8. }
  9. else {
  10. Console.WriteLine(
  11. "Invalid volume and/or temperature"
  12. );
  13. }

Getter och setter

Getter och setter i C#

  1. class Building {
  2. private double volume;
  3. public double Volume {
  4. get {
  5. return volume;
  6. }
  7. set {
  8. if (value >= 0) {
  9. volume = value;
  10. }
  11. else {
  12. Console.WriteLine("Invalid volume");
  13. }
  14. }
  15. }
  16. }

Användning med giltigt och ogiltigt värde

  1. Building valid = new Building {
  2. Volume = 20
  3. };
  4. Building invalid = new Building {
  5. Volume = -20
  6. };

Kasta fel istället för att skriva till konsol

  1. class Building {
  2. private double volume;
  3. public double Volume {
  4. get {
  5. return volume;
  6. }
  7. set {
  8. if (value >= 0) {
  9. volume = value;
  10. }
  11. else {
  12. throw new ArgumentException(
  13. "Invalid volume"
  14. );
  15. }
  16. }
  17. }
  18. }

Anställd med lön

  1. class Employee {
  2. private double salary;
  3. public double Salary {
  4. get {
  5. return salary;
  6. }
  7. set {
  8. if (value >= 12000) {
  9. salary = value;
  10. }
  11. else {
  12. throw new ArgumentException(
  13. "Salary too low"
  14. );
  15. }
  16. }
  17. }
  18. }

Användning med giltigt och ogiltigt värde

  1. Employee valid = new Employee {
  2. Salary = 23000
  3. };
  4. Console.WriteLine(valid.Salary);
  5. Employee invalid = new Employee {
  6. Salary = 5000
  7. };
  8. Console.WriteLine(invalid.Salary);

Användning utan värde

  1. Employee e = new Employee {};
  2. Console.WriteLine(e.Salary);

Konstruktor

  1. class Employee {
  2. private double salary;
  3. public double Salary { ... }
  4. public Employee(double s) {
  5. Salary = s;
  6. }
  7. }

Användning med konstruktor

  1. Employee valid = new Employee(23000);
  2. Console.WriteLine(valid.Salary);
  3. Employee invalid1 = new Employee(5000);
  4. Console.WriteLine(invalid1.Salary);
  5. Employee invalid2 = new Employee();
  6. Console.WriteLine(invalid2.Salary);

Oavsiktlig tilldelning till field istället för property

  1. class Employee {
  2. private double salary;
  3. public double Salary { ... }
  4. public Employee(double s) {
  5. salary = s;
  6. }
  7. }

Argument med samma namn som field

  1. class Employee {
  2. private double salary;
  3. public double Salary { ... }
  4. public Employee(double salary) {
  5. Salary = salary;
  6. }
  7. }

Namngivet argument

  1. Employee employee = new Employee(salary: 23000);
  2. Console.WriteLine(employee.Salary);

Många argument utan namn

  1. Employee employee = new Employee(
  2. "Jakob",
  3. "Kallin",
  4. 1987,
  5. 3,
  6. 3,
  7. 12345
  8. );

Många argument med namn

  1. Employee employee = new Employee(
  2. firstName: "Jakob",
  3. lastName: "Kallin",
  4. birthYear: 1987,
  5. birthMonth: 3,
  6. birthDay: 3,
  7. salary: 12345
  8. );

Fökortad version för enkla properties

  1. class Employee {
  2. private string name;
  3. public string Name {
  4. get {
  5. return name;
  6. }
  7. set {
  8. name = value;
  9. }
  10. }
  11. }
  12. class Employee {
  13. public string Name { get; set; }
  14. }

Motsvarande med metoder (Java)

  1. class Employee {
  2. private string name;
  3. public string GetName() {
  4. return name;
  5. }
  6. public string SetName(string value) {
  7. name = value;
  8. }
  9. }

Metoder

  1. class Spaceship {
  2. public double Fuel { get; set; }
  3. public void Launch(string destination) {
  4. double fuelNeeded = FuelNeeded(destination);
  5. if (fuelNeeded <= Fuel) {
  6. Console.WriteLine("Launching spaceship!");
  7. ...
  8. }
  9. else {
  10. throw new ArgumentException(
  11. "Not enough fuel to get to " + destination;
  12. );
  13. }
  14. }
  15. }

Sammanfattning: properties

  1. class MyClass {
  2. private string myProperty;
  3. public string MyProperty {
  4. get {
  5. return myProperty;
  6. }
  7. set {
  8. // Validera etc.
  9. ...
  10. // Sätt privat fält
  11. myProperty = value;
  12. }
  13. }
  14. }

Sammanfattning: konstruktorer

  1. class MyClass {
  2. public MyClass(string arg1, int arg2, ...) {
  3. // Validera, beräkna, omvandla etc.
  4. ...
  5. // Sätt privata fält
  6. myProperty1 = ...;
  7. myProperty2 = ...;
  8. }
  9. }

Sammanfattning: inkapsling (encapsulation)