Created
December 16, 2014 15:07
-
-
Save stevecooperorg/cfb3a7ff397c07c00f3c to your computer and use it in GitHub Desktop.
Lesson 3 - objects and classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace HelloPerson | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// set up a new object representing Gary, using the class 'Person' | |
Person gary = new Person(); | |
gary.DateOfBirth = new DateTime(1980, 05, 30); | |
gary.FirstName = "Gary"; | |
gary.LastName = "Raper"; | |
gary.Address1 = "Testing1"; | |
gary.Address2 = "Testing2"; | |
gary.City = "York"; | |
// set up a second object representing Lisa, re-using the class 'Person' | |
Person lisa = new Person(); | |
lisa.FirstName = "Lisa"; | |
lisa.LastName = "Wilson"; | |
lisa.Address1 = "1 Artemis House"; | |
lisa.City = "York"; | |
// print out shipping labels for both lisa and gary | |
gary.PrintShippingLabel(); | |
lisa.PrintShippingLabel(); | |
// here's how you would create, then close, an incident; | |
Incident oilRigHasExploded = new Incident(); | |
oilRigHasExploded.Name = "Boom!"; | |
oilRigHasExploded.Close("Managed to put out the fire"); | |
// wait for the user to hit 'Enter' | |
Console.ReadLine(); | |
} | |
} | |
class Incident | |
{ | |
public string Name; | |
private DateTime CloseDate; | |
private string State; | |
public void Close(string closureReason) | |
{ | |
this.CloseDate = DateTime.Now; | |
this.State = "Closed"; | |
} | |
} | |
class Person | |
{ | |
public string FirstName; | |
public string LastName; | |
public string Address1; | |
public string Address2; | |
public string City; | |
public DateTime DateOfBirth; | |
public int GetAge() | |
{ | |
// rough age calculation | |
return DateTime.Now.Year - this.DateOfBirth.Year; | |
} | |
public void PrintShippingLabel() | |
{ | |
// Print out something like; | |
// | |
// Parcel for: Gary Raper | |
// Deliver To: 1 Artemis House | |
// Eboracum Way | |
// York | |
string contact = "Parcel for: " + this.FirstName + " " + this.LastName; | |
string address1 = "Deliver To: " + this.Address1; | |
string address2 = " " + this.Address2; | |
string city = " " + this.City; | |
Console.WriteLine(contact); | |
Console.WriteLine(address1); | |
Console.WriteLine(address2); | |
Console.WriteLine(city); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment