Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet113/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   109   110   111   112   113   114   115   116   ...   453
lISTINg 5-1: 
 Defining a simple class
class MyGreetings:
# Greeting name variable.
Name = ‘Sammy’
# Provides a hello greeting.
def SayHello(self):
print ‘Hello there’, self.Name, ‘!’
# Provides a goodbye message.
def SayGoodbye(self):
print ‘See you later’, self.Name, ‘!’
The class description starts out with the word 
class
, as you might expect, and the name of the 
class. If you plan to inherit from another class, you enclose its name in parentheses behind the class 
name like this:
class MyGreetings(sys):
In this case, the 
MyGreetings
class would inherit from the 
sys
class. A class definition ends with a 
colon, just like every other structure in IronPython. You use indentation, as normal, to signify the 
end of the class.
Attributes are simply variables that you declare as part of the class. As you can see, you normally 
define a default value for attributes. An attribute can be of any type.
You might have noticed that 
Name
 doesn’t include anything other than the variable 
name and its value. IronPython doesn’t support the concept of data hiding — it 
isn’t possible to declare attributes and methods as private. More than a few people 
have discussed the topic online, but the general consensus is that IronPython 
knows what you’re doing and will let you shoot yourself in the foot if you really 
want to do so.
548592c05.indd 90
2/24/10 1:21:49 PM
www.finebook.ir


Creating New Objects
 

 
91
Methods are simply a different kind of function in IronPython. You define them within the class as 
you might expect. A method uses 
def
as the starting point, followed by the method name. Notice 
that both methods, 
SayHello()
and 
SayGoodbye()
, have an input variable named 
self
. When you 
create an instance of a class, the interpreter automatically creates the 
self
variable for that instance. 
The 
self
variable contains all of the data associated with that instance, such as 
Name
in this case. 
As you discover in the “Using Custom Objects in IronPython” section, you don’t actually assign any 
value to 
self
, the interpreter does it for you.
As shown in the method code, you can use any of the data that 
self
provides. In this case, the code 
accesses 
self.Name
to obtain the name you assigned to the 
Name
attribute (or the default, if you 
haven’t). If you were to try to access 
Name
directly, the interpreter would display an error message.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish