Association
The first relationship is Association. This just means that two classes are associated, that one can call methods in the other.
Here is the class diagram for association. (I am at home and so am using Visio 2013. The diagrams will look a little different than the ones I did at school.)
Here is some very simple code to show what association can look like in practice
Class1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Class1 { //this is more complex than need be //I made a string field to hold the //value returned from class two //then I made a property for it. //I have a method that calls class2 //and assigns the value returned to the //string. The method is called from //the constructor string classTwoString; public Class1() { CallClass2(); } public string ClassTwoString { get { return classTwoString; } set { classTwoString = value; } } private void CallClass2() { Class2 c2 = new Class2(); ClassTwoString=c2.HereIAm(); } } }
Class2
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Class2 { //this class is beyond simple //it contains one method //that returns a string public string HereIAm() { return "Hello from Class 2"; } } }
Inheritance
Inheritance is a "Generalization/specialization" relationship. The parent is more generalized, the child more specialized or specific. So in our diagram we have Person as the most general class. Customer and Employee are more specialized versions of Person. HourlyEmployee, SalariedEmployee, and ContractEmployee are more specialized versions of Employee.
Inheritance allows you to abstract out common elements and reduce repetition. Child classes inherit all public fields and methods from the parent
Here are all the inheritance classes
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace inheritanceExample { abstract class Person { public string Name { set; get; } public string Address { set; get; } public string City { set; get; } public string State { set; get; } public string phone {set; get;} } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace inheritanceExample { class Customer:Person { public string CustomerID { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace inheritanceExample { abstract class Employee:Person { public string HireDate { get; set; } public string Title { set; get; } public string EmployeeID { set; get; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace inheritanceExample { class Hourly:Employee { public double Rate { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace inheritanceExample { class Program { static void Main(string[] args) { Customer c = new Customer(); Hourly h = new Hourly(); } } }
Interface
An interface is an abstraction of methods. When a class implements an interface it "Contracts" to implement all the methods in the interface
Here is the code for an interface
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { interface Interface1 { public double CalculatePay(); } }
No comments:
Post a Comment