Difference between revisions of "OOP Class6"
(→Pair Programming) |
|||
Line 217: | Line 217: | ||
I would like you and your paired partner to create and demonstate classes in c# from the diagram below. | I would like you and your paired partner to create and demonstate classes in c# from the diagram below. | ||
− | + | ||
+ | ==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>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> | ||
+ | |||
+ | [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>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=== | ||
+ | [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 | ||
+ | { | ||
+ | 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> | ||
+ | |||
+ | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/DogPoly.cs DogPoly.cs] -source | ||
+ | |||
+ | ==Structs== | ||
+ | - remember these from week 2 | ||
+ | |||
+ | Lightweight alternatives to classes. Structs do not support inheritance or destructors. Structs are value typed objects similar to ints bool etc... while classes are refence types. Structs are more memory efficient and and faster than classes. | ||
+ | Syntax | ||
+ | |||
+ | [ attributes] [access-modifiers] struct identifier | ||
+ | [:interface-list {struct members} | ||
+ | |||
+ | <csharp>struct Dog | ||
+ | { | ||
+ | public string name; | ||
+ | public string weight; | ||
+ | public int age; | ||
+ | }</csharp> | ||
+ | |||
+ | This is a pretty lightweight dog and is pretty useless as a dog (It's can't even bark what fun is that) so I won't make an example. | ||
+ | |||
+ | A good example of a stuct would be and something like a point. There may be many many points in a structure or graph and we would want the points | ||
+ | to be a lightweight as possible. Since the point object won't have an methods this is a good time to use a struct. | ||
+ | |||
+ | <csharp>struct Point | ||
+ | { | ||
+ | public int x; | ||
+ | public int y; | ||
+ | |||
+ | public Point(int x, int y) | ||
+ | { | ||
+ | this.x = x; | ||
+ | this.y = y; | ||
+ | } | ||
+ | |||
+ | public Point Add(Point pt) | ||
+ | { | ||
+ | Point newPt; | ||
+ | |||
+ | newPt.x = x + pt.x; | ||
+ | newPt.y = y + pt.y; | ||
+ | |||
+ | return newPt; | ||
+ | } | ||
+ | }</csharp> | ||
==Homework== | ==Homework== |
Revision as of 14:44, 9 October 2006
Contents
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 properites 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>
In class
Discuss Dog Diagram
Pair Programming
We are going to pair off to do our next assignment. Many of the paired programming principles come from XP (http://www.extremeprogramming.org/ Extreeme Progamming)
Pairs are good because
- Pairs keep each other on track
- A partner can help when you are stuck
- Keep each other accountable
Some XP Principles
Fail - If you are having trouble succeeding fail.
Baby Steps - Do the smallest thing that you possible can that moves you in the right direction
If you do decide to dive and conqure the problem please remember that integration process is unpredictable and can be quite difficult. Try to integrate your code often. Try posting up your code and emailing or plan on meeting several times.
When topair programmers are together one you should sit a one computer (yeah that right two of you at one computer) and one should type while the other watches and reflects. Feel free to slide the keyboard back and forth when someone get tired, stuck or has a new idea.
I would like you and your paired partner to create and demonstate classes in c# from the diagram below.
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
Structs
- remember these from week 2
Lightweight alternatives to classes. Structs do not support inheritance or destructors. Structs are value typed objects similar to ints bool etc... while classes are refence types. Structs are more memory efficient and and faster than classes. Syntax
[ attributes] [access-modifiers] struct identifier [:interface-list {struct members}
<csharp>struct Dog {
public string name; public string weight; public int age;
}</csharp>
This is a pretty lightweight dog and is pretty useless as a dog (It's can't even bark what fun is that) so I won't make an example.
A good example of a stuct would be and something like a point. There may be many many points in a structure or graph and we would want the points to be a lightweight as possible. Since the point object won't have an methods this is a good time to use a struct.
<csharp>struct Point {
public int x; public int y;
public Point(int x, int y) { this.x = x; this.y = y; }
public Point Add(Point pt) { Point newPt;
newPt.x = x + pt.x; newPt.y = y + pt.y;
return newPt; }
}</csharp>
Homework
use Pair Programming to make tests and classes from the following UML
Create a UML Diagram of your c# classes (not the tv and radio yeah the pong machine and ninja and kungfumonkey and stuff)
READ
Chapter 2 Introducin the UML in The Unified Modeling Language Users Guided (hand out)
Chapter 4 Classes in The Unified Modeling Language Users Guided (hand out)
quiz #2 next week