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 9

Användning av objekt

Inbyggda objekt

List

Muterbar array

Utökning av array

  1. string[] heroes = { "Mario", "Luigi", "Peach" };
  2. heroes[3] = "Yoshi";

Utökning av lista

  1. List<string> heroes = new List<string> { "Mario", "Luigi", "Peach" };
  2. Console.WriteLine(heroes.Count);
  3. heroes.Add("Yoshi");
  4. Console.WriteLine(heroes.Count);
  5. heroes.Remove("Luigi");
  6. Console.WriteLine(heroes.Count);

Metoder på listor

  1. List<string> heroes = new List<string> { "Mario", "Luigi", "Peach" };
  2. heroes.Sort();
  3. heroes.Reverse();
  4. int i = heroes.IndexOf("Mario");

Dictionary

Samling med icke-numeriska index

Användning av dictionary

  1. Dictionary<string, int> limits = new Dictionary<string, int> {
  2. ["G"] = 20,
  3. ["VG"] = 40,
  4. ["MVG"] = 60
  5. };
  6. Console.WriteLine(limits["G"]);
  7. Console.WriteLine(limits["VG"]);
  8. Console.WriteLine(limits["MVG"]);
  9. Console.WriteLine(limits.Count);
  10. limits.Add("Super-MVG", 70);
  11. Console.WriteLine(limits["Super-MVG"]);
  12. Console.WriteLine(limits.Count);

Nycklar och värden av objekttyp

  1. Course programming = new Course { ... };
  2. Course math = new Course { ... };
  3. Teacher jakob = new Teacher { ... };
  4. Teacher robin = new Teacher { ... };
  5. Dictionary<Course, Teacher> teachersByCourse =
  6. new Dictionary<Course, Teacher> {
  7. [programming] = jakob,
  8. [math] = robin
  9. };

Loopa över nycklar och värden

  1. Dictionary<string, int> limits =
  2. new Dictionary<string, int> {
  3. ["G"] = 20,
  4. ["VG"] = 40,
  5. ["MVG"] = 60
  6. };
  7. foreach (string grade in limits.Keys) {
  8. Console.WriteLine(grade);
  9. }
  10. foreach (int points in limits.Values) {
  11. Console.WriteLine(points);
  12. }
  13. foreach (KeyValuePair<string, int> pair in limits) {
  14. Console.WriteLine(pair.Key);
  15. Console.WriteLine(pair.Value);
  16. }

String

Klassen bakom strängar

Metoder på strängar

  1. string s1 = "Hello world!";
  2. string s2 = s1.Substring(6, 5);
  3. string s3 = s2.Replace("l", "k");
  4. string s4 = s3.ToUpper();

Mutation av strängar

  1. string name = "Jakob";
  2. char letter = name[2];
  3. Console.WriteLine(letter);
  4. name[2] = 'c';
  5. Console.WriteLine(name);

Strängar är ej muterbara

Dokumentation

Hur man lär sig om inbyggda objekt

Objektorienterad analys och design (OOAD)

Strukturera program utifrån objekt

Programstruktur

Näthandel

Signaturer för produkt och order

  1. class Product {
  2. public string Name;
  3. public bool Bulky;
  4. }
  5. class Order {
  6. public List<Product> Items;
  7. public double Shipping() { ... }
  8. public double Total() { ... }
  9. public void AddItem(Product p) { ... }
  10. }

Användning av produkt och order

  1. Order order = new Order {
  2. Items = new List<Product> {}
  3. };
  4. Product tv = new Product {
  5. Name = "TV",
  6. Bulky = true
  7. };
  8. Product radio = new Product {
  9. Name = "radio",
  10. Bulky = false
  11. };
  12. order.AddItem(tv);
  13. order.AddItem(radio);
  14. Console.WriteLine("Total: " + order.Total());
  15. Console.WriteLine("Shipping: " + order.Shipping());

Näthandel med lager

Signaturer för lager

  1. public class Warehouse {
  2. public Dictionary<Product, int> Stock;
  3. public void AddProduct(Product p, int amount) { ... }
  4. public void RemoveProduct(Product p, int amount) { ... }
  5. public void Ship(Order o) { ... }
  6. }
  7. Warehouse warehouse = new Warehouse {
  8. Stock = new Dictionary<Product, int> {}
  9. };
  10. warehouse.AddProduct(tv, 2);
  11. warehouse.AddProduct(radio, 5);
  12. warehouse.Ship(order);

Och ännu mer