Difference between revisions of "Intro Week 10"
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"") |
||
Line 9: | Line 9: | ||
constructor example | constructor example | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp">public class Dog |
{ | { | ||
public Dog() | public Dog() | ||
Line 19: | Line 19: | ||
Simple Dog class with constructor | Simple Dog class with constructor | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp">//Dog simple class definition |
public class Dog | public class Dog | ||
{ | { | ||
Line 47: | Line 47: | ||
Dog class with overloaded constructor | Dog class with overloaded constructor | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp">//Dog overloaded class definition |
public class Dog | public class Dog | ||
{ | { |
Latest revision as of 03:23, 9 February 2016
Constructors
A classes constructor is the method that is run when an instance of the class is created. If you do not provide a constructor the compiler provides one for you. The constructor is used to initiate all member properties. Unlike intrinsic types variables that are not initialized will be set to 0 or "" or null by the compiler in the default constructor.
The constructor allows you to set default values for the properties of the class. If you do not provide a constructor then a default constructor will be provided. The default constructor will initialize all of the properties in the class to empty/zero/null values.
constructor example
public class Dog
{
public Dog()
{
//constructor code
}
}
Simple Dog class with constructor
//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
}
}
Console Example Dog.cs
OverLoading Constructors
Overloading a constructor allows object creation to happen in different ways with different parameters. The class can be overloaded to accept different parameters but each constructor method must have a unique parameter signature. Dog class with overloaded constructor
//Dog overloaded 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 Dog(string newName)
{
Name = newName;
BarkSound = "Woof!!!";
}
public Dog(string newName, string newBarkSound )
{
Name = newName;
BarkSound = newBarkSound;
}
public void Bark() {
//put bark code here
}
public void Eat() {
//put eat code here
}
public string About() {
string strAbout = "";
strAbout += this.Name;
strAbout += " " + this.Age;
strAbout += " " + this.Weight;
strAbout += " " + this.BarkSound;
return strAbout;
}
}
}
Console Example dogOverload.cs
Art as a windows app
Art history flash cards
Intro To Programming Art Flash Cards
As a windows app
http://iam.colum.edu/introtoprogramming/jeff/sp10/WindowsFormsApplicationArts.zip
Homework
Make the Art as a windows app into a testing app.