Object oriented programming arose out of the desire to create a more natural method for dealing with large coding projects. Rather than try to manage huge numbers of individual methods, code was broken up into objects. These objects reflected the acutal structure of the things the programmer was working with. For instance, a point of sale application might have objects for customer, item, sale, etc. Objects can also be used for systems and networks: things like connection objects. In the programming environment, forms are objects, as are buttons and text boxes, etc.
A class is the abstract description of an object. A class describing a customer, for instance, describes an ideal customer, not any particular customer.
Classes can contain fields (class level variables), properties, methods, constructors, and events.
There are four basic principles of Object oriented programming:
Abstraction
Objects should be abstractions of things. They should represent typical or generic descriptions of things.
Polymorphism
Polymorphism refers to the the ability of objects to behave differently in differnt contexts. For example the + sign between two numbers adds the numbers, between two strings it concatinates the strings. It behaves differently depending on context.
Polymorphism is primarily achieved through two techniques: Overloading and overwriting methods.
Inheritance
Inheritance allows you to derive a new object from an existing one and automatically get all the public variables, methods and properties of the parent. It promotes code reuse.
Encapsulation
Encapsulation is the principle that every object should be as self contained as possible. It should contain all that it needs to functions and be as independent as possible from other objects. The idea is that an object should be like a component or a lego, that you can plug in whereever you need and have it work.
Here is a picture of the form running:
Here is the Xaml for the form
<Window x:Class="MileageWithClass.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Blue">
<Grid>
<Label Content="Enter total Miles" Height="28" HorizontalAlignment="Left" Margin="47,40,0,0" Name="label1" VerticalAlignment="Top" Foreground="White" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="234,40,0,0" Name="txtMiles" VerticalAlignment="Top" Width="120" FontSize="16" />
<Label Content="Enter the total gallons" Height="28" HorizontalAlignment="Left" Margin="25,87,0,0" Name="label2" VerticalAlignment="Top" Foreground="White" FontSize="16" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="234,93,0,0" Name="txtGallons" VerticalAlignment="Top" Width="120" FontSize="16" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="64,172,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
Here is the Mileage Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MileageWithClass
{
public class Mileage
{
//empty constructor
public Mileage()
{
NumberOfMiles = 0;
NumberOfGallons = 0;
PricePerGallon = 0;
}
//overloaded constructor
public Mileage(double miles, double gallons)
{
NumberOfMiles = miles;
NumberOfGallons =gallons;
PricePerGallon = 0;
}
//private fields
private double numberOfMiles;
//public properties that expose private fields
public double NumberOfMiles
{
get
{
return numberOfMiles;
}
set
{
numberOfMiles = value;
}
}
private double numberOfGallons;
public double NumberOfGallons
{
get { return numberOfGallons; }
set { numberOfGallons = value; }
}
private double pricePerGallon;
public double PricePerGallon
{
get { return pricePerGallon; }
set { pricePerGallon = value; }
}
//public method
public double CalculateMileage()
{
return NumberOfMiles / NumberOfGallons;
}
//overloaded method
public double CalculateMileage(double miles, double gallons)
{
NumberOfMiles = miles;
NumberOfGallons = gallons;
return NumberOfMiles / NumberOfGallons;
}
}
}
Here is the code for the button in the form
private void button1_Click(object sender, RoutedEventArgs e)
{
//Mileage mileage = new Mileage();
//mileage.NumberOfMiles = double.Parse(txtMiles.Text);
//mileage.NumberOfGallons = double.Parse(txtGallons.Text);
Mileage m = new Mileage(double.Parse(txtMiles.Text),
double.Parse(txtGallons.Text));
double milesPerGallon = m.CalculateMileage();
MessageBox.Show("the miles per gallon is "
+ milesPerGallon.ToString());
}
No comments:
Post a Comment