Difference between revisions of "OOP Class5"
(→Dog Exmaples) |
|||
(13 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category: | + | [[Category:IAM Classes]] |
==This keyword== | ==This keyword== | ||
The this keyword used within a class refers to the current instance of the class | The this keyword used within a class refers to the current instance of the class | ||
− | <csharp>public Time( int CurrentYear, int CurrentDay) | + | <syntaxhighlight lang="csharp">public Time( int CurrentYear, int CurrentDay) |
{ | { | ||
this.Year = CurrentYear; | this.Year = CurrentYear; | ||
this.Day = CurrentDay; | this.Day = CurrentDay; | ||
− | }</ | + | }</syntaxhighlight> |
Line 155: | Line 155: | ||
[[Image:DogsimpleAgePrivate.png]] | [[Image:DogsimpleAgePrivate.png]] | ||
− | ==Dog | + | ==Dog Examples== |
Simple Dog Class from week 5 [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class6/DiagramDog/DogSimple.cs DogSimple.cs - source]<br /> | 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]] | [[Image:DogSimple.png]] | ||
− | <csharp>using System; | + | <syntaxhighlight lang="csharp">using System; |
//Dog simple class definition | //Dog simple class definition | ||
Line 179: | Line 179: | ||
//put eat code here | //put eat code here | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
Line 186: | Line 186: | ||
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 /> | 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]] | [[Image:DogWithTypes.png]] | ||
− | <csharp>using System; | + | <syntaxhighlight lang="csharp">using System; |
//Dog simple class definition | //Dog simple class definition | ||
Line 210: | Line 210: | ||
//put eat code here | //put eat code here | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
==Class Relationships== | ==Class Relationships== | ||
Line 249: | Line 249: | ||
syntax | syntax | ||
− | <csharp>public class Basenji : Dog | + | <syntaxhighlight lang="csharp">public class Basenji : Dog |
{ | { | ||
//override constructor | //override constructor | ||
Line 276: | Line 276: | ||
this.barkCount++ ; | this.barkCount++ ; | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/BasenjiInhertance.cs BasenjiInhertance.cs] -source | [http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class5/BasenjiInhertance.cs BasenjiInhertance.cs] -source | ||
Line 289: | Line 289: | ||
abstract - A class modifier that specifies that the class must be derived from to be instantiated. | abstract - A class modifier that specifies that the class must be derived from to be instantiated. | ||
− | <csharp>abstract public class Mammal | + | <syntaxhighlight lang="csharp">abstract public class Mammal |
{ | { | ||
public string Birth; | public string Birth; | ||
Line 299: | Line 299: | ||
Skin = " has Hair."; | Skin = " has Hair."; | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
Inheritance example /infod/jeff/classSource/class5/DogMammalInhertance.cs -source | Inheritance example /infod/jeff/classSource/class5/DogMammalInhertance.cs -source | ||
===Polymorphic Types=== | ===Polymorphic Types=== | ||
+ | |||
+ | ;Polymorphism: poly means 'many' morph means 'form'. So a polymorphic type is many forms that have the same attributes and abilities | ||
+ | |||
[http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 Polymorphism (computer science)] | [http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 Polymorphism (computer science)] | ||
− | Different types implementing the same functionality. | + | |
+ | Different types implementing the same functionality. | ||
+ | |||
[http://www.webopedia.com/TERM/p/polymorphism.html polymorphism webopedia] | [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] | The C# Station Tutorial - [http://www.csharp-station.com/Tutorials/Lesson09.aspx Lesson 9: Polymorphism] | ||
− | <csharp>//Mammal class definition is it | + | <syntaxhighlight lang="csharp">//Mammal class definition is it abstract |
abstract public class Mammal | abstract public class Mammal | ||
{ | { | ||
Line 376: | Line 382: | ||
} | } | ||
− | }</ | + | }</syntaxhighlight> |
[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/DogPoly.cs DogPoly.cs] -source | ||
Line 385: | Line 391: | ||
#Chapter 11 Inheritance and Polymorphism p 134-154 | #Chapter 11 Inheritance and Polymorphism p 134-154 | ||
#Chpater 12 Operator Overloading p 155 - 172 | #Chpater 12 Operator Overloading p 155 - 172 | ||
− | |||
'''DO''' | '''DO''' | ||
+ | Create a super class and a sub class for your class.<br /> | ||
− | + | http://iam.colum.edu/oop/classsource/class5/ConsoleApplicationInClass.zip | |
− | . | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | New One | |
+ | http://iam.colum.edu/oop/classsource/class5/ConsoleApplicationInClass5.zip | ||
==Links== | ==Links== |
Latest revision as of 16:30, 10 June 2019
Contents
This keyword
The this keyword used within a class refers to the current instance of the class
public Time( int CurrentYear, int CurrentDay)
{
this.Year = CurrentYear;
this.Day = CurrentDay;
}
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
access-modifiers
public +
- most permissive access. No restrictions on access
private -
- least permissive access level. Only available to the class in which it was declared
Dog Examples
Simple Dog Class from week 5 DogSimple.cs - source
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
}
}
Dog UML with Types
Dog Class with Types from week 4 DogWithTypes.cs - source
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
}
}
Class Relationships
Four Class Relation ships
- Association - Uses A OOP Class6
- Containment - Has A OOP Class6
- Inheritance - Is A
- Interfaces - implements or derives from We'll do this next week in OOP Class7
Inheritance - 'Is A'
C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from.
Sub class
- a Sub class is a class that inherits properties and methods from another class. The class that sub class inherits from id the Super class
Super class
- a Super class that has other classes that inherit from it.
A Basenji is an African barkless dog.
A Basenji is a Dog.
A Dog is a Mammal.
In this case the Mammal class is the Super class to the Dog.
The Basenji is the Sub class to the Dog class.
Here is how you would draw this with UML.
Lets start with the Dog/Basenji Relationship
syntax
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++ ;
}
}
BasenjiInhertance.cs -source
Virtual Functions
A class modifier that indicates that a method can be overridden by a derived class.
By marking a function as virtual any class a class that sub classes the current class can override the function
Abstract Classes
abstract - A class modifier that specifies that the class must be derived from to be instantiated.
abstract public class Mammal
{
public string Birth;
public string Skin;
public Mammal()
{
Birth = " gives birth to live young.";
Skin = " has Hair.";
}
}
Inheritance example /infod/jeff/classSource/class5/DogMammalInhertance.cs -source
Polymorphic Types
- Polymorphism
- poly means 'many' morph means 'form'. So a polymorphic type is many forms that have the same attributes and abilities
Polymorphism (computer science)
Different types implementing the same functionality.
The C# Station Tutorial - Lesson 9: Polymorphism
//Mammal class definition is it abstract
abstract public class Mammal
{
public string Name;
public string Brith;
public string Skin;
public Mammal()
{
Name = "none";
Brith = " gives Brith to live young.";
Skin = " has Hair.";
}
public Mammal(string newName)
{
Name = newName;
Brith = " gives Brith to live young.";
Skin = " has Hair.";
}
public virtual void Bark()
{
Console.WriteLine("Some Mammals Bark.");
}
}
//Dog simple class definition
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
}
}
//a basengi is a barkless dog
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....
}
}
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
DO
Create a super class and a sub class for your class.
http://iam.colum.edu/oop/classsource/class5/ConsoleApplicationInClass.zip
New One http://iam.colum.edu/oop/classsource/class5/ConsoleApplicationInClass5.zip
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