본문 바로가기

Software/C#.Net

The Pillars of OOP

Encapsulation: How does this language hide an object’s internal implementation details and preserve data integrity?
private,protected, Accessor(get/set), properties(get/set)
Inheritance: How does this language promote code reuse?
is a, has a(containment/delegation model or aggregation)
Polymorphism: How does this language let you treat related objects in a similar way?
virtual, abstract, overridden,


C# Access Modifiers
 C# Access Modifier
 May Be Applied To
 Meaning in Life
 public  Types or type members  Public items have no access restrictions. A public member can be accessed from an object as well as any derived class. A public type can be accessed from other external assemblies.
 private  members, nested types
 Private items can only be accessed by the class (or structure) that defines the item
 protected  Type members or nested types Protected items can be used by the class which defines it, and any child class.  However, protected items cannot be accessed from the outside world using the C# dot operator. 
 internal  Types or type members  Internal items are accessible only within the current assembly. Therefore, if you define a set of internal types within a .NET class library, other assemblies are not able to make use of them
 protected internal
 Type members or nested types
When the protected and internal keywords are combined on an item, the item is accessible within the defining assembly, the defining class, and by derived classes.

Default Access Modifiers
// An internal class with a private default constructor. 
class Radio 
{ 
  Radio(){} 
} 
Access Modifiers and Nested Types
public class SportsCar 
{ 
  // OK!  Nested types can be marked private. 
  private enum CarColor 
  { 
    Red, Green, Blue 
  } 
} 

.....
// Error! Nonnested types cannot be marked private! 
private class SportsCar {} 
Read-Only and Write-Only Properties
    Read-only
  • omit set
  • const : assigned value before run-time
  • readonly : assigned value after run-time only once in constructor
  • static readonly : class level readonly
    Write Only
  • omit get
Initializing Inner Types
 Define Rectangle Class
class Rectangle 
{ 
  private Point topLeft = new Point(); 
  private Point bottomRight = new Point(); 
 
  public Point TopLeft 
  { 
    get { return topLeft; } 
    set { topLeft = value; } 
  } 
  public Point BottomRight 
  { 
    get { return bottomRight; } 
    set { bottomRight = value; } 
  }  
Initialization Inner Type
// Create and initialize a Rectangle. 
Rectangle myRect = new Rectangle 
{ 
  TopLeft = new Point { X = 10, Y = 10 }, 
  BottomRight = new Point { X = 200, Y = 200} 
}; 



Virtual / Override Keyword Create Base class with override-able method
partial class Employee 
{ 
  // This method can now be 'overridden' by a derived class.
  public virtual void GiveBonus(float amount) >>>virtual<<<
  { 
    currPay += amount; 
  } 
... 
} 
Override Derived class can override a base method with override keyword
class SalesPerson : Employee 
{ 
... 
  // A salesperson's bonus is influenced by the number of sales.
  public override void GiveBonus(float amount) >>>Override<<<
  { 
    int salesBonus = 0; 
    if (numberOfSales >= 0 && numberOfSales <= 100) 
      salesBonus = 10; 
    else 
    { 
      if (numberOfSales >= 101 && numberOfSales <= 200) 
        salesBonus = 15; 
      else 
        salesBonus = 20; 
    } 
    base.GiveBonus(amount * salesBonus); 
  } 
} 
 
class Manager : Employee 
{ 
... 
  public override void GiveBonus(float amount) >>>override<<<
  { 
    base.GiveBonus(amount); 
    Random r = new Random(); 
    numberOfOptions += r.Next(500); 
  } 
} 

Prevent extending a method not class level by sealed keyword
// SalesPerson has sealed the GiveBonus() method! 
class SalesPerson : Employee 
{ 
... 
  public override sealed void GiveBonus(float amount) ****
  { 
     ... 
  } 
}
//Error to override a method which is sealed 
sealed class PTSalesPerson : SalesPerson 
{ 
  public PTSalesPerson(string fullName, int age, int empID, float currPay, string ssn, int numbOfSales)
               :base (fullName, age, empID, currPay, ssn, numbOfSales) 
  { 
  } 
 
  // Compiler error!  Can't override this method 
  // in the PTSalesPerson class, as it was sealed. 
  public override void GiveBonus(float amount) 
  { 
  } 
} 
Interface with abstract
abstract force to subclass implements abstract method
abstract class Shape 
{ 
  // Force all child classes to define how to be rendered. 
  public abstract void Draw(); 
  ... 
} 
*****
// If we did not implement the abstract Draw() method, Circle would also be 
// considered abstract, and would have to be marked abstract! 
class Circle : Shape 
{ 
  public Circle() {} 
  public Circle(string name) : base(name) {} 
  public override void Draw() 
  { 
    Console.WriteLine("Drawing {0} the Circle", PetName); 
  } 
}
Full story of Polymorphism
static void Main(string[] args) 
{ 
  Console.WriteLine("***** Fun with Polymorphism *****\n"); 
 
  // Make an array of Shape-compatible objects. 
  Shape[] myShapes = {new Hexagon(), new Circle(), new Hexagon("Mick"), 
    new Circle("Beth"), new Hexagon("Linda")}; 
 
  // Loop over each item and interact with the 
  // polymorphic interface. 
  foreach (Shape s in myShapes) 
  { 
    s.Draw(); 
  } 
  Console.ReadLine(); 
}