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

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

Lektion 8

Objektorienterad programmering (OOP)

Vad är objektorienterad programmering?

Citat om OOP

C++ offers remarkable support for procedural, generic, and object-oriented programming paradigms.

cplusplus.com

Citat om OOP

C++ offers remarkable support for procedural, generic, and object-oriented programming paradigms.

cplusplus.com

I made up the term "object-oriented", and I can tell you I did not have C++ in mind.

Alan Kay

Citat om OOP

C++ offers remarkable support for procedural, generic, and object-oriented programming paradigms.

cplusplus.com

I made up the term "object-oriented", and I can tell you I did not have C++ in mind.

Alan Kay

Erlang might be the only object-oriented language.

Joe Armstrong

Definitioner av OOP

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Wikipedia

Definitioner av OOP

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

Wikipedia

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.

Alan Kay

Objektorientering i C#

I praktiken

Klass för personer

  1. class Person {
  2. public string Name;
  3. public int Age;
  4. public double Salary;
  5. public double Wealth;
  6. }

Metoder för personer

  1. static void PaySalary(Person p) { ... }
  2. static void CelebrateBirthday(Person p) { ... }
  3. static void WinLottery(Person p) { ... }
  4. static void ShowPerson(Person p) { ... }

Användning av personklassen

  1. static void Main(string[] args) {
  2. Person p = new Person {
  3. Name = "Anders",
  4. Age = 25,
  5. Salary = 20000,
  6. Wealth = 5000
  7. };
  8. PaySalary(p);
  9. WinLottery(p);
  10. CelebrateBirthday(p);
  11. ShowPerson(p);
  12. }

Gruppera metoderna med instansvariablerna

  1. class Person {
  2. public string Name;
  3. public int Age;
  4. public double Salary;
  5. public double Wealth;
  6. public void PaySalary() { ... }
  7. public void WinLottery() { ... }
  8. public void CelebrateBirthday() { ... }
  9. public void ShowPerson() { ... }
  10. }

Användning av personklassen med instansmetoder

  1. static void Main(string[] args) {
  2. Person p = new Person {
  3. Name = "Anders",
  4. Age = 25,
  5. Salary = 20000,
  6. Wealth = 5000
  7. };
  8. p.PaySalary();
  9. p.WinLottery();
  10. p.CelebrateBirthday();
  11. p.ShowPerson();
  12. }

Implementation av CelebrateBirthday

  1. class Person {
  2. public string Name;
  3. public int Age;
  4. public double Salary;
  5. public double Wealth;
  6. public void CelebrateBirthday() {
  7. Wealth = Wealth + 500;
  8. Age = Age + 1;
  9. }
  10. }

Nyckelordet this

  1. class Person {
  2. public string Name;
  3. public int Age;
  4. public double Salary;
  5. public double Wealth;
  6. public void CelebrateBirthday() {
  7. this.Wealth = this.Wealth + 500;
  8. this.Age = this.Age + 1;
  9. }
  10. }

Tillstånd

  1. static void Main(string[] args) {
  2. Person p = new Person {
  3. Name = "Anders",
  4. Age = 25,
  5. Salary = 20000,
  6. Wealth = 5000
  7. };
  8. p.CelebrateBirthday();
  9. p.CelebrateBirthday();
  10. }

Flera metoder

  1. class Person {
  2. public string Name;
  3. public int Age;
  4. public double Salary;
  5. public double Wealth;
  6. public void CelebrateBirthday() {
  7. Wealth = Wealth + 500;
  8. Age = Age + 1;
  9. }
  10. public void ShowPerson() {
  11. Console.WriteLine(
  12. Name + " är " + Age + " år" +
  13. " och har " + Wealth + " på banken."
  14. );
  15. }
  16. }

Anrop av flera metoder

  1. static void Main(string[] args) {
  2. Person p = new Person {
  3. Name = "Anders",
  4. Age = 25,
  5. Salary = 20000,
  6. Wealth = 5000
  7. };
  8. p.CelebrateBirthday();
  9. p.CelebrateBirthday();
  10. p.ShowPerson();
  11. }

Metod utan argument

  1. class Person {
  2. public double Wealth;
  3. ...
  4. public void WinLottery() {
  5. Wealth = Wealth + 10000;
  6. }
  7. }
  8. ...
  9. static void Main(string[] args) {
  10. Person p = new Person { ... };
  11. p.WinLottery();
  12. }

Metod med argument

  1. class Person {
  2. public double Wealth;
  3. ...
  4. public void WinLottery(double prize) {
  5. Wealth = Wealth + prize;
  6. }
  7. }
  8. ...
  9. static void Main(string[] args) {
  10. Person p = new Person { ... };
  11. p.WinLottery(10000);
  12. p.WinLottery(50);
  13. }

Metod med returvärde

  1. class Person {
  2. public string FirstName;
  3. public string LastName;
  4. public string FullName() {
  5. return FirstName + " " + LastName;
  6. }
  7. }
  8. static void Main(string[] args) {
  9. Person p = new Person {
  10. FirstName = "Scarlett",
  11. LastName = "Johansson"
  12. };
  13. string n = p.FullName();
  14. }

Getter-metod

  1. class Person {
  2. public string FirstName;
  3. public string LastName;
  4. public string GetFullName() {
  5. return FirstName + " " + LastName;
  6. }
  7. }
  8. static void Main(string[] args) {
  9. Person p = new Person {
  10. FirstName = "Scarlett",
  11. LastName = "Johansson"
  12. };
  13. string n = p.GetFullName();
  14. }

Klassvariabler (statiska)

  1. class Person {
  2. public static int AdultAge = 18;
  3. public int Age;
  4. public bool CanMarry() {
  5. return Age >= AdultAge;
  6. }
  7. }

Klassmetoder (statiska)

  1. class Person {
  2. public static int AdultAge = 18;
  3. public int Age;
  4. public bool CanMarry() {
  5. return Age >= AdultAge;
  6. }
  7. public static void DoSomething() {
  8. // Har endast tillgång till AdultAge.
  9. }
  10. }

Objektorientering i C# i praktiken

Jämförelse: användning utan OOP

  1. Person p = new Person { ... };
  2. CelebrateBirthday(p);
  3. WinLottery(p, 500);
  4. bool canMarry = CanMarry(p);

Jämförelse: användning med OOP

  1. Person p = new Person { ... };
  2. p.CelebrateBirthday();
  3. p.WinLottery(500);
  4. bool canMarry = p.CanMarry();

Jämförelse: definitioner utan OOP

  1. class Person {
  2. public int Age;
  3. public double Wealth;
  4. }
  5. public static void CelebrateBirthday(Person p) {
  6. p.Wealth = p.Wealth + 500;
  7. p.Age = p.Age + 1;
  8. }
  9. public static void WinLottery(Person p, double prize) {
  10. p.Wealth = p.Wealth + prize;
  11. }
  12. public static bool CanMarry(Person p) {
  13. return p.Age >= 18;
  14. }

Jämförelse: definitioner med OOP

  1. class Person {
  2. public int Age;
  3. public double Wealth;
  4. public void CelebrateBirthday() {
  5. Wealth = Wealth + 500;
  6. Age = Age + 1;
  7. }
  8. public void WinLottery(double prize) {
  9. Wealth = Wealth + prize;
  10. }
  11. public bool CanMarry() {
  12. return Age >= 18;
  13. }
  14. }

Synlighetsnivå (public och private)