OOP Class9

esse quam videri
Revision as of 18:28, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "</csharp>" to "</syntaxhighlight>")
Jump to: navigation, search


Encapulation

I forgot to show this earlier

http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class4/DogBark


PolyMorphism

Using the least specific class to increase reuse.

Review Coins

?'s

Review MotorVehicle

?'s

Motorvehicle Diagram

Oop Motorvehicle Diagram

http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class7/MotorvehicleRace

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

 1 //private string color read/write
 2 private string color;
 3 
 4 public string Color
 5 {
 6   get
 7   {
 8    return color;
 9   }
10   set
11   {
12    color = value;
13   }
14 }
15 
16 //private string color read only
17 private string color;
18 
19 public string Color
20 {
21   get
22   {
23    return color;
24   }
25 }


Another property that is a good candidate for a private instance data member is the dogs age

 1 private string age;
 2 
 3 public int Age
 4 {
 5   //age can only be accessed with get there is no set accessor
 6   //age must be set with HappyBirthday()
 7   get
 8   {
 9    return age;
10   }
11 }
12 
13 public int HappyBirthday()
14 {
15   age++;
16   return age;
17 }

Windows forms

Events

events from a windows form or web page are handled by event handlers. Events are raised when a user interacts with interface elements and handled by the event handlers.


http://www.csharphelp.com/archives/archive253.html

Examaple of event handlers and classes

http://iam.colum.edu/oop/MotorvehicleRace.zip

1 private void button1_Click(object sender, EventArgs e)
2         {
3             Console.WriteLine("button1 Clicked");
4             MessageBox.Show("button1 Clicked");   
5         }

Create a windows form with a button the uses your class.

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace WindowsDog
10 {
11     public partial class Form1 : Form
12     {
13         Dog fido;
14         
15         public Form1()
16         {
17             InitializeComponent();
18             fido = new Dog();
19         }
20 
21         private void btnBark_Click(object sender, EventArgs e)
22         {
23             lblBark.Text = fido.Bark();
24         }
25 
26 
27         public class Dog 
28         {
29             public string Name;		// the dog's name
30             public int Age;			// the dog's age
31             public int Weight;			// the dog's weight
32             public string BarkSound;	// the sound of the dog's bark
33 
34             public Dog()
35             {
36                 BarkSound = "Woof!!!";
37             }
38 
39             public string Bark()
40             {
41                 return this.BarkSound;
42             }
43             public void Eat()
44             {
45                 //put eat code here 
46             }
47         }	
48     }
49 }

Home Work

Create a windows form and port your classes. The form should have UI elements (buttons, dropdown, etc) that demonstrate the use of you classes.

Watch the Android App Development with Java Essential Training

You should logon to Oasis http://oasis.colum.edu First then select the training tab. Then search for 'Android App Development with Java'

  • Section 0 Introduction
  • Section 1 Getting Started


http://www.lynda.com/Android-2-tutorials/Android-App-Development-with-Java-Essential-Training