Katika somo hili utakwend akujifunza maana ya object, na sheria za kuandika object.
Katika Object-Oriented Programming (OOP), object ni kielelezo (kitu) halisi (instance) cha class. Kila object ina state (hali), behavior (tabia), na identity (utambulisho).
State:
Inawakilishwa na attributes za object.
Inaonyesha property au tabia za object, kama vile jina, rangi, au umri.
Behavior:
Inawakilishwa na methods za object.
Inaonyesha jinsi object inavyoshughulikia data au kuingiliana na objects nyingine.
Identity:
Ni utambulisho wa kipekee wa object, unaomwezesha object mmoja kuingiliana na nyingine. Python hutumia id() function kutambua identity ya object. Id inaweza kuwa ni jina la hiyo object.
Fikiria class Dog:
class Dog:
def __init__(self, breed, age):
self.breed = breed # State (attribute)
self.age = age # State (attribute)
def bark(self):
print("Woof! Woof!") # Behavior (method)
Katika huo mfano hapo tunaona kuwa class ni dog, attributes za class ni breed na age, method ni bark. Kwa mfano halisi nikuwa mbwa ana sifa z akuwa na rangi na aina pia anakuwa na tabia ya kubweka.
Kuunda object:
Unapounda object maana yake unaunda kitu halisi kutoka kwenye hiyo class. Kwanza utataja id ya hiyo object. Ama jinala hiyo object, ikifatiwa na alama ya (=) kisha utaweka jina la class ikifatiwa na mabano, ndani yake utaweka value za attribute kwa mpangilio uleule wa kwenye class mfano my_dog = Dog("Labrador", 3) . Ili kuhususha object na method tutatumia dot mfan my_dog.bark()
my_dog = Dog("Labrador", 3)
State: breed ni "Labrador", na age ni 3.
Behavior: Unapoitisha my_dog.bark(), itachapisha: "Woof! Woof!"
Mfano kamili:
class Dog:
def __init__(self, breed, age):
self.breed = breed # State (attribute)
self.age = age # State (attribute)
def bark(self):
print("Woof! Woof!") # Behavior (method)
my_dog = Dog("Labrador", 3)
my_dog.bark()
Objects hutumika kufikia attributes na methods za class:
class Dog:
def __init__(self, breed, age):
self.breed = breed # State (attribute)
self.age = age # State (attribute)
def bark(self):
print("Woof! Woof!") # Behavior (method)
my_dog = Dog("Labrador", 3)
print(f"My dog is a {my_dog.breed} and is {my_dog.age} years old.") # Kufikia attributes
my_dog.bark() # Kufikia method
Hakuna kikomo juu ya class kuwa inaweza kubeba object ngapi. Ikumbukwe kuwa class ni blue print hvyo itatumia kutengeneza object bila kikomo. Yenyewe ndio inaweka sheria kuwa hizo object ziwe namna gani
Mfano:
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name}, and I am {self.age} years old."
student1 = Student('Musa', 14)
student2 = Student('Rehema', 12)
student3 = Student('Upendo', 14)
student4 = Student('Daudi', 10)
print(student1.introduce())
print(student2.introduce())
print(student4.introduce())
print(student3.introduce())
Object kwenye array
Tofauti na kutengeneza object kwa mtindo huo wa kutofautisha, unaweza kutumia array ili kurahisisha kazi. Kisha object ikapata id yake kutoka kwneye array index.
Mfano
students = [
Student('Musa', 14),
Student('Rehema', 12),
Student('Upendo', 14),
Student('Daudi', 10)
]
Kwa mfano kama huo kila member katika hiyo array anaweza kuitw akivyake. Nakuletea mifano miwili hapo chini ambayo inaweza kukusaidia kuelewa zaidi
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name}, and I am {self.age} years old."
# Creating Student objects
students = [
Student('Musa', 14),
Student('Rehema', 12),
Student('Upendo', 14),
Student('Daudi', 10)
]
# Using a loop to print introductions
for student in students:
print(student.introduce())
Mfano huo hapo juu nimetumia njia ya loop. Hata hivyo unaweza kutumia njia ya indexing kupata output kama hiyo.
Mfano:
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"My name is {self.name}, and I am {self.age} years old."
# Creating Student objects
students = [
Student('Musa', 14),
Student('Rehema', 12),
Student('Upendo', 14),
Student('Daudi', 10)
]
print(students[1].introduce())
Kila kitu katika Python ni object, ikijumuisha integers, strings, lists, na hata functions.
Kwa mfano:x = 10 # x ni object ya integer
print(type(x)) # <class 'int'>
Katika OOP, objects huchukua nafasi muhimu kwa sababu ndizo hufanikisha utekelezaji wa class. Zinaweza kubadilishwa, kufuatiliwa, na kufanikisha mwingiliano kati ya sehemu mbalimbali za programu.
Jifunze zaidi, na wasiliana nasi ukiw ana App yetu. Bofya liln hapo chini kuweza ku download
Download NowJe! umeipenda hii post?
Ndio Hapana Save post
Rajabu Tarehe 2024-12-03 10:07:35 Topic: Python Main: Masomo File: Download PDF Share On Facebook or Whatsapp Views 102
Sponsored links
👉1
Madrasa kiganjani
👉2
Sira ya Mtume Muhammad (s.a.w)
👉3
Simulizi za Hadithi Audio
👉4
Kitau cha Fiqh
👉5
Kitabu cha Afya
👉6
kitabu cha Simulizi
Python somo la 34: Kutumia html kwneye python
Katika somo hili utakwend akujifunz ajinsi ya kutumia html kwneye python Soma Zaidi...
Python somo la 15: Jinsi ya kutumia while loop
Katika somo hili tutakwend akujifunza jinsi ya kutumia while loop kwneye python Soma Zaidi...
Python somo la 18: Jinsi ya kuandika function
Katika somo hili utakwend akujifunza jinsi ya kuandika function, kuweka parameter na kuitumia function Soma Zaidi...
Python somo la 23: Library kwenye python
Hapa utajifunza maana ya library, kazi zake aina zake na jinsi ya ku install lirary Soma Zaidi...
Python somo la 19: Aina za Function
Katika somo hili utakwend akujifunza aina mbalimbali za function ambazo tunaweza kuzitumia kwenye python. Soma Zaidi...
Python somo la 30: Data abstraction
Katika somo hili utakwend akujifunz akuhusu nadharia ya data abstraction na kazi zake kwenye OOP Soma Zaidi...
PYTHON - somo la 8: method za namba zinazotumika kwenye python
Katika somo hili utakwenda kujifunza kuhusu number method yaani function zinazofanya kazi kwenye namba. Soma Zaidi...
Python somo la 32: Jinsi ya kusoma mafaili
Katika somo hili utakwenda kujifunz ajinsi ya kusoma mafaili na mitindo mbalimbali ya kusoma data Soma Zaidi...
Python somo la 16: Jinsi ya kutumia break na continue kwenye loop
Katika somo hili tunakwenda kujifunza matumizi ya break na continue kwenye loop Soma Zaidi...
Python somo la 35: Jinsi ya kutumia MYSQL kwenye python
Katika somo hili utajifunz akutumia database kwenye python Soma Zaidi...