programming using c# for teachers introduction to objects reference types functions of classes...
TRANSCRIPT
Programming using C# for Teachers
Introduction to Objects
Reference Types
Functions of Classes
Attributes and Types to a class
LECTURE 2
Summary of last lecture
Hello world recap
Objects & Classes?
Variables & variable declarations
Object Manipulation
What’s to come today?
• Introduction to Objects
• Naming classes(Abstract, base and
inherited)
• Reference (object) types
• Functions of classes (reusability)
• Attributes to a class
Objects
• An object is an instance of a class, you can have many
different instances of the same class
• All objects have states and behaviours.
• An object’s state is determined by it’s fields (variables)
• An object’s behaviour is determined by it’s methods
State-OffBehaviour-No current flowing
State-OnBehaviour-Current flowing
Inheritance
• Simply put inheritance is when a class is defined in terms of
another class
• This allows for the reuse of code
• Inheritance implements an IS-A relationship. E.g A giraffe IS-
A mammal.
• Inheritance is one of the most important concepts of object
oriented programming.
Inheritance Example
Inheritance Examplepublic class ParentClass{
public ParentClass() {
Console.WriteLine("Parent
Constructor.");
}
public void print() {
Console.WriteLine("I'm printing from a
Parent Class.");
}
}
public class ChildClass1 : ParentClass
{
public ChildClass1()
{
Console.WriteLine("Child
Constructor.");
}
public static void Main()
{
ChildClass1 child = new ChildClass1();
child.print();
}
}
public class ChildClass2 : ParentClass
{
public ChildClass2()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass2 child = new ChildClass2();
child.print();
}
}
Both child classes are able to access the parent class’ public
method “print()”
Abstract Classes
• A class that can not be instantiated, only sub classed
(inheritance)
• An abstract class can contain abstract methods
• An abstract method is a method that has a declaration
but has no statements
• We use an abstract class because we want to require
subclasses to have certain methods or variables but
want that subclass to fill in the rest of the code
Abstract Classes
• An abstract class is the only type of class that can contain an
abstract method, otherwise the compiler will produce an error
• If an abstract class has an abstract method, then any class
that inherits the abstract class will have to override the
abstract method
Abstract Class Example
class Program
{
static void Main(string[] args)
{
Giraffe raffe1 = new Giraffe();
raffe1.printFeatures();
raffe1.print();
Console.Read();
}
}
abstract class Mammal{
protected int numOfLimbs;
public abstract void setFeatures(int numLimbs);
public abstract void printFeatures();
public void print(){
Console.WriteLine(“This animal is a
mammal");
}
}
class Giraffe : Mammal
{
public Giraffe()
{
setFeatures(4);
}
public override void
setFeatures(int numLimbs)
{
this.numOfLimbs = numLimbs;
}
public override void printFeatures()
{
Console.WriteLine("The giraffe
has " + numOfLimbs + " legs.");
}
}
Abstract Class Example
• An abstract class can also contain concrete methods
such as the print() method in the previous example
• Concrete methods can be used so that all sub classes
have access to that method
• The result of our simple program is as follows
Base and Inherited Classes
• A base class is a class from which other classes are
derived from.
• Code can be implicitly inherited from a base class and
can be reused by the child (sub/derived) class.
• An inherited class is a class that is based on another
class.
• Inheritance allows for the reuse of code from a base
class and allows for independent changes to be made
Base Class and Inherited Class Example
public class Bird{
protected bool canFly;
protected void setCanFly(bool cFly)
{
canFly = cFly;
}
public void message(){
if (this.canFly){
Console.WriteLine("This bird
can fly");
}
else{
Console.WriteLine("This bird
cannot fly");
}
}
}
public class Robin: Bird
{
public Robin(){
setCanFly(true);
}
}
class birdTest{
static void Main(string[] args)
{
Robin robin1 = new Robin();
robin1.message();
Console.Read();
}
}
Parent Class Child Class
Reference Types
• A reference type is a data type that stores a reference to it’s data
instead of storing the data’s value.
• So a reference variable is stored and the actual data is stored
• This means different variables can reference a single piece of data.
• Since a single piece of data can be accessed by different variables.
Modifying one variable may affect another.
• References are like labels for objects
• You can change and move the labels around but they always refer to
the one object
• Examples of reference types in C#;
-Classes
-Interfaces
-Delegates
Example using Classes
Name = john;
john = new Name();
Name mark = john;
Variable Name – is a reference to the Name object
Name is a class Memory allocates two different spaces; one for the data and one for the reference to
that data
Only one space is made available for the reference
to the data to be copied into.
Name Objectjohn
mark
Methods and References
• By default methods pass parameters by value in C#
• i.e. it will pass either a copy of the value or a copy of
the reference depending on whether the parameter is a
value or reference type.
• Parameters can be passed by reference instead of
value by using the ref keyword
foo(ref string eyeColour) {
Console.WriteLine(“This person has” + eyeColour+” eyes.”);
}
…..
foo(ref string eyeColour); //method call
Functions of Classes
• The use of classes in object oriented programming allows for
great reusability purposes.
• Classes usually do one specific thing and should be named to
suit it’s purpose
• Once we have written a class we can access that class and
use all of it’s methods and functionality
• To do this we need to make sure we have used the “using”
keyword or that the class is in the namespace we are using
Functions of Classes
• Once we can use the class outside of itself, there is
great potential for reusability
• It is possible to call methods from that class and use
their functionality without having to rewrite all the code
• For instance if you are looking to create a program to
calculate the price of travelling by car to three different
places.
• You could write three different classes to calculate each distance
• or you could write one class that would allow for three objects to
be created which could store the price.
Attributes
• An attribute is a special tag in a C# class
• It stores metadata about objects, methods or
properties
• An attribute is usually a property of a property
• An attribute is marked by being enclosed by []
• There are many predefined attributes available but
it also possible to make custom attributes
Attribute Example#define DEBUGusing System; using System.Diagnostics;
public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } }
class Test { static void function1() { Myclass.Message("In Function 1."); function2(); } static void function2() { Myclass.Message("In Function 2."); } public static void Main() { Myclass.Message("In Main function."); function1(); Console.ReadKey(); } }
Sets DEBUG to true if in debug mode
Predefined Attribute checks if DEBUG is true
Calls on the attribute class’ method
The messages will only be printed if the compiler is in debug mode
What did we cover today?
• Introduction to Objects
•Naming classes(Abstract, base and
inherited)
•Reference (object) types
•Functions of classes (reusability)
•Attributes to a class
What’s to come next time
• Principles of control
structures
in terms of
sequence and
selection