0% found this document useful (0 votes)
2 views

Python OOP (2) (1)

Uploaded by

olibax0000
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python OOP (2) (1)

Uploaded by

olibax0000
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python OOP

OOP nima?

Ob'ektga yo'naltirilgan dasturlash yoki


qisqacha OOP, dasturlarning paradigmasi
bo'lib, u xususiyatlari va xulq-atvorlari alohida
ob'ektlarga to'planishi uchun dasturlarni tuzish
vositasi hisoblanadi.
Example

Person - name, age, address etc.,


xususiyatlari bilan yoki xulq-atvor bilan
esa yurish, yugurish, gapirish va nafas
olish.
Email – xususiyatlari subject, recipient
list, body etc., va fe’l-atvorlari adding
attachments va sending
Python Classes

Python dagi deyarli barcha narsa ob’ekt


hisoblanadi.
Python da mavjud bo'lgan sodda ma'lumotlar
strukturalari, numbers, strings va lists kabi
narsalar oddiy narsalarni, masalan, she'rning
nomi va sevimli ranglar kabi oddiy narsalarni
ifodalash uchun mo'ljallangan.
Agar biror narsani murakkabroq ifodalashni
xohlasangiz nima bo'lar edi?
Classes yangi foydalanuvchi tomonidan
tuzilgan ma'lumotlar tuzilmalarini yaratish
uchun foydalaniladigan mexanizmdir.
Ma'lumotlar bilan bir qatorda, ushbu
ma'lumotlarni qayta ishlash uchun
ishlatiladigan usullar ham mavjud.

class Dog:
pass
Instance Attributes

class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
Class Attributes

Instance attributes har bir ob'ektga xos bo'lsa-da,


Class attributes barcha holatlar uchun bir xil - bu
holda barcha itlar uchun.
class Dog:
# Class Attribute
species = 'mammal'
#Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
class Dog:
# Class Attribute
species = 'mammal'
# Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
# Instantiate the Dog object
reks = Dog(“Reks", 5)
hatiko = Dog(“Hatiko", 6)
# Access the instance attributes
print("{} is {} and {} is {}.".format( reks.name, reks.age, hatiko.name,
hatiko.age))
#Is Reks a mammal?
if reks.species == "mammal":
print("{0} is a {1}!".format(reks.name, reks.species))
Instance Methods

# instance method
def description(self):
return "{} is {} years old".format(self.name,
self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name,
sound)
Python Object Inheritance

Inheritance - bu bir classning boshqa xususiyat


va usullarini oladigan jarayon hisoblanadi.
Yangi tashkil etilgan classlar child classes
deb nomlanadi va child classes olingan
classlarga parent classes deb nomlanadi.
# Child class (inherits from Dog class)
class Shepherd (Dog):
def run(self, speed):
return "{} runs {}".format(self.name, speed)
# Child class (inherits from Dog class)
class Bulldog(Dog):
def run(self, speed):
return "{} runs {}".format(self.name,
speed)
# Child classes inherit attributes and
# behaviors from the parent class
jim = Bulldog("Jim", 12)
print(jim.description())
# Child classes have specific attributes
# and behaviors as well
Overriding the Functionality of a Parent Class
>>> class Dog:
... species = 'mammal'
...
>>> class SomeBreed(Dog):
... pass
...
>>> class SomeOtherBreed(Dog):
... species = 'reptile'
...
>>> frank = SomeBreed()
>>> frank.species
'mammal'
>>> beans = SomeOtherBreed()
>>> beans.species
'reptile'

You might also like