Uploaded by shrikant.palekar

AdvanceDayFirst

advertisement
OOPS - object oriented programming
a. attributes(characteristics) : methods(verb/behaviour)
student - name, school, class : study, play
car - brand,color : break, accerate,move
movies - length, name, cast : start, resume, stop
pen - brand, color : write, sketch
laptop - brand, processor : restart, shutdown, repair
b. class, object
class is a blueprint of how the object will be
student - rani, rishi
car - swift, safari
movies - terminator, xmen
pen - reynolds, cello
laptop - hp, dell
==================================================================
>>> x = 20
>>> type(x)
<class 'int'>
>>> y = 33.9
>>> type(y)
<class 'float'>
>>> z = "Python"
>>> type(z)
<class 'str'>
>>> a = True
>>> type(a)
<class 'bool'>
>>> ===================================================================
class student:#class declaration and definition
pass
rani = student()#object of the class student
rishi = student()
print(type(rani))
print(type(rishi))
print(id(rani))#location print(id(rishi))
=========================================================================
class student:#class declaration and definition
def details():#function inside the class - methods
print("St. john","Pune")
rani = student()
rishi = student()
#details()= name 'details' is not defined
#methods can be accessed using object
#rani.details() = details() takes 0 positional arguments but 1 was given
student.details()
=======================================================================
class student:#class declaration and definition
def details(self):#function inside the class - methods
#self - object of the student/class
print("St. john","Pune")
rani = student()
rishi = student()
#details()= name 'details' is not defined
#methods can be accessed using object
rani.details()
rishi.details()
#student.details()- TypeError: details() missing 1 required positional argument: 'self'
student.details(rishi)
============================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self):
print("One object created")
def details():#function inside the class - methods
#self - object of the student/class
print("St. john","Pune")
details()
rani = student()#object of the class student
print("===============")
rishi = student()
=============================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
def details():#function inside the class - methods
#self - object of the student/class
print("St. john","Pune")
rani = student("Rani","9C")#object of the class student
print("**************")
rishi = student("Rishi","8A")
=====================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
def details(self):#function inside the class - methods
#self - object of the student/class
self.location = "St. john"
print("Name is ",self.name)
print("Section is ",self.section)
print("Location is ",self.location)
rani = student("Rani","9C")#object of the class student
print("**************")
rishi = student("Rishi","8A")
print("#################")
rani.details()
print(rani.name)
print(rani.section)
print("***********")
rishi.details()
print(rishi.name)
print(rishi.section)
=================================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent,sectionLocation):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
self.location = sectionLocation
def details(self):#function inside the class - methods
#self - object of the student/class
#self.location = "St. john"
print("Name is ",self.name)
print("Section is ",self.section)
print("Location is ",self.location)
rani = student("Rani","9C","Chennai")#object of the class student
print("**************")
rishi = student("Rishi","8A","Mohali")
print("#################")
rani.details()
print(rani.name)
print(rani.section)
print("***********")
rishi.details()
print(rishi.name)
print(rishi.section)
================================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent,sectionLocation):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
self.location = sectionLocation
def details(self):#function inside the class - methods
#self - object of the student/class
self.location = "St. john"
print("Name is ",self.name)
print("Section is ",self.section)
print("Location is ",self.location)
rani = student("Rani","9C","Chennai")#object of the class student
print("**************")
rishi = student("Rishi","8A","Mohali")
print("#################")
print(rani.location)
rani.details()
print(rani.name)
print(rani.section)
print(rani.location)
print("***********")
print(rishi.location)
rishi.details()
print(rishi.name)
print(rishi.section)
print(rishi.location)
=============================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent,sectionLocation):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
self.location = sectionLocation
def details(self):#function inside the class - methods
#self - object of the student/class
self.location = "St. john"
print("Name is ",self.name)
print("Section is ",self.section)
print("Location is ",self.location)
rani = student("Rani","9C",None)#object of the class student
print("**************")
rishi = student("Rishi","8A","Mohali")
print("#################")
print(rani.location)
rani.details()
print(rani.name)
print(rani.section)
print(rani.location)
print("***********")
print(rishi.location)
rishi.details()
print(rishi.name)
print(rishi.section)
print(rishi.location)
==========================================================
#constructor - intialize the object during
class student:#class declaration and definition
def __init__(self,nameStudent, sectionStudent,marksScience,marksMaths):
print("One student created")
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
self.marksScience = marksScience
self.marksMaths = marksMaths
def details(self):#function inside the class - methods
#self - object of the student/class
self.marks = "St. john"
print("Name is ",self.name)
print("Section is ",self.section)
print("marks Science is ",self.marksScience)
print("marks Maths is ",self.marksMaths)
def compare(self,other):
t0 = self.marksScience + self.marksMaths
t2 = other.marksScience + other.marksMaths
if t0>t2:
print("Topper is ", self.name)
else:
print("Topper is ", other.name)
rani = student("Rani","9C",98,89)#object of the class student
rishi = student("Rishi","8A",55,96)
rahul = student("Rahul","7B",90,99)
rani.compare(rishi)
rishi.compare(rahul)
=======================================================================
#constructor - intialize the object during
class student:#class declaration and definition
schoolName = "St. john" #class variable
def __init__(self,nameStudent, sectionStudent):
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
def details(self):#function inside the class - methods
print("Name is ",self.name)
print("Section is ",self.section) rani = student("Rani","9C")#object of the class student
rishi = student("Rishi","8A")
rahul = student("Rahul","7B")
print(student.schoolName)
print(rani.schoolName)
print(rishi.schoolName)
print(rahul.schoolName)
print(id(student.schoolName))
print(id(rani.schoolName))
print(id(rishi.schoolName))
print("============================")
rani.schoolName = "KV"
print(student.schoolName)
print(rani.schoolName)
print(rishi.schoolName)
print(rahul.schoolName)
print(id(student.schoolName))
print(id(rani.schoolName))
print(id(rishi.schoolName))
print("***************************")
student.schoolName = "Govt school"
print(student.schoolName)
print(rani.schoolName)
print(rishi.schoolName)
print(rahul.schoolName)
print(id(student.schoolName))
print(id(rani.schoolName))
print(id(rishi.schoolName))
=================================================================
#constructor - intialize the object during
class student:#class declaration and definition
schoolName = "St. john" #class variable
def __init__(self,nameStudent, sectionStudent):
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
def details(self):#function inside the class - methods
print("Name is ",self.name)
print("Section is ",self.section) name = input("Enter your name: ")
section = input("Enter your section : ")
s2 = student(name, section)
s2.details()
======================================================
#constructor - intialize the object during
class student:#class declaration and definition
schoolName = "St. john" #class variable
def __init__(self,nameStudent, sectionStudent,laptop):
self.name = nameStudent #attributes
self.section = sectionStudent#attributes
self.laptop = laptop
print("student created")
def details(self):#function inside the class - methods
print("Name is ",self.name)
print("Section is ",self.section)
self.laptop.details()
class laptop:
def __init__(self,brand, processor,os):
self.brand = brand #attributes
self.processor = processor #attributes
self.os = os #attributes
print("laptop created")
def details(self):#function inside the class - methods
print("Brand is ",self.brand)
print("Processor is ",self.processor)
print("OS is ",self.os)
l2 = student.laptop("HP","intel","Windows")
s2 = student("Rani", "4C",l2)
l3 = student.laptop("Apple","Macbook","MacOS")
s3 = student("Rahul", "9C",l3)
s2.details()
s2.laptop.details()
==============================================================
#inheritance
class A:#parent class
def AA(self):
print("In AA")
def AB(self):
print("In AB")
class B(A):#child of A
def BB(self):
print("In BB")
a = A()
a.AA()
a.AB()
==============================================================
#inheritance
class A:#parent class
def AA(self):
print("In AA")
def AB(self):
print("In AB")
class B(A):#child of A
def BB(self):
print("In BB")
b = B()
b.AA()
b.AB()
b.BB()
a = A()
a.AA()
a.AB()
#a.BB() - error
==========================================================
#inheritance
class A:#parent class
def AA(self):
print("In AA")
def AB(self):
print("In AB")
class B(A):#child of A
def BB(self):
print("In BB")
class C(A):#child of A
def CC(self):
print("In CC") b = B()
b.AA()
b.AB()
b.BB()
print("---------")
c = C()
c.AA()
c.AB()
c.CC()
===========================================
#inheritance
class A:#parent class
def AA(self):
print("In AA")
def AB(self):
print("In AB")
class B(A):#child of A
def BB(self):
print("In BB")
class C(B):#child of B
def CC(self):
print("In CC") c = C()
c.BB()
c.AA()
c.AB()
c.CC()
==============================================
#inheritance
class A:
def AA(self):
print("In AA")
def AB(self):
print("In AB")
class B:
def BB(self):
print("In BB")
class C(A,B):
def CC(self):
print("In CC") c = C()
b = B()
c.AA()
c.AB()
c.BB()
c.CC()
============================================
#inheritance
class A:
def AA(self):
print("In AA")
def BB(self):
print("In A========BB")
class B:
def BB(self):
print("In B--------BB")
class C(B,A):
def CC(self):
print("In CC") c = C()
b = B()
c.BB()
================================================
#inheritance
class A:
def AA(self):
print("In AA")
def BB(self):
print("In A========BB")
class B:
def CC(self):
print("In B--------BB")
class C(B):
def CC(self):
super().CC()
print("In CC") c = C()
b = B()
c.CC()
=======================================================
#Access modifiers - public, protected, private
#_b = protected
#__c = private
class A:
a = "I am A" #public
_ba = "I am B protected in A"#protected
__bb = "I am B private in A"#private
def AA():
print("In AA")
print(__bb)
def BB(self):
print("In A========BB")
class B(A):
def CC(self):
print("In B--------BB")
a = A()
print(a.a)
print(a._ba)
A.AA()
#print(a.__bb)
print("=========B===========")
b = B()
print(b.a)
print(b._ba)
#print(b.__bb)
=================================================
Download