Академический Документы
Профессиональный Документы
Культура Документы
What is OOP ?
Programming Methodology
Uses Objects to carryout Functionality
OOP Benefits
Programs are organized around object that perform actions.
Programs no longer dependent on linear logic.
Definition of C# Class
Class is a custom User Defined Type (UDT) that is composed of
Scenario
We wish to build a class that maintains the name, current pay,
and employeeID for each worker. In addition, the Employee class defines one method named GiveBonus(), which increases an individuals current pay by some amount, and another named DisplayStats(), which prints out relavent statistics for this individual.
Class Diagram
Employee
Constructor
Constructor is used to initialize an object (instance) of a class. Constructor is a like a method without any return type.
Self-Reference in C#
this keyword is used whenever we wish to make reference to the current object .
The use of this keyword is to avoid clashes between the parameter names and names of your internal state variables
Public types may be created by any other objects within the same assembly as well as by other external assemblies
Internal Classes Can only be created by types living within the same assembly and are not accessible from outside the assemblys bounds
Encapsulation
Hides the unnecessary implementation details from the
definitions. Allows to extend the behavior of a base class by inheriting core functionality into a subclass. A Hexagon is-a Shape that is-a Object
Car Radio
Polymorphism
This trait captures languages ability to treat related objects the same way.
Classical Polymorphism Ad Hoc Polymorphism
Classical Polymorphism
Takes place in a language that also supports classical
Inheritance.
A base class can define a set of members that can be
overridden by a subclass.
Redefining how they respond to the same message
Ad Hoc Polymorphism
Allows objects that are not related by classical inheritance to
be treated in a similar manner, provided that every object has a method of the exact same signature.
They uses a technique called Late binding to discover at runtime
Encapsulation Services
The objects internal data should not be directly accessible from an
object instance.
We can alter the state of an object indirectly using accessor (get) and
Encapsulation provides a way to preserve the integrity of state data. Rather than defining public fields, we should get in the habit of
defining private data fields, which are indirectly manipulated using one of two main techniques: 1. Define a pair of traditional accessor and mutator methods. 2. Define a named property.
fullName = n;
}
}
As a class designer we are able to perform any internal logic necessary before making the value assignment.
// Custom property for the EmpID data point. public class Employee { ... private int empID; // Property for the empID data point. public int EmpID { get { return empID;} set { empID = value; } } }
Static Properties
// Static properties must operate on static data! public class Employee { private static string companyName; public static string Company { get { return companyName; } set { companyName = value;} } ... }
// Set and get the name of the company that employs these people... public static int Main(string[] args) { Employee.Company = Btech, Inc"; Console.WriteLine("These folks work at {0} ", Employee.Company); ... }
assignment. However, unlike a constant, the value assigned to a read-only field can be determined at runtime, and therefore can legally be assigned within the scope of a constructor (but nowhere else).
This can be very helpful when you dont know the value of a field
until runtime (perhaps because you need to read an external file to obtain the value), but wish to ensure that the value will not change after that point.
direct base class. It is not possible to create a class type that directly derives from two or more base classes. // Illegal! The .NET platform does not allow // multiple inheritance for classes! class WontWork: BaseClassOne, BaseClassTwo {}
to derive from this type. // This class cannot be extended! sealed class MiniVan : Car {} // Error! Cannot extend a class marked with the sealed keyword! class DeluxeMiniVan: MiniVan {}
parent is read-only; therefore we are unable to assign the incoming string parameter to this field, as seen in the final code statement of this custom constructor. The 2nd issue, we have indirectly created a rather inefficient constructor, given the fact that under C#, unless you say otherwise, the default constructor of a base class is called automatically before the logic of the derived constructor is executed. To implement our subclass constructors to explicitly call an appropriate custom base class constructor, rather than the default.
items cannot be accessed from any object beyond the class that has defined it. When a base class defines protected data or protected members, it establishes a set of items that can be accessed directly by any descendent. If we wish to allow the SalesPerson and Manager child classes to directly access the data sector defined by Employee, we can update the original Employee class definition as follows:
// protected state data. partial class Employee { // Derived classes can now directly access this information. protected string empName; protected int empID; protected float currPay; protected int empAge; protected string empSSN; protected static string companyName; ... }
public partial class Employee { // Contain a BenefitPackage object. protected BenefitPackage empBenefits = new BenefitPackage(); // Expose certain benefit behaviors of object. public double GetBenefitCost() { return empBenefits.ComputePayDeduction(); } // Expose object through a custom property. public BenefitPackage Benefits { get { return empBenefits; } set { empBenefits = value; } } ... }
// Employee nests BenefitPackage. public partial class Employee { // BenefitPackage nests BenefitPackageLevel. public class BenefitPackage { public enum BenefitPackageLevel { Standard, Gold, Platinum } public double ComputePayDeduction() { return 125.0; } } ... }
// A Manager "is-an" Employee too. Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "101-11-1321", 1); // A PTSalesPerson "is-a" SalesPerson. SalesPerson jill = new PTSalesPerson("Jill", 834, 3002, 100000, "111-121119", 90);
classes are related by an is-a relationship, it is always safe to store a derived type within a base class reference. Formally, this is called an implicit cast. second law of casting: we must explicitly downcast using the C# casting operator.