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

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

Föreläsning 12

Arv (inheritance)

Gränssnitt för produkter

  1. interface IProduct {
  2. string Name { get; set; }
  3. double Price { get; set; }
  4. string Summary { get; }
  5. }

Klasser som implementerar gränssnittet

  1. class Milk : IProduct {
  2. public string Name { get; set; }
  3. public double Price { get; set; }
  4. public double Fat { get; set; }
  5. public string Summary {
  6. get {
  7. return Name + " has " + Fat + " percent fat";
  8. }
  9. }
  10. }
  11. class Car : IProduct {
  12. public string Name { get; set; }
  13. public double Price { get; set; }
  14. public string Color { get; set; }
  15. public string Summary {
  16. get {
  17. return Name + " is " + Color;
  18. }
  19. }
  20. }

Användning av klasserna

  1. Milk milk = new Milk {
  2. Name = "Arla Standard",
  3. Price = 8.5,
  4. Fat = 3.0
  5. };
  6. Console.WriteLine(milk.summary);
  7. Car car = new Car {
  8. Name = "DeLorean",
  9. Price = 2500000,
  10. Color = "gray"
  11. };
  12. Console.WriteLine(car.summary);

Vad är gemensamt?

  1. class Milk : IProduct {
  2. public string Name { get; set; }
  3. public double Price { get; set; }
  4. public double Fat { get; set; }
  5. public string Summary {
  6. get {
  7. return Name + " costs " + Price + " kr";
  8. }
  9. }
  10. }
  11. class Car : IProduct {
  12. public string Name { get; set; }
  13. public double Price { get; set; }
  14. public string Color { get; set; }
  15. public string Summary {
  16. get {
  17. return Name + " costs " + Price + " kr";
  18. }
  19. }
  20. }

Gemensam klassmetod

  1. class Milk : IProduct {
  2. ...
  3. public string Summary {
  4. get {
  5. return ProductSummary(this);
  6. }
  7. }
  8. }
  9. class Car : IProduct {
  10. ...
  11. public string Summary {
  12. get {
  13. return ProductSummary(this);
  14. }
  15. }
  16. }
  17. static string ProductSummary(IProduct p) {
  18. return p.Name + " costs " + p.Price + " kr.";
  19. }

Motsvarande kod med arv

  1. class Product {
  2. public string Name { get; set; }
  3. public double Price { get; set; }
  4. public string Summary {
  5. get {
  6. return Name + " costs " + Price + " kr.";
  7. }
  8. }
  9. }
  10. class Car : Product {
  11. public string Color { get; set; }
  12. }
  13. class Milk : Product {
  14. public string Fat { get; set; }
  15. }

Hur klassen "egentligen" ser ut

  1. class Car : Product {
  2. public string Name { get; set; }
  3. public double Price { get; set; }
  4. public string Summary {
  5. get {
  6. return Name + " costs " + Price + " kr.";
  7. }
  8. }
  9. public string Color { get; set; }
  10. }

Fullständiga koden igen

  1. class Product {
  2. public string Name { get; set; }
  3. public double Price { get; set; }
  4. public string Summary {
  5. get {
  6. return Name + " costs " + Price + " kr.";
  7. }
  8. }
  9. }
  10. class Car : Product {
  11. public string Color { get; set; }
  12. }
  13. class Milk : Product {
  14. public string Fat { get; set; }
  15. }

Problem med arv

Varför använder vi arv?

Arv i .NET

Fönster i GUI

  1. class MyForm : Form {
  2. public MyForm() {
  3. Text = "This is my GUI";
  4. Button button = new Button {
  5. Text = "My Button"
  6. };
  7. Controls.Add(button);
  8. }
  9. }

Hur klassen "egentligen" ser ut

  1. class MyForm : Form {
  2. public string Text { get; set; }
  3. public List<Control> Controls { get; set; }
  4. ...
  5. public MyForm() {
  6. Text = "This is my GUI";
  7. Button button = new Button {
  8. Text = "My Button"
  9. };
  10. Controls.Add(button);
  11. }
  12. }

Egna properties och metoder

  1. class MyForm : Form {
  2. public string Text { get; set; }
  3. public List Controls { get; set; }
  4. ...
  5. public string CustomProperty1 { get; set; }
  6. public string CustomProperty2 { get; set; }
  7. public void CustomMethod() { ... }
  8. public MyForm() {
  9. Text = "This is my GUI";
  10. Button button = new Button {
  11. Text = "My Button"
  12. };
  13. Controls.Add(button);
  14. }
  15. }

Interaktivitet med händelser (events)

  1. class MyForm : Form {
  2. public MyForm() {
  3. Text = "This is my GUI";
  4. Button button = new Button {
  5. Text = "My Button"
  6. };
  7. button.Click += OnButtonClick;
  8. Controls.Add(button);
  9. }
  10. private void OnButtonClick(object sender, EventArgs e) {
  11. MessageBox.Show("Hello!");
  12. }
  13. }