صفحه 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;