Difference between revisions of "OOP Class11"
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"") |
||
Line 9: | Line 9: | ||
Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpful when you want to also do other things when a data member is changed or change the rutern value under certain conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible | Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpful when you want to also do other things when a data member is changed or change the rutern value under certain conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp">//private string color read/write |
private string color; | private string color; | ||
Line 38: | Line 38: | ||
Another property that is a good candidate for a private instance data member is the dogs age | Another property that is a good candidate for a private instance data member is the dogs age | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
private string age; | private string age; | ||
Line 63: | Line 63: | ||
==Interfaces== | ==Interfaces== | ||
IFlyable | IFlyable | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
interface IFlyable | interface IFlyable | ||
{ | { | ||
Line 73: | Line 73: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Implementation | Implementation | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
class Helicopter : IFlyable | class Helicopter : IFlyable | ||
{ | { |
Revision as of 03:23, 9 February 2016
Duck Study
Contents
Private instance data members - accessors
Microsoft has stared calling private variables with accessors Properties
Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpful when you want to also do other things when a data member is changed or change the rutern value under certain conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible
//private string color read/write
private string color;
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
//private string color read only
private string color;
public string Color
{
get
{
return color;
}
}
Another property that is a good candidate for a private instance data member is the dogs age
private string age;
public int Age
{
//age can only be accessed with get there is no set accessor
//age must be set with HappyBirthday()
get
{
return age;
}
}
public int HappyBirthday()
{
age++;
return age;
}
Interfaces
IFlyable
interface IFlyable
{
int CurrentAltitude { get; set; }
void FlyUP();
void FlyDown();
}
Implementation
class Helicopter : IFlyable
{
protected int currentAltitude;
public Helicopter()
{
this.CurrentAltitude = 0;
}
public int CurrentAltitude
{
get
{
return currentAltitude;
}
set
{
currentAltitude = value;
}
}
public void FlyUP()
{
this.CurrentAltitude++;
}
public void FlyDown()
{
this.CurrentAltitude--;
}
}
Problems with inheritance
The super class Duck has a swim method.
Duck.swim()
Any class that subclasses the duck class will inherit the swim() method. So far so good all ducks swim
The super class also has a fly() method sa all classes that inherit from duck will get the fly() method.
All ducks don't fly...
Controls is an example of good inhertance
http://iam.colum.edu/oop/classsource/class11/controls.aspx
see
Hierarchy
Object ---> Control---> WebControl---> BaseDataBoundControl ---> DataBoundControl --->ListControl ---> DropDownList
Strategy Pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx
Strategy Pattern
- Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Ducks From Book
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class11/Duck
http://www.dofactory.com/Patterns/PatternStrategy.aspx
Pretty good example of the strategey method in action.
Phones
http://iam.colum.edu/oop/classsource/class12/ConsoleApplicationPhoneNoInterface.zip
Review Homework
Problems
Phone interface
Look as phone project. Discuss polymorphic phones interchangeability. Look for possible interfaces to extract.
Homework
Extract interfaces for phone class. Make a web interface for the phone classes.
You should be able to choose an phone type and then dial it and send messages if it is ITxtable.
http://iam.colum.edu/oop/classsource/class12/ConsoleApplicationPhoneNoInterface.zip