Вы находитесь на странице: 1из 24

Windows Programming

November 2016
Object Oriented Programming
Concepts
Topics

• OOP?
• Classes and Objects
• Constructors
• Properties

01/01/2021 Windows Programming 3


OOP?
• an approach to address many of the problems
of functional (or procedural) programming
• offers many modules of code
– each offering specific functionality
– many more modules, isolated or completely
independent
– gives much more versatility
– code reuse
• Key characteristics OOL?
01/01/2021 Windows Programming 4
Classes
• main data structure in C#
• blends ideas from both C++ and Java
– methods are declared inline, as in Java
– no C++ style header files
– supports only single inheritance
– inheritance syntax from C++
– allows multiple classes to be declared in one file
like C++
– methods can be used before declared

01/01/2021 Windows Programming 5


Class Definition

[attributes] [access-modifiers] class identifier


[:base-class] using System;
{
public class Point
class-body {
} int x;
int y; fields

}
01/01/2021 Windows Programming 6
Class Definition

01/01/2021 Windows Programming 7


Access Modifiers
Modifier Explanation
public Visible to any method of any class

private Visible only to methods of the declaring class. This


is the default access modifier if none is specified.
protected Visible to declaring class and subclasses

internal Visible to any method in the same assembly as the


declaring class
protected Visible to declaring class, subclasses, and classes in
internal the same assembly

01/01/2021 Windows Programming 8


Object Creation
• Primitive types like int, char, etc. are value
types and are created on the stack
• Objects are reference types, created on the
heap
– Declare an object reference variable
– Use the new operator to create a class instance on
the heap
• Assign this new instance to the reference variable

01/01/2021 Windows Programming 9


Object Creation
• class_name object_identifier = new class_name();
• point startingPoint = new Point();
– This creates a new instance on the heap
– The constructor for the new instance is invoked
– The reference to the new instance is assigned to
the variable
– The object will be destroyed when there are no
more references to it and it is reclaimed by the
garbage collector

01/01/2021 Windows Programming 10


What’s in a Class?
• Members class Line
{
– Data
public Point starting = new Point();
– Function public Point ending = new Point();
public double len;
}
class Point
{
public int x;
public int y;
}
01/01/2021 Windows Programming 11
Using Members
• can be used like any variable or literal
class lineApp
{
public static void Main()
{
Line myLine = new Line();
myLine.starting.x = 1;
myLine.starting.y = 4; …
01/01/2021 Windows Programming 12
Using Members
class lineApp
class Point
{
{ public static void Main()
public int x; {
public int y; Line line1 = new Line();
} Line line2 = new Line();
class Line //Assign values to members…
{
static public Point origin= new Point();
public Point ending = new Point();
}
01/01/2021 Windows Programming 13
Constructors
Life Cycle of an Object
• Construction
– When an object is first instantiated it needs to be
initialized
– carried out by a constructor function
• Destruction
– some clean-up tasks when an object is destroyed
– job of a destructor function

01/01/2021 Windows Programming 14


Constructors
• a special method with the same name as the
class
• run to initialize the class
• public, internal, private, protected
• before it is run the class is undifferentiated
memory
• after it is run, memory is an instance of the
class
01/01/2021 Windows Programming 15
Constructors
• never have a return type
• usually public
• may be overloaded
• if no constructor is provided, the CLR will
create one which does nothing
• if any constructor is provided, no default
constructor will be created

01/01/2021 Windows Programming 16


Constructors
• could be static
– executes once per type
– only one parameterless constructor per class
– no guarantees about when a static constructor will
be executed, no code …
– cannot predict in what order static constructors of
different classes will execute
class Test
{
static Test() { Console.WriteLine ("Type Initialized"); }
}
01/01/2021 Windows Programming 17
Constructors
• could be static
– invoked before your code makes any reference to
the class
– never called by any other C# code, no access
modifiers
– can access only static members
– possible to have a static constructor and a zero-
parameter instance constructor
– if more than one class with static constructor,
which executes first is undefined

01/01/2021 Windows Programming 18


Member Initialization

• c# allows you to initialize a member variable


when it is declared
– int day = 30;
• an initializer
• if no initializer, a default value is assigned
• no uninitialized variables in C#

01/01/2021 Windows Programming 19


Object Initializers
• simplifies object initialization
• can make constructor accept optional
parameters instead
• Example:

01/01/2021 Windows Programming 20


Properties
public class Stock
• a method or a pair {
of methods decimal currentPrice;
dressed to look public decimal CurrentPrice
like a field
• Why Properties? {
• keywords get and get { return currentPrice; }
set
set { currentPrice = value; }
}
}
01/01/2021 Windows Programming 21
Properties
• could be read only | write only
• could be calculated
• access modifiers

decimal currentPrice;
decimal
decimalcurrentPrice,
currentPrice; sharesOwned;
public decimal CurrentPrice
public
public decimal
decimal Worth
CurrentPrice
{
{{
get { return currentPrice; }
get { return currentPrice;
currentPrice *} sharesOwned; }
}
} private set { currentPrice = value; }
}
01/01/2021 Windows Programming 22
Properties
• could be automatic
• property accessors internally compile
to methods

//decimal currentPrice; Not needed


public decimal CurrentPrice
{
get ; set;
}

01/01/2021 Windows Programming 23


• Read about indexers

01/01/2021 Windows Programming 24

Вам также может понравиться