صفحه 1:
برنامه سازی پیشر فته
صفحه 2:
Object-Oriented
Design.
™The focus of methods is on doing
things; roughly speaking, we can
say that methods focus on the
verbs.
™In object-oriented design, we
focus on the nouns
a ire ceria oa cate coca لمجا وبند رج oN ara eRe peas
۱ ۵۲ 0-7
eld Nore Chel and cP Cac ced aoa Mod
صفحه 3:
User-Defined Class
ES
™ A user-defined class is also called a
user-defined type
ede eros
™ A class encapsulates (wrap together)
data and methods:
Serr need Cee etree eee eee eee oo)
01 ل aed
صفحه 4:
Objects
™ An object has:
لل 000 eens
ee Ten aa tt)
™ For example, consider a coin ina
computer game
Sy er te te teed coat)
Rd ea eed ded a
™ Note the interactions between state and
behaviors
۱ ee ne ee
00 oa et een ee eee
صفحه 5:
تعاریف
22
ی به دستهها «کلاس» )1-1-9 ۳۳
ل Sha 0
"! مشخصات هر شى را «صفت» (! 1161186 0313) مىنامند.
eS SCS lia ل ا ۱
صفحه 6:
اصول برنامهنویسی شی گرا
بستهبندی (6۳۸6۵۵5۱۷۱۵]100)
#اوراثت (ع6م3غ21عطم1)
* ل نا
صفحه 7:
بستهبندی
(Encapsulation),
ee Pee 000
مخفى شود.
صفحه 8:
(Inheritance) sl),
تس
#دردنیای واقعی» ورافت بهاین معناست که یک شی وقتی
متولد میشوده خصوصیات و ویژگیهایی را از والا خود به
0 ۳
ا ل ۱
مىشود و علاوه بر اين كه مىتوان از كدهاى قبلى استفاده
SCRN ECT) ا ا لاك
ease ا 00
صفحه 9:
1
لاسا
Prony ee eset ا ا Bal
ECE CCT Dy ep perc SB Bee mere Ngee BEG Conder
میکند.
صفحه 10:
Defining Classes
# Use to add a new
class to your project
™ A class contains and
ypublic int x, y
sprivate char ch مب هب
Member (data/method) Access
۱0۵56۲5
ee oo ay
تست
* member is accecsible only
صفحه 11:
Data Declarations
+۳ 6 اا 11 79
™ You can define two types of variables in a
class (called class variables)
فرص 3
NS RA aN ee a ١
becouse euch iusteue (object) oP the class hos its pws copy
SRN a eR a a Rad
صفحه 12:
Method Declarations
۲ ۵ و وا روم اعد موی ون caso
SR eee ee a Cd
سم مس سم
«۶ سین
Se eee ere ee
Sry hee the sxecram reer see thre kane
Se ee ee ee
cee)
Se nea
Oe nee eee
۱ تم و
See keane
ee ceed nel
صفحه 13:
ا Ber) Far eee] Be bE
210000 آنهاء CL Sepa ۱0
ندارد. به اين سازنده سازندةٌ يبيش فرض (1(6121111
5111101017) مى كويند.
اكر در يك كلاسء سازندةً ييشفرض تعريف
نشود. كاميايلر به طور خودكار أن را براى
كلاس مذكور ايجاد م ىكند.
صفحه 14:
Example: Time1 class
eo ۳ ۳ ج
™ We define the Timel class to represent time.
™ The state of a time object can be
۳۵۴۵۲۵5۹۵۸۲۵۵0 ۱:
ee eee MS teeter ees nd
™ We might define the following methods:
۱ ا
لل نا rT
ام م ا ا ا لل ال نا
لي يي با اس هت تما ی
SET ا ee nee nnn nnn nee et ad
Saeco he a SR gaa)
صفحه 15:
ee
۱۸/۸۵ US UL a
رمع ری ومقون
ECACC eC res tL)
0 ا
00 تا او
2 0
private int second; // 0-59
eu ل ا ا Con)
۱/۸ ال LT
01 Sa8)
7 <
)عمتكعمد 0, 0, 6 (
1
۱۸۸ ها ات ec aag
ee ce a eC OC Oe Ce
Pen عساةلالممععد غمذ ,عساولاءغ متم غمة ,عن اولامسهط غمذ )عسذكععد هذه (
5
بط ora re 0 && hourValue < 24 ) ? hourValue
minute = ( minuteValue >= 0 && minuteValue < 60 ) ? minuteValue : 0;
second = ( secondValue >= 0 && secondValue < 60 ) ? secondValue : 0;
1
ا ا ل ات ل بن لل سا سا ا م ا ا
5 ام ده ام 5
18
19
20
0
22
23
3
25
26
صفحه 16:
// convert time to universal-time (24 hour) format string
public string ToUniversalString()
{
return String. Format (
“{0:D2}:{1:D2}:{2:D2}", hour, minute, second );
// convert time to standard-time (12 hour) format string
public string ToStandardString()
{
return String.Format( “{0}:{1:02}:{2:02} {3}",
( (hour == 12 || how رو ۶ 12 : ۱۵۵۳ ۶ 12 (,
minute, second, ( hour < 12 ? "AM" : "PM" ) );
3
} // end class Time1
صفحه 17:
// TimeTest1.cs
// Demonstrating class Timel.
ا
M ar ere 0
ese)
acme aise
a
۳
۸/۸
0
i
۲1061 106001 - ككاق ۸۸ ز()لعمذ؟ بهم
Reet mens tha
// assign string representation of time to output
Cire ae rest enh ie ee st ee
timeObj1.ToUniversalString() +
"\nInitial standard time is: " +
لت See T0
عومنتاعء عهنا مناد امه //
timeObj1.SetTime( 13, 27, 6 );
Ae Ue ee ete ee ea
Cite Ti aeRO ER Ter en St Oe Da eC Tb er
time0bj1.ToUniversalString() +
صفحه 18:
1
DNC et ee SU CMCC ice t lO es
1ن es Cee SC CMCC stron
ees ee oS ECS) تسا
70000 ae)
ا ۱۳۸/۸
Initial universal time is: 00:00:00
Initial standard time is: 12:00:00 AM
Universal time after SetTime i
Standard time after SetTime is:
After attempting invalid settings:
Universal time: 00:00:00
Standard time: 12:00:00 AM
0۲ it
صفحه 19:
Class View and Object
Browser
™Class View and Object Browser are
features of Visual Studio that
facilitate the design of object-
oriented applications
=™Class View
ل
م لم ا ا
0
RRC ا cee)
۱ eee ee
صفحه 20:
Class View: Example
an
and Inte
صفحه 21:
ا لان
1
etre Ca A bee}
eee Caos Tea eed
ete Cae ea
0000
{
arc a
0
public Time2( int hour )
{
ee Ee ey )عسذتهد
1
۱ Leah)
ز( 6 ,ععنمقم ,عسمط و
1
۱ ee Cae Oa ure ae cL
0
bs
ات یت
| LaRue suas)
8
صفحه 22:
// TimeTest2.cs
// Using overloaded constructors.
using System;
using System.Windows.Forms;
// TimeTest2 demonstrates constructors of class Time2
class TimeTest2
time6;
00:00
00:00
34:00
25:42
00:00
25:42
// main entry point for application
static void Main( )
Time2 timel, time2, time3, time4, time5,
00:
02:
21:
12:
00:
12:
//
427
//
//
2
//
Time2();
Time2( 2 );
Time2( 21, 34 );
Time2( 12, 25, 42 );
Time2( 27, 74, 99 (۶
Time2( time4 );
new
new
new
new
new
new
timel =
time2
time3
time4 =
time5
time6 =
{
{
سر یه نب چ و و اب و وا
د م ما 8
صفحه 23:
Encapsulation: Two Views of an
Object
a
™You can take one of two views of
an object:
Sotercd - the structure oP its data, the dyoriheos ued
nec
® extercd ا oP the objet wits viker port
Ce aa
صفحه 24:
Encapsulation: An Object As a Black Box
™ From the external view, an object is an
fae ree a ga KFT) و 56۲ ۴
services
0 ا
۱ ل Ot Ete
۱ aU es a kaenaas eld agar CL ad
eee eee eer)
صفحه 25:
Accomplish Encapsulation: Access
Modifiers
أ آذآ ببس ااا
In C#, we accomplish encapsulation =
through the appropriate use of access
7002126۳5
" 0مسلاععا #© جه كة 7001116 و5وعءءءة مة
that specifies the accessibility of a
method, data field, or class
00 ل eee eke eA h a)
® Op utldsnes te vier two wolPers (protected, internal)
دا
صفحه 26:
The public and private Access Modifiers
|
™ Classes (types) and members of a class
that are declared with pubiic can be
accessed from anywhere
™ Members of a type that are declared
with »rivete can only be accessed from
inside the class
= Members of a class declared without an
access modifier have default private
|
صفحه 27:
// RestrictedAccess.cs
// Demonstrate compiler errors from attempt to access
// private class members.
class RestrictedAccess
{
// main entry point for application
static void Main( string{] args )
4
Timel time = new Timel();
time.hour = 7;
time.minute = 15;
time.second = 30;
¥// end ct (1)
سراي سه م هات ص فق ير ير يع بم يزب
صفحه 28:
پیادهسازی بستهبندی
به عنوان یک قانون کلی» هیچ عضو دادهای به صورت 16 0۱00
تعریف نمیشود.
"" از خصوصيات (1©5! ©1070 0) براى دسترسى به دادهها استفاده
مى كنيم.
اعضاي دادهاي به صورت جرهم و خصوصيات آنها به صورت
عاطم تعريف ميشوند.
۱
1 Dees enn
2 ا
صفحه 29:
صفحه 30:
Time2 time = new Time2();
time.Hour 15;
time.Minute = 45;
time.Second = 20;
برنامه سازي پيشرفته
2
Object-Oriented
Design
The focus of methods is on doing
things; roughly speaking, we can
say that methods focus on the
verbs.
In object-oriented design, we
focus on the nouns
In object-oriented design, we tend to group methods
together according to the nouns
Important for large, complex programs
User-Defined Class
A user-defined class is also called a
user-defined type
class written by a programmer
A class encapsulates (wrap together)
data and methods:
data members (member variables or instance variables)
methods that manipulate data members
Objects
An object has:
state - descriptive characteristics
behaviors - what it can do (or be done to it)
For example, consider a coin in a
computer game
The state of the coin is its current face (head or tail)
The behavior of the coin is that it can be flipped
Note the interactions between state and
behaviors
the behavior of an object might change its state
the behavior of an object might depend on its state
تعاريف
به دستهها «کالس» ( )Classميگويند.
به نمونههاي هر کالس «شي» ( )Objectميگويند.
مشخصات هر شي را «صفت» ( )Data Memberمينامند.
به رفتارهاي هر شي «متد» ( )Methodميگويند.
اصول برنامهنويسي شيگرا
بستهبندي ()Encapsulation
وراثت ()Inheritance
چندريختي ()Polymorphism
بستهبندي
()Encapsulation
يعني اين که دادههاي مرتبط ،با هم
ترکيب شوند و جزييات پيادهسازي
مخفي شود.
وراثت ()Inheritance
در دنياي واقعي ،وراثت به اين معناست که يک شي وقتي
متولد ميشود ،خصوصيات و ويژگيهايي را از والد خود به
همراه دارد.
امتياز وراثت در اين است که از کدهاي مشترک استفاده
ميشود و عالوه بر اين که ميتوان از کدهاي قبلي استفاده
مجدد کرد ،در زمان نيز صرفهجويي شده و استحکام منطقي
برنامه هم افزايش مييابد.
چندريختي ()Polymorphism
که به آن چندشکلي هم ميگويند به معناي يک چيز بودن و
چند شکل داشتن است .چندريختي بيشتر در وراثت معنا پيدا
ميکند.
Defining Classes
Use Project menu -> Add Class to add a new
class to your project
A class contains data declarations and
method declarations
class MyClass
;public int x, y
;private char ch
Data declarations
Method declarations
Member (data/method) Access
Modifiers
public : member is accessible outside
the class
Data Declarations
You can define two types of variables in a
class (called class variables)
static class variables
nonstatic variables are called instance variables (fields)
because each instance (object) of the class has its own copy
class variables can be accessed in all methods of the class
Method Declarations
A class can define many types of methods:
Access methods : read or display data
Predicate methods : test the truth of conditions
Constructors
initialize objects of the class
they have the same name as the class
There may be more than one constructor per class (overloaded
constructors)
can take arguments
If a class has no constructor, a default constructor is provided
It has no code and takes no parameters
they do not return any value
it has no return type, not even void
ي;;ک کالس ميتوان;;د س;;ازندههاي مختلف;;ي داشت;;ه باشد.
س;;ادهترين آنه;;ا ،س;;ازندهاي اس;;ت ک;;ه هي;;چ پارامتري
ندارد .ب;ه اي;ن س;ازنده سازندۀ پيشفرض (Default
)Constructorميگويند.
اگر در يک کالس ،سازندۀ پيشفرض تعريف
نشود ،کامپايلر به طور خودکار آن را براي
کالس مذکور ايجاد ميکند.
Example: Time1 class
We define the Time1 class to represent time.
The state of a time object can be
represented by:
hour, minute, second : integers representing time
We might define the following methods:
a Time1 constructor, to set up the object
a SetTime method, to set time
a ToUniversalString method, to convert the internal representation to a
string representing the time in 24 hour format
a ToStandardString method, to convert the internal representation to a
string representing the time in 12 hour format
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Time1.cs
// Class Time1 maintains time in 24-hour format.
using System;
// Time1 class definition
public class Time1
{
private int hour;
// 0-23
private int minute; // 0-59
private int second; // 0-59
// Time1 constructor initializes instance variables to
// zero to set default time to midnight
public Time1()
سازنده پيش فرض
{
)Default Constructor(
SetTime( 0, 0, 0 );
}
// Set new time value in 24-hour format. Perform validity
// checks on the data. Set invalid values to zero.
public void SetTime( int hourValue, int minuteValue, int secondValue
{
hour
= ( hourValue
>= 0 && hourValue
< 24 ) ? hourValue
:
minute = ( minuteValue >= 0 && minuteValue < 60 ) ? minuteValue :
second = ( secondValue >= 0 && secondValue < 60 ) ? secondValue :
)
0;
0;
0;
28
29
30
31
32
33
33
35
36
37
38
39
40
41
42
43
// convert time to universal-time (24 hour) format string
public string ToUniversalString()
{
return String.Format(
"{0:D2}:{1:D2}:{2:D2}", hour, minute, second );
}
// convert time to standard-time (12 hour) format string
public string ToStandardString()
{
return String.Format( "{0}:{1:D2}:{2:D2} {3}",
( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ),
minute, second, ( hour < 12 ? "AM" : "PM" ) );
}
} // end class Time1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// TimeTest1.cs
// Demonstrating class Time1.
using System;
using System.Windows.Forms;
// TimeTest1 uses creates and uses a Time1 object
class TimeTest1
براي ايجاد شي از عملگر
{
. استفاده ميشودnew
// main entry point for application
static void Main( )
{
با ايجاد شي تابع سازنده
Time1 timeObj1 = new Time1(); // calls Time1.شود
constructor
فراخواني مي
string output;
// assign string representation of time to output
output = "Initial universal time is: " +
timeObj1.ToUniversalString() +
"\nInitial standard time is: " +
timeObj1.ToStandardString();
// attempt valid time settings
timeObj1.SetTime( 13, 27, 6 );
// append new string representations of time to output
output += "\n\nUniversal time after SetTime is: " +
timeObj1.ToUniversalString() +
"\nStandard time after SetTime is: " +
34
35
36
37
38
39
40
41
42
output += "\n\nAfter attempting invalid settings: " +
"\nUniversal time: " + time.ToUniversalString() +
"\nStandard time: " + time.ToStandardString();
MessageBox.Show( output, "Testing Class Time1" );
} // end method Main
} // end class TimeTest1
Class View and Object
Browser
Class View and Object Browser are
features of Visual Studio that
facilitate the design of objectoriented applications
Class View
Displays variables and methods for all classes in a project
Displays as treeview hierarchical structure
+ at nodes allows nodes to be expanded
- at nodes allows nodes to be collapsed
Can be seen by selecting View < Class View
Class View: Example
public class Time2
{
private int hour;
private int minute;
private int second;
// 0-23
// 0-59
// 0-59
public Time2()
{
SetTime( 0, 0, 0 );
}
public Time2( int hour )
{
SetTime( hour, 0, 0 );
}
public Time2( int hour, int minute )
{
SetTime( hour, minute, 0 );
}
public Time2( int hour, int minute, int second )
{
SetTime( hour, minute, second );
}
public Time2( Time2 time )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// TimeTest2.cs
// Using overloaded constructors.
using System;
using System.Windows.Forms;
// TimeTest2 demonstrates constructors of class Time2
class TimeTest2
{
// main entry point for application
static void Main( )
{
Time2 time1, time2, time3, time4, time5, time6;
time1
time2
time3
time4
time5
time6
=
=
=
=
=
=
new
new
new
new
new
new
Time2();
Time2( 2 );
Time2( 21, 34 );
Time2( 12, 25, 42 );
Time2( 27, 74, 99 );
Time2( time4 );
//
//
//
//
//
//
00:00:00
02:00:00
21:34:00
12:25:42
00:00:00
12:25:42
Encapsulation: Two Views of an
Object
You can take one of two views of
an object:
internal - the structure of its data, the algorithms used
by its methods
external - the interaction of the object with other part
of the world
Encapsulation: An Object As a Black Box
From the external view, an object is an
encapsulated entity, providing a set of
specific services
These services define the interface to the object
An encapsulated object can be thought of as a black box
The user, or client, of an object can request its services, but it should not have
to be aware of how those services are accomplished
Client
Methods
Data
Accomplish Encapsulation: Access
Modifiers
In C#, we accomplish encapsulation
through the appropriate use of access
modifiers
An access modifier is a C# keyword
that specifies the accessibility of a
method, data field, or class
We will discuss two access modifiers: public, private
We will discuss the other two modifiers (protected, internal)
later
The public and private Access Modifiers
Classes (types) and members of a class
that are declared with public can be
accessed from anywhere
Members of a type that are declared
with private can only be accessed from
inside the class
Members of a class declared without an
access modifier have default private
accessibility
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// RestrictedAccess.cs
// Demonstrate compiler errors from attempt to access
// private class members.
class RestrictedAccess
{
// main entry point for application
static void Main( string[] args )
{
Time1 time = new Time1();
time.hour = 7;
time.minute = 15;
time.second = 30;
}
} // end class RestrictedAccess
پيادهسازي بستهبندي
به عنوان يك قانون كلي ،هيچ عضو دادهاي به صورت public
تعريف نميشود.
از خصوصيات ( )properiesبراي دسترسي به دادهها استفاده
ميكنيم.
اعضاي دادهاي به صورت privateو خصوصيات آنها به صورت
publicتعريف ميشوند.
خصوصيات دو methodدارند :
: get دسترسيبJJJراJيخJواJندناJعضايدادهJاJي
: set دسترسيبJJJراJينJJوشتد اJعضايدادهJاJي
Time2 پيادهسازي سه خصوصيت براي كالس
// property Hour
public int Hour
{
get { return hour; }
set { hour = ( ( value >= 0 && value < 24 ) ? value : 0 ); }
} // end property Hour
// property Minute
public int Minute
{
get { return minute; }
set { minute = ( ( value >= 0 && value < 60 ) ? value : 0 ); }
} // end property Minute
// property Second
public int Second
{
get { return second; }
set { second = ( ( value >= 0 && value < 60 ) ? value : 0 ); }
} // end property Second
Time2 time
time.Hour
time.Minute
time.Second
...
=
=
=
=
new Time2();
15;
45;
20;