1 / 27

Лекція 4

Лекція 4. Класи в UML та C #. У комірці імені розміщується ім'я класу , в стилі UpperCamelCase ( без спеціальних знаків). + public - private # protected. за замовчуванням = 1. Сигнатура операції включає : ім'я ; список параметрів ( типи всіх параметрів ); тип результату.

Télécharger la présentation

Лекція 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Лекція 4 Класи в UML та C#

  2. У комірці імені розміщується ім'я класу, в стилі UpperCamelCase(без спеціальних знаків) + public - private # protected за замовчуванням = 1 • Сигнатура операції включає: • ім'я; • список параметрів (типивсіхпараметрів); • тип результату. • Сигнатура кожноїопераціїабо методу класумає бути унікальною. Типованотація класу UML

  3. UML C# public class MyShape { public Double height; public Double width; private Point origin; protected String[2] description; public void Draw(); public double getArea(); } Приклад класуUML та С#

  4. Модифікатори доступу

  5. Конструктори PowerDesigner

  6. public class Point { public Point() { // TODO: implement } public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } ~Point() { // TODO: implement } private double X; private double Y; } public class Point { static Point() { // TODO: implement } public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } ~Point() { // TODO: implement } private double X; private double Y; } protectedoverridevoidFinalize() { try { //Очищенняданих … } finally { base.Finalize(); } } Коструктори за замовчуванням та копіювання

  7. Перевизначення параметрів конструктора за замовчуванням

  8. Створення операції-конструктора

  9. public class Point { private double X; private double Y; public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } public Point() { // TODO: implement } public Point(double R) { if (R > 0)&&(R < 256) X = R; if (R > 0)&&(R < 120) Y = R: } public Point(double X, double Y) { if (X > 0)&&(X < 256) this.X = X; if (Y > 0)&&(Y < 120) this.Y = Y; } public string ToString() { return "(" + X.ToString() + "; " + Y.ToString() + ")"; } } public class Point { private double X; private double Y; public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } public Point(): this(0) { // TODO: implement } public Point(double R):this(R, R) { } public Point(double X, double Y) { if (X > 0)&&(X < 256) this.X = X; if (Y > 0)&&(Y < 120) this.Y = Y; } public string ToString() { return "(" + X.ToString() + "; " + Y.ToString() + ")"; } } Ланцюги виклику конструкторів

  10. class Program { static void Main(string[] args) { Point MyP = new Point(); Console.WriteLine("Конструктор за замовчуванням {0}", MyP.ToString()); Point MyP1 = new Point(5); Console.WriteLine("Конструктор з одним значенням {0}", MyP1.ToString()); MyP1 = new Point(1, 5); Console.WriteLine("Конструктор здвомазначеннями {0}", MyP1.ToString()); Console.ReadLine(); } } Перевірка роботи конструкторів

  11. static void Main() { Point MyP = new Point(); Console.WriteLine("Без аргументів {0}", MyP.ToString()); Point MyP1 = new Point(5); Console.WriteLine("З одним аргументом {0}", MyP1.ToString()); MyP1 = new Point(Y:5); Console.WriteLine("З аргументом Y {0}", MyP1.ToString()); MyP1 = new Point(1, 5); Console.WriteLine("З двома аргументами {0}", MyP1.ToString()); } class Point { private double X; private double Y; public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } public Point(double X = 0, double Y = 0) { if (X > 0 && X < 256) this.X = X; if (Y > 0 && Y < 120) this.Y = Y; } publicoverride string ToString() { return "(" + X.ToString() + "; " + Y.ToString() + ")"; } } Ініціалізація аргументів

  12. private static double MaxX = 256; private static Double MinX = 0; private static double MaxY = 120; private static Double MinY = 0; public static void SetMaxX(double r) { MaxX = r; } public static double GetMaxX() { return MaxX; } … public static double MaxX = 256; public static Double MinX = 0; public static double MaxY = 120; public static Double MinY = 0; static void Main() { Point MyP = new Point(); Console.WriteLine("Без аргументів {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); Point.MaxX = 300; Point.MinX = -100; Point MyP1 = new Point(5); Console.WriteLine("З одним аргументом {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); } static void Main() { Point MyP = new Point(); Console.WriteLine("Без аргументів {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.GetMaxX(), Point.GetMinX(), Point.GetMaxY(), Point.GetMinY()); Point.SetMaxX(3000); Point.SetMinX(-1000); Point MyP1 = new Point(5); Console.WriteLine("З одним аргументом {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.GetMaxX(), Point.GetMinX(), Point.GetMaxY(), Point.GetMinY()); } Статичні поля та методи

  13. private static double MaxX; private static Double MinX; private static double MaxY; private static Double MinY; static Point() { MaxX = MinX = MinY = 1; MaxY = Environment.CommandLine.Length; } … static void Main() { Point MyP = new Point(); Console.WriteLine("Без аргументів {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.GetMaxX(), Point.GetMinX(), Point.GetMaxY(), Point.GetMinY()); Point.SetMaxX(3000); Point.SetMinX(-1000); Point MyP1 = new Point(5); Console.WriteLine("З одним аргументом {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.GetMaxX(), Point.GetMinX(), Point.GetMaxY(), Point.GetMinY()); } Статичний конструктор

  14. class Point { private static double maxX; private static Double minX; private static double maxY; private static Double minY; public static Double MaxX { get { return maxX; } set { maxX = value; } } public static Double MinX { get { return minX; } set { minX = value; } } public static Double MaxY { get { return maxY; } set { maxY = value; } } public static Double MinY { get { return minY; } set { minY = value; } } … static void Main() { Point MyP = new Point(); Console.WriteLine("Без аргументів {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); Point.MaxX = 300; Point.MinX = -100; Point MyP1 = new Point(5); Console.WriteLine("З одним аргументом {0}, {1}, {2}, {3}, {4}", MyP.ToString(), Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); } Інкапсуляція статичних полів

  15. private double x; private double y; public Double X { get { return x; } set { if (value > MaxX) x = MaxX; else if (value < MinX) x = MinX; else x = value; } } public Double Y { get { return y; } set { if (value > MaxY) y = MaxY; else if (value < MinY) y = MinY; else y = value; } } public Point(Point oldPoint) { this.X = oldPoint.X; this.Y = oldPoint.Y; } public Point(double X = 0, double Y = 0) { this.X = X; this.Y = Y; } public override string ToString() { return "(" + X.ToString() + "; " + Y.ToString() + ")"; } static void Main() { Console.WriteLine("Обмеження: maxx = {0}, minx = {1}, maxy = {2}, miny = {3}", Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); Point MyP = new Point(); Console.WriteLine("За замовчуванням {0}", MyP.ToString()); MyP.X = 30000; Console.WriteLine("ПісляX = 30000: {0}", MyP.ToString()); Point MyP1 = new Point(Y:3000); Console.WriteLine("Конструктор зперевищенимY {0}", MyP1.ToString()); } Інкапсуляція через властивості

  16. Підтримкаоператорів Нотаціявластивостей Нотаціягетерів та сетерів static void Main() { … MyP.X++; … } static void Main() { … MyP.SetX(MyP.GetX + 1); … } Властивостітільки для читання (запису) Автоматичнівластивості • public String Name{ get; set } • public Double X • { • get { return x; } • } Списки ініціалізації Змінамодифікаторів доступу static void Main() { Console.WriteLine(@"Обмеження: maxx = {0}, minx = {1}, maxy = {2}, miny = {3}", Point.MaxX, Point.MinX, Point.MaxY, Point.MinY); Point MyP2 = new Point { X=15, Y=16}; Console.WriteLine("Список 15, 16 {0}", MyP2.ToString()); } • public Double X • { • get { return x; } • protected set • { • if (value > MaxX) • x = MaxX; • else if (value < MinX) • x = MinX; • else x = value; • } • } Особливості властивостей

  17. public double _X { get { return X; } set { if (this.X != value) this.X = value; } } public double _Y { get { return Y; } set { if (this.Y != value) this.Y = value; } } Властивості в PowerDesigner

  18. public class Shape { public void Draw(){}; public double GetArea(){}; public Point origin; private Double Height; private double Width; } Асоціації можуть мати: • ім'яасоціації • імена ролей • кратність • можливістьнавігації Асоціація між класами

  19. Властивості асоціації

  20. Властивості ролей асоціації

  21. Наслідування

  22. Перевизначення методів

  23. public abstract class Shape { public abstract void Draw(); public abstract double GetArea(); public Point origin; private Double Height; private Double Width; } public class Circle : Shape { public override void Draw() { // TODO: implement } public override double GetArea() { // TODO: implement return 3.14 * Height * Height; } } public class Square : Shape { public override void Draw() { // TODO: implement } public override double GetArea() { // TODO: implement return Height*Height; } } Поліморфізм

  24. Поліморфізм public class Picture { public Shape[] items; }

  25. Множини наслідування

  26. public class Point { private double X; private double Y; private static double MaxX; private static Double MinX; private static double MaxY; private static Double MinY; … public class Convertation { public intCompX() { // TODO: implement return 0; } public intCompY() { // TODO: implement return 0; } public Point GetPoint(int x, int y) { // TODO: implement return null; } private intCx; private int Cy; } } Вкладені типи

  27. Елементи діаграми класів PowerDesigner

More Related