Upload
psywar
View
220
Download
0
Embed Size (px)
Citation preview
8/10/2019 Object-oriented programming 2-Prefinal.pptx
1/68
OBJECT-ORIENTED
PROGRAMMING2
Introduction to Object-OrientedProgramming
8/10/2019 Object-oriented programming 2-Prefinal.pptx
2/68
WHAT YOU WILL LEARN IN THIS
CHAPTER
What object-oriented programming is
How to use OOP techniques
How desktop applications rely on OOP
8/10/2019 Object-oriented programming 2-Prefinal.pptx
3/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
4/68
What Is a Class?
When you definea class, you definea blueprint for adata type or
object A class definition
starts with thekeyword class
followed by theclass name; andthe class body,enclosed by a pairof curly braces.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
5/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
6/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
7/68
public int MyIntProp
{
get
{
// Property get code.}
set
{
// Property set code.
}
}
// Field used by property.
private int myInt;
// Property.
public int MyIntProp
{
get{
return myInt;
}
set
{
// Property set code.}
}
8/10/2019 Object-oriented programming 2-Prefinal.pptx
8/68
Member Functions and Encapsulation
Member variables areattributes of an object and they
are kept private to implement
encapsulation. These variables
can only be accessed using the
public member functions.
A member function of a class
is a function that has its
definition or its prototype
within the class definition like
any other variable. It operates
on any object of the class ofwhich it is a member, and has
access to all the members of
a class for that object.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
9/68
Access Modifiers for Class Definitions
MODIFIER DESCRIPTION
none or internal Class is accessible only from within the current
project
public Class is accessible from anywhere
abstract or internal abstract
Class is accessible only from within the current
project, and cannot be instantiated, only derivedfrom
public abstract
Class is accessible from anywhere, and cannot be
instantiated, only derived from
sealed or internal sealed
Class is accessible only from within the current
project, and cannot be derived from, only
instantiated
public sealed
Class is accessible from anywhere, and cannot be
derived from, only instantiated
8/10/2019 Object-oriented programming 2-Prefinal.pptx
10/68
public abstract class MyClass
{
// Class members, may be abstract.
}
public sealed class MyClass
{
// Class members.
}
internal class MyBase{
// Class members.
}
virtual The method can be overridden.
abstract The method must be overridden
in non-abstract derived classes (only permitted
in abstract classes).
override The method overrides a base
class method (it must be used if a method is
being overridden).
extern The method definition is found
elsewhere.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
11/68
Constructors
A class constructor isa special memberfunction of a classthat is executedwhenever we createnew objects of thatclass.
A constructor willhave exact samename as the class andit does not have anyreturn type. Followingexample explains the
concept ofconstructor:
8/10/2019 Object-oriented programming 2-Prefinal.pptx
12/68
Destructors A destructoris a special
member function of aclass that is executedwhenever an object ofits class goes out ofscope. A destructorwillhave exact same nameas the class prefixed
with a tilde (~) and itcan neither return avalue nor can it take anyparameters.
Destructor can be veryuseful for releasingresources beforecoming out of theprogram like closingfiles, releasingmemories etc.Destructors cannot beinherited or overloaded.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
13/68
Static and Instance Class Members
As well as having members such as properties,methods, and fields that are specific to objectinstances, it is also possible to have static (also knownas shared)
Static members are shared between instances of aclass, so they can be thought of as global for objectsof a given class. Static properties and fields enableyou to access data that is independent of any objectinstances, and static methods enable you to execute
commands related to the class type but not specific toobject instances. When using static members, in fact,you donteven need to instantiate an object.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
14/68
Inheritance
allows us to define a class in terms of another class,which makes it easier to create and maintain anapplication. This also provides an opportunity to reusethe code functionality and fast implementation time.
In OOP terminology, the class being inherited from(derived from) is theparent class (also known as thebase class)
Inheritance enables you to extend or create more
specific classes from a single, more generic base class.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
15/68
InheritanceThe syntax used in C# for creating derived classes is as follows:
class { ... }
class :
{ ... }
8/10/2019 Object-oriented programming 2-Prefinal.pptx
16/68
Polymorphism
All objects instantiated from a derived class canbe treated as if they were instances of a parentclass.
often expressed as 'one interface, multiplefunctions'.
Polymorphism can be static or dynamic. In staticpolymorphism the response to a function is
determined at the compile time. In dynamicpolymorphism , it is decided at run-time.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
17/68
Static Polymorphism
The mechanism of linking a function with an
object during compile time is called early
binding. It is also called static binding. C#
provides two techniques to implement staticpolymorphism. These are:
Function overloading
Operator overloading
8/10/2019 Object-oriented programming 2-Prefinal.pptx
18/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
19/68
Operator Overloading
You can redefine oroverload most of thebuilt-in operatorsavailable in C#. Thusa programmer canuse operators with
user-defined types aswell. Overloadedoperators arefunctions with specialnames the
keyword operatorfollowed by the symbolfor the operatorbeing defined
8/10/2019 Object-oriented programming 2-Prefinal.pptx
20/68
Dynamic Polymorphism
C# allows you to create abstract classes that are used toprovide partial class implementation of an interface.Implementation is completed when a derived classinherits from it. Abstract classes contain abstractmethods, which are implemented by the derived class.
The derived classes have more specialized functionality. Please note the following rules about abstract classes:
You cannot create an instance of an abstract class
You cannot declare an abstract method outside an abstractclass
When a class is declared sealed, it cannot be inherited,abstract classes cannot be declared sealed.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
21/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
22/68
Dynamic Polymorphism
When you have a functiondefined in a class that youwant to be implemented in aninherited class(es), youuse virtualfunctions. The
virtual functions could beimplemented differently indifferent inherited class andthe call to these functions willbe decided at runtime.
Dynamic polymorphism isimplemented by abstractclassesand virtual functions.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
23/68
Interfaces
is a collection of public instance (that is, nonstatic)methods and properties that are grouped together toencapsulate specific functionality.
describes the behavior or capabilities of a C# class
without committing to a particular implementation ofthat class.
define properties, methods and events, which are themembers of the interface. Interfaces contain only thedeclaration of the members. It is the responsibility of
the deriving class to define the members. It oftenhelps in providing a standard structure that thederiving classes would follow.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
24/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
25/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
26/68
OOP TOOLS IN VISUAL STUDIO
The Class View WindowThis window shows you the class hierarchy
of your application and enables you to see at a
glance the characteristics of
the classes you use.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
27/68
OOP TOOLS IN VISUAL STUDIO
The Object Browser
The Object Browser is an expanded version of the Class View window,
enabling you to view other classes
available to your project, and even external classes.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
28/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
29/68
OOP TOOLS IN VISUAL STUDIO
Class DiagramsOne powerful feature of VS that you havent looked at yet is the
capability to generate class diagrams from code and use them to
modify projects. The class diagram editor in VS enables you to
generate UML-like diagrams of your code with ease.
Features:
Classes are shown as blue boxes, including their name and type.
Interfaces are shown as green boxes, including their name and
type.
Inheritance is shown with arrows with white heads (and in some
cases, text inside class boxes).
Classes implementing interfaces have lollipops.
Abstract classes are shown with a dotted outline and italicized
name.
Sealed classes are shown with a thick black outline.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
30/68
OOP TOOLS IN VISUAL STUDIO
CLASS LIBRARY PROJECTS
A project that contains nothing but classes (along with other relevant type definitions,
but no entry point) is called a class library.
Class library projects compile into .dll assemblies, and you can access their contents
by adding references to them from other projects
8/10/2019 Object-oriented programming 2-Prefinal.pptx
31/68
OBJECT-ORIENTED
PROGRAMMING2
Basic Desktop Programming
8/10/2019 Object-oriented programming 2-Prefinal.pptx
32/68
Basic Desktop Programming
WHAT YOU WILL LEARN IN THIS CHAPTERHow to use the WPF designer
How to use controls for displaying information to the user, such as the
Label and TextBlock controls
How to use controls for triggering events, such as the Button control
How to use the controls that enable users of your application to enter text,
such as the TextBox control
How to use controls that enable you to inform users of the current state
of the application and allow the user to change that state, such as the
RadioButton and CheckButton controls
How to use controls that enable you to display lists of information, such as
the ListBox and ComboBox controls
How to use panels to lay out your user interfaces
8/10/2019 Object-oriented programming 2-Prefinal.pptx
33/68
Ways to create user interface in Visual Studio Windows Forms
basic tool for creating applications that target classicWindows
use Windows Presentation Foundations (WPF)
provide a wider range of application types and attempts tosolve a number of problems with Windows Forms
is technically platform-independent, and some of its
flexibility can be seen in the fact that a subset of WPFcalled Silverlightis used to create interactive webapplications
8/10/2019 Object-oriented programming 2-Prefinal.pptx
34/68
VS Tools in GUI Design
Window Designer
heart of the development of most graphical
Windows applications
In Windows Form, it is used to create a userinterface by dragging and dropping controls from
a Toolbox to your window, placing them where
you want them to appear when you run the
application
In WPF, the user interface can be done by
dragging and dropping controls with writing raw
XAML.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
35/68
XAML
Extensible Application Markup Language (XAML, pronounced
zammel)
a language that uses XML syntax
(Extensible Markup Language (XML) is a technology for
storing data in a simple text format and use by .NET, from
describing the configuration of applications to
transporting information between web services)
and enables controls to be added to a user interface in a
declarative, hierarchical way
you can add controls in the form of XML elements, and
specify control properties with XML attributes.
a powerful way to declare user interfaces, it is not a
programming language
8/10/2019 Object-oriented programming 2-Prefinal.pptx
36/68
XAML
XAML is designed with todays powerful graphics cards in mind,and as such it enables you to use all the advanced capabilities thatthese graphics cards offer through DirectX. The following listssome of these capabilities:
Floating-point coordinates and vector graphics to provide layout thatcan be scaled, rotated, and otherwise transformed with no loss ofquality
2D and 3D capabilities for advanced rendering
Advanced font processing and rendering
Solid, gradient, and texture fills with optional transparency for UIobjects
Animation storyboarding that can be used in all manner of situations,including user-triggered events such as mouse clicks on buttons
Reusable resources that you can use to dynamically style controls
8/10/2019 Object-oriented programming 2-Prefinal.pptx
37/68
XML Sample Code
Beginning Visual C# 2012
Karli Watson
7582
Professional C# 2012
Simon Robinson
7043
8/10/2019 Object-oriented programming 2-Prefinal.pptx
38/68
XAML SAMPLE CODE
8/10/2019 Object-oriented programming 2-Prefinal.pptx
39/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
40/68
WPF Controls
Controls combine prepackaged code and a
GUI that can be reused to create more
complex applications.
They can define how they draw themselves
by default and a set of standard behaviors.
Example: Label, Button, and TextBox controls
8/10/2019 Object-oriented programming 2-Prefinal.pptx
41/68
Adding Controls to a Window
Example
Add controls to the Design View by draggingthem from the Toolbox panel or by typing theXAML manually.
Start by dragging a Button control from the Toolboxonto the Design View. Notice how the text in theXAML View is updated to reflect the change youmade.
Now drag another Button, but this time drop it in the
XAML View below the first Button, but above the tag.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
42/68
Properties
all controls have a number of properties that are usedto manipulate the behavior of the control. Example;height and width
Dependency Properties
property that is registered with the WPF property systemin such a way as to allow extended functionality. Thisextended functionality includes, but is not limited to,automatic property change notifications.
Attached Properties is a property that is made available to each child object of
an instance of the class that defines the property.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
43/68
Manipulating Properties1. Start by selecting the second Button control in Design View;
this is the button that is currently filling the entire window.
2. You can change the name of the control in the Properties
panel at the very top. Change it to rotatedButton.
3. Under the Common node, change the Content to 2nd Button.
4. Under Layout, change width to 75 and height to 22.5. Expand the Text node and change the text to bold by clicking
the B icon.
6. Select the first button and drag it to a position above the
second button. Visual Studio will assist with the positioning by
snapping the control.7. Select the second button again, and hover the mouse pointer
over the top-left corner of it. The pointer changes to a quarter-
circle with arrows on both ends. Drag down until the button is
tilted down.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
44/68
Manipulating PropertiesThe XAML code for the window should now look like this:
8/10/2019 Object-oriented programming 2-Prefinal.pptx
45/68
Events
8/10/2019 Object-oriented programming 2-Prefinal.pptx
46/68
Handling Events
two basic ways to add a handler for an event.
use the Events list in the Properties window
type the name of the event directly in XAML and add the
name of the handler there.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
47/68
Routed Events
WPF uses events that are called routed events. Astandard .NET event is handled by the code that hasexplicitly subscribed to it and it is sent only to thosesubscribers. Routed events are different in that theycan send the event to all controls in the hierarchy inwhich the control participates bobbling event- event travels up the control hierarchy
Ex. ButtonGridWindow
tunneling event - event travel in the other direction, that
is, from the root element to the control on which theaction was performedand by convention all events likethis are prefixed with the word Preview
Ex. PreviewMouseButtonDown event
8/10/2019 Object-oriented programming 2-Prefinal.pptx
48/68
Control Types
Two Types
Content controls,
a control that have Content property, such as the
Button control,
Items control,
a control that allows you to insert multiple controls as
content. Example: Grid control.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
49/68
CONTROL LAYOUT All content layout controls derive from the abstract Panelclass. This class
simply defines a container that can contain a collection of objects that derivefrom UIElement. All WPF controls derive from UIElement.
You cannot use the Panel class directly for control layout, but you can derive
from it if you want to.
Alternatively, you can use one of the following layout controls that derive
from Panel: CanvasThis control enables you to position child controls any way you see fit. It doesnt
place any restrictions on child control positioning, but nor does it provide any assistance in
positioning.
DockPanelThis control enables you to dock child controls against one of its four edges.
The last child control fills the remaining space.
Grid This control enables flexible positioning of child controls. You can divide the layout of
this control into rows and columns, which enables you to align controls in a grid layout.
StackPanelThis control positions its child controls in a sequential horizontal or vertical
layout.
WrapPanel This control positions its child controls in a sequential horizontal or vertical
layout as StackPanel, but rather than a single row or column of controls,
8/10/2019 Object-oriented programming 2-Prefinal.pptx
50/68
Stack Order
When a container control contains multiple child controls,
they are drawn in a specific stack order.
The first child in a container is placed on the lowest layer
in the stack, and the last child on the topmost layer.
Alignment, Margins, Padding, and Dimensions
Alignment determine how the control is aligned. Two
alignment properties, HorizontalAlignment (Left, Right,
Center, or Stretch)and VerticalAlignment (Top, Bottom,
Center, or Stretch),
Margin and Padding specify the space to leave blank
around the edges of controls and inside the edges of
controls, respectively.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
51/68
Border
control is a very simple, and very useful, container
control. It holds a single child, not multiple children
Canvas provides complete freedom over control positioning
and the HorizontalAligment and VerticalAlignment
properties used with a child element will have no
effect whatsoever over the positioning of thoseelements.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
52/68
Canvas
8/10/2019 Object-oriented programming 2-Prefinal.pptx
53/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
54/68
StackPanel
slimmed down version of DockPanel, where
the edge to which child controls are docked is
fixed for those controls.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
55/68
WrapPanel
essentially an extended version of
StackPanel; controls that dont fi t are
moved to additional rows (or columns).
8/10/2019 Object-oriented programming 2-Prefinal.pptx
56/68
Grid
Grid controls can have multiple rows and columns that you can use to lay out child controls.You have used Grid controls several times already in this chapter, but in all cases you used a
Grid with a single row and a single column. To add more rows and columns, you must use
the RowDefinitions and ColumnDefinitions properties, which are collections of
RowDefinition ,respectively, and are specifi ed using property element syntax:
Th I C t l
8/10/2019 Object-oriented programming 2-Prefinal.pptx
57/68
The Image Control
Image is a very simple control that can be
used to great effect. It allows you to display asingle image and to resize this image as you
see fit. The control exposes two properties,
8/10/2019 Object-oriented programming 2-Prefinal.pptx
58/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
59/68
8/10/2019 Object-oriented programming 2-Prefinal.pptx
60/68
The Button Control
This control is used everywhere and is easily
recognized on a user interface
The button does not contain any properties to
display images or text, but you can use the Contentproperty to display simple text or embed an Image
control in the content to display an image
8/10/2019 Object-oriented programming 2-Prefinal.pptx
61/68
The TextBox Control These controls are designed exclusively for displaying text to the
user. The TextBox control allows the user to type text into the
application.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
62/68
The CheckBox Control
CheckBoxes present the users with options
that they can select or clear. You should use a
CheckBox if you have want to present an
option to the users that can be turned on oroff, or want the users to answer yes or no to
a question.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
63/68
The RadioButton Control
RadioButtons are used with other RadioButtons to
allow users to choose between multiple options
where only one can be selected at any time. You
should use RadioButtons when you want the users
to answer a question that has a very limited number
of possible values.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
64/68
The ComboBox Control
ComboBoxes are commonly used to displaylong lists of values and allow users to selectexactly one option in a drop-down list.
A ComboBox can be changed to display itselfwith a TextBox at the top that allows theusers to type any values that they feel aremissing.
A ComboBox is an Items control, whichmeans that you can add multiple items to it.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
65/68
ComboBox Properties
8/10/2019 Object-oriented programming 2-Prefinal.pptx
66/68
The TabControl
It is a layout control that is used to groupcontrols on pages that can be selected byclicking on them.
Tab controls are used when you want to displaya lot of information in a single window but dontwant to clutter the view too much. In this case,you should divide the information into groups of
related items and create a single page for eachgroup.
8/10/2019 Object-oriented programming 2-Prefinal.pptx
67/68
The ListBox Control
ListBoxes often allows the user to select
multiple items and display its content in a list
that is always expanded
8/10/2019 Object-oriented programming 2-Prefinal.pptx
68/68