Difference between revisions of "OOP Class7"
Line 1: | Line 1: | ||
[[Category:OOP]] | [[Category:OOP]] | ||
+ | |||
+ | ==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 | ||
+ | protected # | ||
+ | :accessible within its class and by derived classes | ||
+ | internal | ||
+ | :accessible only within files in the same assembly | ||
+ | protected internal | ||
+ | :combination of protected and internal | ||
+ | Public Dog | ||
+ | [[Image:DogSimpleUML.png]] | ||
+ | |||
+ | Dog with private age | ||
+ | [[Image:DogsimpleAgePrivate.png]] | ||
+ | |||
+ | ==Properties 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 | ||
+ | |||
+ | <csharp>//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; | ||
+ | } | ||
+ | }</csharp> | ||
+ | |||
+ | |||
+ | Another property that is a good candidate for a private instance data member is the dogs age | ||
+ | |||
+ | <csharp> | ||
+ | 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; | ||
+ | } | ||
+ | |||
+ | |||
+ | </csharp> | ||
+ | |||
+ | ===In class=== | ||
+ | |||
+ | ==In Class Project== | ||
+ | |||
+ | *Build a Dog class with a private variable called age. | ||
+ | *Make a public Accessor for the private age the is read only | ||
+ | *Make a method that increments the age of your dog | ||
+ | *Make a test class for the new property and accessor | ||
+ | |||
+ | <!--In class build triangle class. | ||
+ | Properties | ||
+ | |||
+ | * sideA | ||
+ | * sideB | ||
+ | * sideC | ||
+ | |||
+ | Methods | ||
+ | |||
+ | * Area //A method that return the area of the triangle | ||
+ | |||
+ | use Heronian formula which is able to compute the area of a triange by knowing the length of the three sides. | ||
+ | triangle area given a,b,c = sqrt(s(s-a)(s-b)(s-c)) when s = (a+b+c)/2 (Heron's formula) | ||
+ | |||
+ | <csharp>// Heronian formula | ||
+ | double s = (a + b + c) / 2.0; | ||
+ | double dArea = Math.Sqrt(s*(s-a)*(s-b)*(s-c)); | ||
+ | </csharp> | ||
+ | |||
+ | Once you have built the triangle class build a class to test it. | ||
+ | The test class should create a triangle with | ||
+ | sideA= 3 | ||
+ | sideB= 4 | ||
+ | sideC= 5 | ||
+ | |||
+ | and then display the area as text in the console. | ||
+ | |||
+ | http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class4/tri_class.cs tri_class.cs] | ||
+ | --> | ||
+ | |||
+ | |||
+ | |||
+ | Here's a class of dogs that return different color depending on whether on not they are clean. It uses private private instance data members | ||
+ | |||
+ | |||
==Visual Studio 2005== | ==Visual Studio 2005== |
Revision as of 20:53, 16 October 2007
Contents
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
protected #
- accessible within its class and by derived classes
internal
- accessible only within files in the same assembly
protected internal
- combination of protected and internal
Properties 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
<csharp>//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; }
}</csharp>
Another property that is a good candidate for a private instance data member is the dogs age
<csharp> 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;
}
</csharp>
In class
In Class Project
*Build a Dog class with a private variable called age. *Make a public Accessor for the private age the is read only *Make a method that increments the age of your dog *Make a test class for the new property and accessor
Here's a class of dogs that return different color depending on whether on not they are clean. It uses private private instance data members
Visual Studio 2005
Pass out MSDNAA Visual Studio 2005 DVD
Visual Studio Demo
Demo of students diagram
http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class7/StudentDiagram/StudentDiagram
http://iam.colum.edu/oop/classsource/class7/StudentDiagram.zip
Fancy Arrays
Arrays
- Regular Array objects are like list collections
Arrays are groups of variables of the same type
- Syntax
type [] identifier
single dimension arrays
<csharp>string [] aryNames = new string[5];
aryNames [0] = "Feta"; aryNames [1] = "Colby"; aryNames [2] = "Gruyere"; aryNames [3] = "Edam"; aryNames [4] = "Colby";</csharp>
Arrays are actually objects and support various methods.
- GetValue
- IndexOf
- LastIndexOf
- SetValue
- Reverse
- Sort
GetValue
<csharp>string s = aryNames.GetValue(1).ToString();</csharp>
- s="Colby"
IndexOf
<csharp>int i = Array.IndexOf(aryNames, "Colby");</csharp>
- i=1
LastIndexOf
<csharp>int Lasti= Array.LastIndexOf(aryNames, "Colby");</csharp>
- Lasti=4
SetValue <csharp>
aryNames.SetValue("Gouda", 1); string setValue = aryNames.GetValue(1).ToString();</csharp>
Now Array(1) == "Gouda"
Reverse
- Reverse reverses the array
<csharp>Array.Reverse(aryNames);</csharp>
The Reversed array looks like Reverse
0 Colby 1 Edam 2 Gruyere 3 Gouda 4 Feta
Sort
- The Sort method sort the array. Arrays are sorted accourding to how objects compare to each other usually implemented using the IComparer interface
<csharp>Array.Sort(aryNames);</csharp>
The Sorted Array List Looks like
0 Colby 1 Edam 2 Feta 3 Gouda 4 Gruyere
Array Example 2
Pros and Cons of working with arrays pros
- East to use
- Fast to alter
- Flast to iterate though
- Specific type of elements ( single type)
cons
- Fixed size
- Insterting is difficult
Collections
Collections are groups of objects. types of collections
* set - no index * list - has index * map - has key/value pair
Set is like storing a bunch of objects in a bag. The order they are removed from the bag has nothing to do with the order they are removed. Set collections do not have a unique identifier.
List types of collections have an integer for an indexer. This gives them a unique identifier and an order.
Map types also have an index but it is not necessarily an integer. It could be anything unique.
ArrayList
The Array List of now part of System.Collections. In order to user it you must add a using statement <csharp>using System.Collections;</csharp>
create an arraylist
<csharp>ArrayList myArrayList = new ArrayList();</csharp>
add and element or two <csharp> myArrayList.Add("Feta"); myArrayList.Add("Colby"); myArrayList.Add("Gruyere"); myArrayList.Add("Edam"); myArrayList.Add("Colby"); </csharp> This produces and Array list like
Feta Colby Gruyere Edam Colby
Insert an element
<csharp>myArrayList.Insert(1,"Gouda");</csharp>
Now our ArrayList Looks like
Feta Gouda Colby Gruyere Edam Colby
Remove an element
<csharp>//remove by index myArrayList.RemoveAt(1); //remove by data myArrayList.Remove("Colby"); //removes first occurance of Colby </csharp>
Now our Array List looks like
Feta Gruyere Edam Colby
Pros and con of ArrayList Pros
- No fixed size
- Insert and Remove
Cons
- Slower than arrays
- Inefficient with memory (when the array size exceeds the upper bound .NET doubles the size of the array.
ArrayList Example
Polymorphism
Arrays and Collections of objects allow for polymorphism.
foreach
<csharp>foreach (string n in aryNames) {
aryDropDown.Items.Add(n);
}</csharp>
Home Work
Build and Demonstrate The classes in the Student Diagram in VS2005.
Build a UML Diagram that shows your classes and how they interact.