C#, OOP Osztályok tervezése C#-ban
OOP Létrehozás (creating) Megszüntetés (destroying) Túlterhelés (overlading) Felsorolás típus (enumerated types) 2
Hajó osztály Sailboat class using System; class Sailboat public string Name; public double Length; public Sailboat(string name, double length) this.name = name; this.length = length; public double Hullspeed() return 1.34 * Math.Sqrt(Length); 3
Objects in C# Objektum létrehozása new Objektumok automatikusan törlődnek (garbage collection) Itt jön létre void SomeMethod() Sailboat s = new Sailboat("Windancer", 70.0); Console.WriteLine("0: max speed 1:0.00 kts", s.name, s.hullspeed()); Windancer 70.0 Itt szűnik meg 4
Overloading C# támogatja a metódusok túlterhelését Pl.: constructors class Sailboat public string Name; public double Length; public int Material; // fiberglass, carbon, steel public Sailboat(string name, double length, int material) this.name = name; this.length = length; this.material = material; public Sailboat(string name, double length) : this(name, length, 1 /*default: fiberglass*/) public Sailboat(XmlDocument doc)... Meghívja az előző konstruktort 5
Enumerated types C# supports enums class Sailboat public enum Materials Fiberglass, Carbon, Steel, Other public string Name; public double Length; public Materials Material; public Sailboat(string name, double length, Materials material) this.name = name; this.length = length; this.material = material; Sailboat s = new Sailboat("Windancer", 70.0, Sailboat.Materials.Fiberglass); 6
Osztályok tervezése Ősosztály (Object class) Integrálás a.net Frameworkbe Overriding ToString, Equals és GetHashCode Elérhetőségi felügyelet: Tulajdonság (Properties Getters / Setters) 7
.NET type system Osztályhierarchia gyökér az Object System-defined types User-defined types Object String Array ValueType Exception Delegate Class1 Boolean Primitive types Single Enum Structure1 Multicast Delegate Class2 Byte Double Int16 Decimal Enum1 Delegate1 Class3 Int32 DateTime Int64 TimeSpan Char Guid 8
The Object class Object a gyökere a.net osztályhierarchiának: class Object public virtual string ToString() return this.gettype().fullname; public virtual bool Equals(Object obj) return this == obj; Minden C# osztály Object osztályból öröklődik. A C#-ban a virtual kulcsszó jelzi, hogy egy metódus felülírható. public virtual int GetHashCode() return...;. 9
Overriding ToString C#-ban elvárt a ToString felülbírálata class Sailboat... s = new Sailboat("Windancer", 70.0);. Console.WriteLine(s); public override string ToString() return this.name; 10
Overriding Equals A felhasználó által definiált osztályokban az Equals is felülírandó public class Sailboat. public override bool Equals(object obj) if ( obj == null!obj.gettype().equals(this.gettype()) ) return false; // cannot compare apples and oranges! Sailboat other = (Sailboat) obj; s1 = new Sailboat(...); s2 = new Sailboat(...);. if (s1.equals(s2))... return this.name.equals(other.name); 11
Overriding GetHashCode Az Equals felülbírálata GetHashCode metódust is felül kell bírálni Szabály: ha két objektum megegyezik (Equals), ugyanazt a HashCode-ot kapják Ugyanazt a mezőt használjuk a hashcode kiszámításához, mint az egyenlőség vizsgálatához public class Sailboat. public override bool Equals(object obj) if (obj == null obj.gettype().equals(this.gettype()) == false) return false; // cannot compare apples and oranges! Sailboat other = (Sailboat) obj; return this.name.equals(other.name); public override int GetHashCode() return this.name.gethashcode(); 12
Hozzáférési szabályozás A publikus mezők veszélyesek class Sailboat public string Name; public double Length;... A név lehet üres, a hossz 0 vagy negatív 13
A megoldás: Properties A.NET-ben, a Property methods a szabványos megoldás Nyilvánosan mezőnek látszik Az használatuk Get és Set metódusokon keresztül folyik class Sailboat private string m_name; private double m_length; public string Name get return m_name; set if (value!= null) m_name = value; s1 = new Sailboat("A2", 36.33); Console.WriteLine(s1.Name); s1.name = "A2.1"; s1.length = 70.0; public double Length // read-only! get return m_length; private set m_length = value; 14
Properties rövídítés C#-ban class Sailboat private string m_name; public string Name get return m_name; set m_name = value; private double m_length; public double Length get return m_length; private set m_length = value; class Sailboat public string Name get; set; public double Length get; private set; refactor 15
Tervezési szabályok összegzése 1. szabály: inicializálás ==> Constructors 2. szabály: az osztály információnak elrejtése==> Private fields, and public properties 3. szabály:.net integráció==> override ToString, Equals, and GetHashCode 16
Visual Studio Támogatás XML kommentek Class tervező Object test bench 17
XML Kommentek NET támogatja az XML kommenteket a dokumentációhoz Elérhető: Project Properties, Build tab (Project menu) <summary> /// VS This produces class represents.xml a single comment Sailboat... file in bin\debug subdirectory class Sailboat /// </summary> public private string m_name; use Sandcastle tool to turn XML into MSDN-style /// <summary> /// Compares THIS boat's name to the other boat's name, case-sensitive. documentation /// </summary> /// <param name="obj">the object to compare to</param> /// http://sandcastle.codeplex.com/, <returns>true if boats are same type have same name, /// false if not</returns> public http://blogs.msdn.com/sandcastle/ override bool Equals(object obj)... 18
Class Designer Visual Studio Class Designer-e segítségével vízuálisan tervezhetőek az osztályok Project menu, Add New Item, Class Diagram 19