Difference between revisions of "OOP Class5"
Line 10: | Line 10: | ||
}</csharp> | }</csharp> | ||
+ | ==What is UML== | ||
+ | ;UML : Unified Modeling Language. | ||
+ | :"A modeling language is a language whose vocabulary and rules focus on the conceptual and physical representation of a system" | ||
+ | The Unified Modeling Language User Guide, Booch Rumbaugh Jacobson | ||
+ | ACM Press, Addison Wesley 1999 ISBN 0201571684 | ||
+ | [http://www.amazon.com/gp/product/0201571684/104-7742183-3467114?v=glance&n=283155 The Unified Modeling Language User Guide] | ||
+ | ==Simple Notation== | ||
+ | Class notation in UML | ||
+ | The name of the Class should go inside of a box | ||
− | == | + | {| align="center" cellspacing="0" style="border:2px solid black;" |
− | + | |Class Name | |
− | + | |} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | + | Then Two lines are added. The first line separates Attributes. The second line separates Operations. | |
− | + | Attributes are the equivalent of fields or properties in c# and Operations are the same as methods. | |
− | + | {| align="center" border = 0 | |
+ | | | ||
− | + | {| cellspacing="0" style="border:2px solid black;" | |
− | + | |Class Name | |
+ | |- | ||
+ | | style="border-Top:2px solid black;" | | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | | ||
+ | |} | ||
− | + | | | |
− | { | + | |
− | + | {| align="center" cellspacing="0" Border = 0 | |
− | + | | <--'''Class Name''' | |
− | + | |- | |
− | + | | <--'''Attributes''' | |
− | + | |- | |
− | + | | <--'''Opertaions''' | |
− | + | |} | |
− | + | ||
− | } | + | |} |
+ | |||
+ | |||
+ | A Rectagle Class might look like this | ||
+ | |||
+ | {| align="center" cellspacing="0" style="border:2px solid black;" | ||
+ | |Rectangle | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | height | ||
+ | |- | ||
+ | | width | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | area() | ||
+ | |- | ||
+ | | add() | ||
+ | |- | ||
+ | | move() | ||
+ | |- | ||
+ | | copy() | ||
+ | |- | ||
+ | | isEmpty() | ||
+ | |} | ||
− | + | You can also specify Attribute types with a : | |
− | |||
− | + | {| align="center" cellspacing="0" style="border:2px solid black;" | |
− | + | |Rectangle | |
− | + | |- | |
− | + | | style="border-Top:2px solid black;" | height : Float | |
− | + | |- | |
− | + | | width : Float | |
− | } | + | |- |
+ | | style="border-Top:2px solid black;" | area() | ||
+ | |- | ||
+ | | add() | ||
+ | |- | ||
+ | | move() | ||
+ | |- | ||
+ | | copy() | ||
+ | |- | ||
+ | | isEmpty() | ||
+ | |} | ||
+ | Default vlaues are specified with = | ||
− | + | {| align="center" cellspacing="0" style="border:2px solid black;" | |
+ | |Rectangle | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | height : Float = 6.0 | ||
+ | |- | ||
+ | | width : Float = 4.0 | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | area() | ||
+ | |- | ||
+ | | add() | ||
+ | |- | ||
+ | | move() | ||
+ | |- | ||
+ | | copy() | ||
+ | |- | ||
+ | | isEmpty() | ||
+ | |} | ||
− | |||
− | |||
− | + | Operator return type are also specifed with a :. Opertor parameter signatures are specified by ( '''name''' : '''Type''' ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {| align="center" cellspacing="0" style="border:2px solid black;" | |
− | + | |Rectangle | |
− | + | |- | |
− | + | | style="border-Top:2px solid black;" | height : Float = 6.0 | |
− | } | + | |- |
+ | | width : Float = 4.0 | ||
+ | |- | ||
+ | | style="border-Top:2px solid black;" | area() : Float | ||
+ | |- | ||
+ | | add()(r:Rectangle) | ||
+ | |- | ||
+ | | move() | ||
+ | |- | ||
+ | | copy() :Rectangle | ||
+ | |- | ||
+ | | isEmpty() : Boolean = true | ||
+ | |} | ||
+ | ==UML Resources== | ||
+ | [http://www.gnome.org/projects/dia/ Dia A Drawing Program] | ||
− | + | [http://dia-installer.sourceforge.net/ Dia for windows] | |
− | = | + | [http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class6/DiagramDog Dog Diagrams with Dia] |
− | == | + | ==Dog Exmaples== |
+ | Simple Dog Class from week 5 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogSimple.cs DogSimple.cs - source]<br /> | ||
+ | [[Image:DogSimple.png]] | ||
+ | <csharp>using System; | ||
− | + | //Dog simple class definition | |
− | + | public class Dog | |
− | + | { | |
− | + | public string Name; // the dog's name | |
+ | public int Age; // the dog's age | ||
+ | public int Weight; // the dog's weight | ||
+ | public string BarkSound; // the sound of the dog's bark | ||
+ | |||
+ | public Dog() | ||
+ | { | ||
+ | BarkSound = "Woof!!!"; | ||
+ | } | ||
− | < | + | public void Bark() { |
− | + | //put bark code here | |
+ | } | ||
+ | public void Eat() { | ||
+ | //put eat code here | ||
+ | } | ||
+ | }</csharp> | ||
− | |||
− | |||
− | |||
− | + | Dog UML with Types | |
− | + | Dog Class with Types from week 4 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogWithTypes.cs DogWithTypes.cs - source]<br /><br /> | |
+ | [[Image:DogWithTypes.png]] | ||
+ | <csharp>using System; | ||
+ | |||
+ | //Dog simple class definition | ||
+ | public class Dog | ||
+ | { | ||
+ | public string Name; // the dog's name | ||
+ | private int Age; // the dog's age | ||
+ | public int Weight; // the dog's weight | ||
+ | public string BarkSound; // the sound of the dog's bark | ||
+ | private int barkCount; //The number of time the dog has barked | ||
+ | public Dog() | ||
+ | { | ||
+ | BarkSound = "Woof!!!"; | ||
+ | } | ||
+ | |||
+ | public string Bark() { | ||
+ | string strBark = this.BarkSound; | ||
+ | barkCount ++; | ||
+ | return strBark; | ||
+ | } | ||
+ | |||
+ | public void Eat() { | ||
+ | //put eat code here | ||
+ | } | ||
+ | }</csharp> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ==Inheritance - 'Is A'== | |
− | - | ||
+ | [http://ia300033.us.archive.org/1/items/arsdigitac04n03/Lecture_03.html ArsDigita University Course 04: Object-oriented Program Design and Software Engineering - Lecture Notes 3] | ||
+ | C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. | ||
− | + | A Basenji is an African barkless dog. | |
+ | A Basenji is a dog. | ||
+ | syntax | ||
− | <csharp>// | + | <csharp>public class Basenji : Dog |
− | public | + | { |
+ | //override constructor | ||
+ | public Basenji() : base() | ||
{ | { | ||
− | + | barkSound = "Basenjis don't bark, but they do howl and growl."; | |
− | + | dogCount++; | |
− | + | } | |
− | + | public override void Bark() | |
− | + | { | |
− | + | Console.WriteLine("Basenjis dont bark."); | |
− | + | } | |
− | + | ||
− | + | // some more code.... | |
− | + | } | |
− | + | ||
− | + | public class Dog | |
− | + | { | |
− | + | // some dog code... | |
− | + | //methods need to be marked virtual or protected to be overridden | |
− | + | public virtual void Bark() | |
− | + | { | |
+ | //make dog bark | ||
+ | Console.WriteLine (this.Name + " says " + this.barkSound); | ||
+ | //add 1 to the number of times the dog has barked | ||
+ | this.barkCount++ ; | ||
+ | } | ||
+ | }</csharp> | ||
+ | |||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/Basenji.cs Basenji.cs] -source | ||
+ | |||
+ | ==Virtual Functions== | ||
− | + | A class modifier that indicates that a method can be overridden by a derived class. | |
− | + | We will look at virtual function when we look at abstract classes | |
− | + | ==Abstract Classes== | |
+ | abstract - A class modifier that specifies that the class must be derived from to be instantiated. | ||
− | <csharp> | + | <csharp>abstract public class Mammal |
− | public class | ||
{ | { | ||
− | + | private string birth; | |
− | + | private string skin; | |
− | + | ||
− | private string | + | public Mammal() |
− | private | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | public | ||
{ | { | ||
− | + | birth = " gives birth to live young."; | |
− | + | skin = " has Hair."; | |
} | } | ||
− | public string | + | public string Birth |
{ | { | ||
get | get | ||
{ | { | ||
− | return | + | return birth; |
} | } | ||
− | + | } | |
+ | public string Skin | ||
+ | { | ||
+ | get | ||
{ | { | ||
− | + | return skin; | |
} | } | ||
} | } | ||
− | public | + | }</csharp> |
+ | |||
+ | Inheritance example /infod/jeff/classSource/class5/DogMammal.cs -source | ||
+ | |||
+ | ===Polymorphic Types=== | ||
+ | [http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 Polymorphism (computer science)] | ||
+ | Different types implementing the same functionality.<br> | ||
+ | [http://www.webopedia.com/TERM/p/polymorphism.html polymorphism webopedia] | ||
+ | The C# Station Tutorial - [http://www.csharp-station.com/Tutorials/Lesson09.aspx Lesson 9: Polymorphism] | ||
+ | |||
+ | <csharp>abstract public class Mammal | ||
+ | { | ||
+ | private string name; | ||
+ | private string birth; | ||
+ | private string skin; | ||
+ | |||
+ | public Mammal() | ||
+ | { | ||
+ | name = "none"; | ||
+ | birth = " gives birth to live young."; | ||
+ | skin = " has Hair."; | ||
+ | } | ||
+ | |||
+ | public Mammal(string newName) | ||
+ | { | ||
+ | name = newName; | ||
+ | birth = " gives birth to live young."; | ||
+ | skin = " has Hair."; | ||
+ | } | ||
+ | |||
+ | public string Birth | ||
{ | { | ||
get | get | ||
{ | { | ||
− | return | + | return birth; |
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
− | public | + | public string Skin |
{ | { | ||
get | get | ||
{ | { | ||
− | return | + | return skin; |
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } | ||
− | public string | + | public string Name |
{ | { | ||
get | get | ||
{ | { | ||
− | return | + | return name; |
} | } | ||
set | set | ||
{ | { | ||
− | + | name = value; | |
} | } | ||
} | } | ||
− | public | + | public virtual void Bark() |
− | |||
{ | { | ||
− | + | Console.WriteLine("Some Mammals Bark."); | |
− | |||
− | |||
− | |||
} | } | ||
− | + | } | |
− | + | ||
− | + | public class Dog : Mammal | |
− | + | { | |
− | + | public string BarkSound; // the sound of the dog's bark | |
− | + | ||
− | + | public Dog(string newName) | |
− | + | { | |
− | + | Name = newName; | |
− | + | BarkSound = "Woof!!!"; | |
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | public void Bark() { | + | public override void Bark() { |
− | + | base.Bark(); | |
− | Console.WriteLine (this.Name + " says " + this. | + | Console.WriteLine(this.Name + " says " + this.BarkSound); |
− | |||
− | |||
} | } | ||
public void Eat() { | public void Eat() { | ||
//put eat code here | //put eat code here | ||
} | } | ||
− | } | + | } |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | public class Basenji : Dog | |
− | |||
− | |||
{ | { | ||
− | // | + | //override constructor |
− | + | public Basenji(string newName) : base(newName) | |
− | |||
− | |||
− | public | ||
{ | { | ||
− | + | BarkSound = "basenjis don't bark, but they do howl and growl."; | |
− | |||
− | |||
} | } | ||
− | public | + | |
+ | public override void Bark() | ||
{ | { | ||
− | + | base.Bark(); | |
− | + | Console.WriteLine("\nBasenjis dont bark."); | |
− | + | } | |
− | + | // some more code.... | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | //some | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}</csharp> | }</csharp> | ||
− | + | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogPoly.cs DogPoly.cs] -source | |
− | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Revision as of 16:32, 9 October 2007
Contents
This keyword
The this keyword used within a class refers to the current instance of the class
<csharp>public Time( int CurrentYear, int CurrentDay) {
this.Year = CurrentYear; this.Day = CurrentDay;
}</csharp>
What is UML
- UML
- Unified Modeling Language.
- "A modeling language is a language whose vocabulary and rules focus on the conceptual and physical representation of a system"
The Unified Modeling Language User Guide, Booch Rumbaugh Jacobson ACM Press, Addison Wesley 1999 ISBN 0201571684 The Unified Modeling Language User Guide
Simple Notation
Class notation in UML
The name of the Class should go inside of a box
Class Name |
Then Two lines are added. The first line separates Attributes. The second line separates Operations.
Attributes are the equivalent of fields or properties in c# and Operations are the same as methods.
|
|
A Rectagle Class might look like this
Rectangle |
height |
width |
area() |
add() |
move() |
copy() |
isEmpty() |
You can also specify Attribute types with a :
Rectangle |
height : Float |
width : Float |
area() |
add() |
move() |
copy() |
isEmpty() |
Default vlaues are specified with =
Rectangle |
height : Float = 6.0 |
width : Float = 4.0 |
area() |
add() |
move() |
copy() |
isEmpty() |
Operator return type are also specifed with a :. Opertor parameter signatures are specified by ( name : Type )
Rectangle |
height : Float = 6.0 |
width : Float = 4.0 |
area() : Float |
add()(r:Rectangle) |
move() |
copy() :Rectangle |
isEmpty() : Boolean = true |
UML Resources
Dog Exmaples
Simple Dog Class from week 5 DogSimple.cs - source
<csharp>using System;
//Dog simple class definition public class Dog {
public string Name; // the dog's name
public int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark
public Dog() { BarkSound = "Woof!!!"; }
public void Bark() { //put bark code here } public void Eat() { //put eat code here } }</csharp>
Dog UML with Types
Dog Class with Types from week 4 DogWithTypes.cs - source
<csharp>using System;
//Dog simple class definition public class Dog {
public string Name; // the dog's name
private int Age; // the dog's age public int Weight; // the dog's weight public string BarkSound; // the sound of the dog's bark private int barkCount; //The number of time the dog has barked public Dog() { BarkSound = "Woof!!!"; }
public string Bark() { string strBark = this.BarkSound;
barkCount ++; return strBark;
}
public void Eat() { //put eat code here } }</csharp>
Inheritance - 'Is A'
C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from.
A Basenji is an African barkless dog. A Basenji is a dog. syntax
<csharp>public class Basenji : Dog {
//override constructor public Basenji() : base() { barkSound = "Basenjis don't bark, but they do howl and growl."; dogCount++; } public override void Bark() { Console.WriteLine("Basenjis dont bark."); }
// some more code.... }
public class Dog { // some dog code... //methods need to be marked virtual or protected to be overridden
public virtual void Bark() { //make dog bark Console.WriteLine (this.Name + " says " + this.barkSound); //add 1 to the number of times the dog has barked this.barkCount++ ; }
}</csharp>
Basenji.cs -source
Virtual Functions
A class modifier that indicates that a method can be overridden by a derived class. We will look at virtual function when we look at abstract classes
Abstract Classes
abstract - A class modifier that specifies that the class must be derived from to be instantiated.
<csharp>abstract public class Mammal { private string birth; private string skin;
public Mammal() { birth = " gives birth to live young."; skin = " has Hair."; }
public string Birth { get { return birth; } } public string Skin { get { return skin; } } }</csharp>
Inheritance example /infod/jeff/classSource/class5/DogMammal.cs -source
Polymorphic Types
Polymorphism (computer science)
Different types implementing the same functionality.
polymorphism webopedia
The C# Station Tutorial - Lesson 9: Polymorphism
<csharp>abstract public class Mammal { private string name; private string birth; private string skin;
public Mammal() { name = "none"; birth = " gives birth to live young."; skin = " has Hair."; }
public Mammal(string newName) { name = newName; birth = " gives birth to live young."; skin = " has Hair."; }
public string Birth { get { return birth; } } public string Skin { get { return skin; } } public string Name { get { return name; } set { name = value; } } public virtual void Bark() { Console.WriteLine("Some Mammals Bark."); } }
public class Dog : Mammal { public string BarkSound; // the sound of the dog's bark
public Dog(string newName) { Name = newName; BarkSound = "Woof!!!"; }
public override void Bark() { base.Bark(); Console.WriteLine(this.Name + " says " + this.BarkSound); } public void Eat() { //put eat code here } }
public class Basenji : Dog { //override constructor public Basenji(string newName) : base(newName) { BarkSound = "basenjis don't bark, but they do howl and growl."; }
public override void Bark() { base.Bark(); Console.WriteLine("\nBasenjis dont bark."); }
// some more code....
}</csharp>
DogPoly.cs -source
HomeWork
READ
- Chapter 9 Inside Methods p 111-123
- Chapter 11 Inheritance and Polymorphism p 134-154
- Chpater 12 Operator Overloading p 155 - 172
- chapter 13 Structs p 172 - 177 (relax its only 54 pages total)
DO
Extend the class that you worked on last week.
.
Demonstrate
- Containment 'Has A'
- Make a class that contains or is contained by your old class
- Association - 'Uses A'
- Make a class that uses your original class
Extra credit add two dags
roover = fido + milo;
Quiz 2 next week
Links
The C# Station Tutorial - Lesson 7: Introduction to Classes
The C# Station Tutorial - Lesson 8: Class Inheritance
The C# Station Tutorial - Lesson 9: Polymorphism
The C# Station Tutorial - Lesson 10: Properties
The C# Station Tutorial - Lesson 12: Structs
The C# Station Tutorial - Lesson 13: Interfaces
The C# Station Tutorial - Lesson 4: C# Fundamentals