python - intermediate - 7 - OOP
Class | instance / obje | metot |
---|---|---|
Liste | [1,2,3,4] | listA.append |
Person | name, surname, birthday |
Cok veri oldugunda class kullanmak daha avantajli oluyor
Aciklamalar
class
Person: # buyuk harf ile baslamali
#bos olmamali, bos birakacaksak icine `pass`
yazmaliyiz
# ya attributes ya da methods kisminda birsey olmali
#attributes
#constructor icinde tanimlanir
#constructor tanimlamak icin metot tanimlamasi yapilir
# attributes 2 cesit:
#1 / class attr
#2 / object attr
#bunu def ile baslayarak yapiyoruz:
def __init__(self, name, year): #self: parametre =
#self class`tan turetilmis herhangi bir objeyi temsil eder
#self ya p1 ya da p2`yi temsil eder
#obje userinden bir deger aktarmak istedigimizde self`i kullaniriz
# self`in uzerine hangi attribute`lari eklemek istiyorsam,
#self`ten sonra vitguller ile parametre olarak bunu yapiyorum
#methods
#object, instance
p1 = Person() # kucuk harfle baslar - Person Class`indaki metot ve attrib. lara ulasabilirim
#p1`in adresi var artik, her objenin adresi farklidir
p2 = person()
Nasil yapilir
class Person:
#attributes
#constructor (yapici metot)
def __init__(self, name, year):
# parantez icindeki parametreler hakkinda: 3 parametre var
# self icin parametre gonderilmez p1 ve p2 onu parametresidir,
# yani self icin deger verilmez
# diger ikisi: name ve year icin parametre gonderilmeli
# program run ettiginde, init metodu her obje (p1,p2) icin cagrilir
# attributes 2 cesittir:
#1 / class attr
#2 / object attr
self.name = name
self.year = year
print('init metodu calisti')
#methods
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
#2 adet parametre gonderdik
#
init metodu calisti #p1 icin calisti
init metodu calisti #p2 icin calisti
deger yazdirmak
class Person:
#attributes
#constructor (yapici metot)
def __init__(self, name, year):
# attributes 2 cesit:
#1 / class attr
#2 / object attr
self.name = name
self.year = year
print('init metodu calisti')
#methods
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
print(f'name: {p1.name} - year: {p1.year}')
print(f'name: {p2.name} - year: {p2.year}')
#
init metodu calisti
init metodu calisti
name: ali - year: 1990
name: yagmur - year: 1995
Class attributes`lar
class Person:
#attributes
#1 / class attr - class icinde genel
address='no information'
# bilgiyi girmek yerine cl att olarak tanimladik
#constructor (yapici metot)
def __init__(self, name, year):
#2 / object attr - objelere ozeldir
self.name = name
self.year = year
print('init metodu calisti')
#methods
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
# accessing object attributes
print(f'name: {p1.name} - year: {p1.year} - adress: {p1.address}')
print(f'name: {p2.name} - year: {p2.year} - adress: {p2.address}')
#
name: ali - year: 1990 - adress: no information
name: yagmur - year: 1995 - adress: no information
Attribute update`ini yapmak
class Person:
#attributes
#1 / class attr
address='no information'
#constructor (yapici metot)
def __init__(self, name, year):
#2 / object attr
self.name = name
self.year = year
print('init metodu calisti')
#methods
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
#updating
p1.name= 'ahmeet'
p1.address='kocaeli'
#access obj attributes
print(f'name: {p1.name} - year: {p1.year} - adress: {p1.address}')
print(f'name: {p2.name} - year: {p2.year} - adress: {p2.address}')
#
name: ahmeet - year: 1990 - adress: kocaeli
name: yagmur - year: 1995 - adress: no information
Metod tanimlamak
class Person:
#attributes
#1 / class attr
address='no information'
#constructor (yapici metot)
def __init__(self, name, year):
#2 / object attr
self.name = name
self.year = year
#instance methods
def intro(self): #self cunku instance metodu bu
print('Hello There. I am '+self.name)
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
#access obj attributes
print(f'name: {p1.name} - year: {p1.year} - adress: {p1.address}')
print(f'name: {p2.name} - year: {p2.year} - adress: {p2.address}')
p1.intro()
p2.intro()
#
name: ali - year: 1990 - adress: no information
name: yagmur - year: 1995 - adress: no information
Hello There. I am ali
Hello There. I am yagmur
metod - ornek 2 -
class Person:
#attributes
#1 / class attr
address='no information'
# bilgiyi girmek yerine cl att olarak tanimladik
#constructor (yapici metot)
def __init__(self, name, year):
#2 / object attr
self.name = name
self.year = year
#instance methods
def intro(self): #self cunku instance metodu bu
print('Hello There. I am '+self.name)
def calculateAge(self): #her seferinde self parametresini almasi lazim
return 2019-self.year
#object, instance
p1 = Person('ali', 1990)
p2 = Person('yagmur', 1995)
#access obj attributes
print(f'name: {p1.name} - year: {p1.year} - adress: {p1.address}')
print(f'name: {p2.name} - year: {p2.year} - adress: {p2.address}')
p1.intro()
print(f'yasim: {p1.calculateAge()}')
p2.intro()
print(f'yasim: {p2.calculateAge()}')
#
Hello There. I am ali
yasim: 29
Hello There. I am yagmur
yasim: 24
- yeni ornek -
class CircleIsMe:
#att
pi=3.14
def __init__(self,CirYaricap=1): #default 1
self.xCirYaricap = CirYaricap
# Methods
def cevre_hsp(self):
return 2*self.pi*self.xCirYaricap
def alan_hsp(self):
return self.pi*(self.xCirYaricap**2)
c1 = CircleIsMe() #default 1 olacak - deger yok
c2 = CircleIsMe(5)
print(f'c1: alan = {c1.alan_hsp()} cevre = {c1.cevre_hsp()}')
print(f'c2: alan = {c2.alan_hsp()} cevre = {c2.cevre_hsp()}')
#
c1: alan = 3.14 cevre = 6.28
c2: alan = 78.5 cevre = 31.400000000000002
Inheritence
Person class`indaki bilgiler (name, lastname, age), temel class olacak ve,
Student class`inda kullanilmasi: Student(Person)
veya Teacher class`inda kullanilmasi: Teacher(Person)
class Person():
def __init__(self):
print('Person created')
class Student(Person):
def __init__(self):
Person.__init__(self) # Student`i Person`a baglar
print('Student created')
p1=Person()
s1=Student()
#
Person created
Person created (Student create ettigimiz ve People`a bagliligindan bu ikinci kere yazar)
Student created
- uygulama -
class Person():
def __init__(self, fname, lname):
self.firstName = fname
self.lastName = lname
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
p1=Person('Ali','Yilmaz')
s1=Student('Cinar','Turan')
print(p1.firstName+" "+p1.lastName)
print(s1.firstName+" "+s1.lastName)
#
Ali Yilmaz
Cinar Turan
-uygulama-
Student`da daha detay
class Person():
def __init__(self, fname, lname):
self.firstName = fname
self.lastName = lname
#print('Person created')
class Student(Person):
def __init__(self, fname, lname, StNumber):
Person.__init__(self, fname, lname)
self.StudentNumber = StNumber
#print('Student created')
p1=Person('Ali','Yilmaz')
s1=Student('Cinar','Turan',1256)
print(p1.firstName+" "+p1.lastName)
print(f'{s1.firstName} {s1.lastName} {s1.StudentNumber}')
#
Ali Yilmaz
Cinar Turan 1256
- child`in inherit etmesi:
class Person():
def __init__(self, fname, lname):
self.firstName = fname
self.lastName = lname
#print('Person created')
def who_am_i(self):
print('I am a person')
class Student(Person):
def __init__(self, fname, lname, StNumber):
Person.__init__(self, fname, lname)
self.StudentNumber = StNumber
p1=Person('Ali','Yilmaz')
s1=Student('Cinar','Turan',1256)
print(p1.firstName+" "+p1.lastName)
print(f'{s1.firstName} {s1.lastName} {s1.StudentNumber}')
p1.who_am_i()
s1.who_am_i()
# not edilecek husus, Person elemaninda var ama Student
# elemaninda olmasa da student uzerinden cagirabiliyoruz
# override edilebilir - sonraki ornek gosteriyor
#
Ali Yilmaz
Cinar Turan 1256
I am a person
I am a person # Studen inherit ettigi icin bunu yaziyor
- child override etmesi
class Person():
def __init__(self, fname, lname):
self.firstName = fname
self.lastName = lname
def who_am_i(self):
print('I am a person')
class Student(Person):
def __init__(self, fname, lname, StNumber):
Person.__init__(self, fname, lname)
self.StudentNumber = StNumber
def who_am_i(self):
print('I am a student')
p1=Person('Ali','Yilmaz')
s1=Student('Cinar','Turan',1256)
print(p1.firstName+" "+p1.lastName)
print(f'{s1.firstName} {s1.lastName} {s1.StudentNumber}')
p1.who_am_i()
s1.who_am_i()
#
Ali Yilmaz
Cinar Turan 1256
I am a person
I am a student # Override etti
son uygulama
class Person():
def __init__(self, fname, lname):
self.firstName = fname
self.lastName = lname
#print('Person created')
def who_am_i(self):
print('I am a person')
class Student(Person):
def __init__(self, fname, lname, StNumber):
Person.__init__(self, fname, lname)
self.StudentNumber = StNumber
def who_am_i(self):
print('I am a Student')
class Teacher(Person):
def __init__(self,fname,lname,branch):
super().__init__(fname, lname) #bu "Person.__init__(self," ile ayni sonucu verir
self.ThBranch=branch
def who_am_i(self):
print(f'I am a {self.ThBranch} teacher')
p1=Person('Ali','Yilmaz')
s1=Student('Cinar','Turan',1256)
t1=Teacher('Serkan','Yilmaz',"Math")
t1.who_am_i()
print(p1.firstName+" "+p1.lastName)
print(f'{s1.firstName} {s1.lastName} {s1.StudentNumber}')
p1.who_am_i()
s1.who_am_i()
# not edilecek husus, Person elemaninda var ama Student
# elemaninda olmasa da student uzerinden cagirabiliyoruz
#
I am a Math teacher
Ali Yilmaz
Cinar Turan 1256
I am a person
I am a Student
- quizz app -
#question
class Question:
def __init__(self,text,choices,answer):
self.Qtext = text
self.Qchoices=choices
self.Qanswer=answer
def checkanswer(self,answer):
return self.Qanswer==answer
#quizz
class Quizz:
def __init__(self,questions):
self.Qquestions=questions
self.score=0
self.questionIndex=0
def getQuestion(self):
return self.Qquestions[self.questionIndex]
def displayQuestion(self):
Question = self.getQuestion()
print(f'Soru {self.questionIndex + 1}: {Question.Qtext}')
for q in Question.Qchoices:
print('-'+q)
answer=input('cevap: ')
# print(Question.checkanswer(answer))
self.guess(answer)
self.loadQuestion()
def guess(self,answer):
Question = self.getQuestion()
if Question.checkanswer(answer):
self.score+=1
self.questionIndex+=1
def loadQuestion(self):
if len(self.Qquestions)==self.questionIndex:
self.showScore()
else:
self.displayProgress()
self.displayQuestion()
def showScore(self):
print('score: ',self.score)
def displayProgress(self):
totalQuestion=len(self.Qquestions)
questionNumber=self.questionIndex+1
if questionNumber==totalQuestion:
print('Quiz bitti.')
else:
print(f'Question {questionNumber} of {totalQuestion}'.center(75,"-"))
q1=Question('best prog lang?',['c#','pyt','js','java'],'pyt')
q2=Question('en populer prog lang?',['c#','pyt','js','java'],'pyt')
q3=Question('en kazancli prog lang?',['c#','pyt','js','java'],'pyt')
q4=Question('en cok sevilen prog lang?',['c#','pyt','js','java'],'pyt')
q5=Question('en kolay prog lang?',['c#','pyt','js','java'],'pyt')
questions=[q1,q2,q3,q4,q5]
quiz = Quizz(questions)
# question=quiz.getQuestion()
# print(question.Qtext)
quiz.loadQuestion()
Comments
Post a Comment