1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 4 Object - Oriented Programming

50 230 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 1,27 MB

Nội dung

Object-Oriented Programming Chapter 4 Ebook: Beginning Visual C# 2010, part 1, chapter 8,9,10,13 Reference: DEITEL - CSharp How to Program Contents  Review concepts in OOP  Write classes in C#  Interface  Inheritance  Polymorphism  Relationships between objects Slide 2 Review concepts in OOP  Review concepts in OOP  class  object  field  property  method  event  Other problems  Static members: field, property and method (p.191)  Static constructor (p.191)  Static class (p.192) Slide 3 ClassName - Fields + Methods Class diagram Contents  Review concepts in OOP  Write classes in C#  Interface  Inheritance  Polymorphism  Relationships between objects Slide 4 Create a class in C# Slide 7 Member access modifiers  Without a access modifier : can be accessed by any class in the same namespace  private: can only be accessed from inside the class  public: can be accessed from anywhere  protected: can be accessed from inside the child class or any class in the same namespace Slide 8 Writing properties  Properties (accessors):  Contain two blocks: get and set the value of the fields  One of these blocks can be omitted to create read-only or write-only properties  You can use Encapsulate Field function to properties Slide 9 Writing constructors  A constructor is a special method in a class that is invoked when the object gets instantiated  A constructor is used to initialize the properties of the object  Note to write a constructor:  The name of the constructor and the name of the class are the same  A constructor does not return value, not even void  A class can have multiple constructors (overloaded constructors) public ClassName (parameterList) { } Slide 10 Instantiating an object  To instantiate an object, using the new keyword  Example: Lop l = new Lop(); Lop c; c = new Lop(“NCTH2K”, “Cao dang nghe 2K”); ClassName object = new ClassName (…); ClassName object; object = new ClassName (…); Slide 11 Using properties, methods of a class  Call methods or properties of a class  Using dot operator  Accessors:  get: variable = object.propertyName  set: object.propertyName = variable  Example: Lop c = new Lop(); c.MaLop = "NCTH2K"; // call set{} c.TenLop = "Cao dang nghe 2K"; // call set{} Slide 12 [...]... child class can redefine a base-class method; this method is called overriding method To be overridden, a base-class method must be declared vir tual To write an overriding method, using override keyword in the method header To view the method header for a method, using Object Browser  Example: view Object s methods Slide 27 Object class (p.215)  All classes inherit from Object   All classes have... Class View Window (p.222)   The Object Browser (p.2 24)   Menu View\Class View Menu View \Object Browser Class Diagrams (p.227)  The class diagram editor in VS enables you to generate UMLlike diagrams of your code and use them to modify projects Slide 13 static class members (p.191)  Every object of a class has its own copy of all instance variables  How can all objects of a class share the same... void Main( string[] args ) { Circle circle = new Circle( 37, 43 , 2.5 ); // get Circle3's initial x-y coordinates and radius string output = "X coordinate is " + circle.X + "\n" + "Y coordinate is " + circle.Y + "\nRadius is " + circle.Radius; // set Circle3's x-y coordinates and radius to new values circle.X = 2; circle.Y = 2; circle.Radius = 4. 25; // display Circle3's string representation output +=... Slide 33 Contents       Review concepts in OOP Write classes in C# Interface Inheritance Polymorphism Relationships between objects Slide 34 Polymorphism (p.196)  Polymorphism allows objects to be represented in multiple forms  Polymorphism via inheritance: All objects instantiated from a derived class can be treated as if they were instances of a parent class Cow myCow = new Cow(); Animal... the abstract methods and properties of the base-class Slide 36 Polymorphism example  Base-class Employee    Classes that derive from Employee        abstract abstract method Earnings Boss CommissionWorker PieceWorker HourlyWorker All derived-classes implement method Earnings Driver program uses Employee references to refer to instances of derived-classes Polymorphism calls the correct version...   All classes have access to the protected and public members of this class (table p.215) You can override some methods of Object class (if a method is being overridden)   Example: public override string ToString() Some Object s methods:    ToString(): example Equals (object) : example GetHashCode(): example Slide 28 Example: class Point public class Point { private int x, y; public Point()... keyword static  Static members can be fields, properties, methods  When using static members, don’t need to instantiate an object Slide 14 static class members (cont.) public class Employee { private string firstName; private string lastName; private static int count; // Employee objects in memory public Employee( string fName, string lName ) { firstName = fName; lastName = lName; count++; } public static... and methods of the parent class   A programmer can tailor a child class as needed by adding new variables or methods, or by modifying the inherited ones C# provides a common base class for all objects called Object class Slide 23 Inheritance (cont.)  Declare inheritance class public class ChildClass : ParentClass { // }   ParentClass ChildClass Classes in C# may derive only from a single base class... Constants.PI * constantValues.radius, "Circumference" ); } } Slide 19 Contents       Review concepts in OOP Write classes in C# Interface Inheritance Polymorphism Relationships between objects Slide 22 Inheritance (p.1 94)   Inheritance enables to create a new class that extends an existing class  The existing class is called the parent class, or super class, or base class  The new class is called... base class directly The protected modifier allows a child class to reference a variable or method directly in the parent class  A protected variable is visible to any class in the same namespace Slide 24 The base reference    Constructors are not inherited To invoke the parent's constructor : base (parameters); The base reference can also be used to reference other variables and methods defined in . 4 Ebook: Beginning Visual C# 2010, part 1, chapter 8,9,10, 13 Reference: DEITEL - CSharp How to Program Contents  Review concepts in OOP  Write classes in C#  Interface  Inheritance  Polymorphism. C#  Interface  Inheritance  Polymorphism  Relationships between objects Slide 2 Review concepts in OOP  Review concepts in OOP  class  object  field  property  method  event  Other problems  Static. (p.191)  Static constructor (p.191)  Static class (p.192) Slide 3 ClassName - Fields + Methods Class diagram Contents  Review concepts in OOP  Write classes in C#  Interface  Inheritance  Polymorphism

Ngày đăng: 13/05/2014, 11:30

TỪ KHÓA LIÊN QUAN