Uploaded by Sheik Mohideen

Example of OOPs on Python

advertisement
Example of OOPs on Python
Class and Object
Program 1:
class Computer:
def config(self):
print("i7,16gb, 1TB")
a=10;
print(type(a))
com1 = Computer()
print(type(com1))
program 2:
class Computer:
a=10
def config(self):
print("i7,16gb, 1TB")
#config()
#Computer.config()
com1=Computer()
com2=Computer()
com1.config()
com2.config()
print(com1.a)
#Computer.config(com1)
program 3:
class Computer:
compaddr = 123
def __init__(self,cpu,Ram):
self.cpu = cpu
self.Ram = Ram
print("constructor")
def config(self):
print("Config is",self.cpu, self.Ram)
com1=Computer('i5',16)
com2=Computer('i7',8)
com1.config()
com2.config()
#obj address
print(id(com1))
program 4:
class Human:
def __init__(self):
self.name = "sheik"
self.age = 25
def update(self):
self.age = 30
def compare(self, h2):
if self.age == h2.age:
return True
else:
return False
h1 = Human()
h2 = Human()
print(h1.name)
print(h2.name)
# if you change object name attribute
h1.name = "gces"
print(h1.name)
# update attribute
h1 = Human()
print(h1.name)
print(h1.age)
h1.update()
print(h1.name)
print(h1.age)
# comparing
if h1.compare(h2):
print("Same")
else:
print("different")
program 5:
Object Taxi:
class Taxi:
def __init__(self, dname, onduty, cities):
self.dname = dname
self.onduty = onduty
self.cities = cities
self.numpassenger = 0
def changedrivername(self, newdrivername):
self.dname = newdrivername
def passenger(self, numofpassenger):
self.numpassenger += numofpassenger
t1 = Taxi("john", True, ['try', 'chennai'])
print(t1.dname)
print(t1.onduty)
print(t1.cities)
print(t1.numpassenger)
t1.changedrivername("sheik")
print(t1.dname)
print(t1.onduty)
print(t1.cities)
print(t1.numpassenger)
t1.passenger(4)
print(t1.dname)
print(t1.onduty)
print(t1.cities)
print(t1.numpassenger)
Type of variable:
#Type of Variable
#instance variable
#class(static) variable
class Car:
#static class variable
wheels = 4
def __init__(self):
#instance variable
self.company = "BMW"
self.mil = 10
c1 = Car()
c2 = Car()
Car.wheels= 5
c1.mil = 6
print(c1.company,c1.mil,c1.wheels)
print(c2.company,c2.mil,c2.wheels)
Types of methods:
#Type of Variable
#instance variable
#class(static) variable
class Car:
#static class variable
wheels = 4
def __init__(self):
#instance variable
self.company = "BMW"
self.mil = 10
c1 = Car()
c2 = Car()
Car.wheels= 5
c1.mil = 6
print(c1.company,c1.mil,c1.wheels)
print(c2.company,c2.mil,c2.wheels)
#Types of methods
#Instance , Class, Static
class Students:
college = "gces"
def __init__(self,m1,m2,m3):
self.m1 = m1
self.m2 = m2
self.m3 = m3
def avg(self):
return (self.m1 + self.m2 + self.m3)/3
@classmethod
def collegename(cls):
return cls.college
@staticmethod
def info():
print("this is static")
s1 = Students(99,99,99)
s2 = Students(90,90,90)
print(s1.avg())
print(s2.avg())
print(s2.collegename())
s1.info()
s2.info()
Students.info()
#inner class
class Stud:
def __init__(self,name,rollno):
self.name = name
self.rollno = rollno
self.dpt = self.Dept
class Dept:
def __init__(self,dept):
self.dept= dept
stud1 = Stud("Sheik",1234)
dpt1= stud1.Dept("CSE")
print(stud1.name,stud1.rollno,dpt1.dept)
#single inheritance
class viva:
def feature1(self):
print("feature 1
def feature2(self):
print("feature 2
class oppo(viva):
def feature3(self):
print("feature 3
def feature4(self):
print("feature 4
f1 = oppo()
f1.feature1()
working")
working")
working")
working")
#multilevel
class viva:
def feature1(self):
print("feature 1
def feature2(self):
print("feature 2
class oppo(viva):
def feature3(self):
print("feature 3
def feature4(self):
print("feature 4
class Iphone(oppo):
def feature5(self):
print("feature 5
f1 = Iphone()
f1.feature1()
working")
working")
working")
working")
working")
#multiple
class Viva:
def feature1(self):
print("feature 1
def feature2(self):
print("feature 2
class Oppo():
def feature3(self):
print("feature 3
def feature4(self):
print("feature 4
class Iphone(Oppo,Viva):
def feature5(self):
print("feature 5
f1 = Iphone()
f1.feature1()
f1.feature3()
working")
working")
working")
working")
working")
#constructor with
Super() inheritance
class Viva:
def __init__(self):
print("Welcome Viva Constructor Calling")
class Oppo(Viva):
def __init__(self):
super().__init__()
print("Welcome oppo Constructor Calling")
f1 = Oppo()
#Method Resolution Order
class Viva:
def feature4(self):
print("feature 1 working")
class Oppo():
def feature1(self):
print("feature 1 B working")
class Iphone(Viva,Oppo):
def __init__(self):
super().feature1()
print("Welcome Iphone Constructor Calling")
f1 = Iphone()
print(Iphone.mro())
#Polymorphism
Need module 're' for regular
expression duck typing
import re
search_string = "wrwr"
pattern = "Tutorials"
match = re.match(pattern, search_string)
#If-statement after search() tests if it succeeded
if match:
print("regex matches: ", match.group())
else:
print('pattern not found')
#operator overloading
class Student:
def __init__(self,m1,m2):
self.m1=m1
self.m2=m2
def __add__(self, other):
m1 = self.m1 + other.m1
m2 = self.m2 + other.m2
s3=Student(m1,m2)
return s3
s1 = Student(88, 14)
s2 = Student(99, 89)
s3 = s1 + s2
print(s3.m1)
#Method Overloading
class Student:
def __init__(self,m1,m2):
self.m1=m1
self.m2=m2
def sum(self,a=None, b=None, c=None):
s=0
if a!=None and b!=None and c!=None:
s=a+b+c
elif a!=None and b!=None:
s=a+b
else:
s=a
return s
s1 = Student(66,66)
print(s1.sum(34,45,100))
#Method Overriding
class students:
pass
class emp:
def show(self):
print(("e-Name"))
class owner(students,emp):
pass
own1=owner()
own1.show()
#Abstraction
from abc import ABC, abstractmethod
class Payment(ABC):
def print_slip(self, amount):
print('Purchase of amount- ', amount)
@abstractmethod
def payment(self, amount):
pass
class CreditCardPayment(Payment):
def payment(self, amount):
print('Credit card payment of- ', amount)
class MobileWalletPayment(Payment):
def payment(self, amount):
print('Mobile wallet payment of- ', amount)
obj = CreditCardPayment()
obj.payment(100)
obj.print_slip(100)
print(isinstance(obj, Payment))
obj = MobileWalletPayment()
obj.payment(200)
obj.print_slip(200)
print(isinstance(obj, Payment))
#Encapsulation
class Base:
def __init__(self):
# Protected member
self._a = 2
class Derived(Base):
def __init__(self):
Base.__init__(self)
print("Calling protected member of base class: ")
print(self._a)
obj1 = Derived()
obj2 = Base()
#print(obj2.a)
#Private
class Car:
def __init__(self):
self.brand_name = 'Audi '
self.model = 'r8'
def __engine(self):
return '5.2 L V10'
def get_description(self):
return self.brand_name + self.model + " is the car"
c = Car()
print(c.get_description())
#print(c.__engine())
#private access
class Car:
def __init__(self):
self.brand_name = 'Audi '
self.model = 'r8'
self.__engine_name = '5.2 L V10'
def __engine(self):
return '5.2 L V10'
def get_description(self):
return self.brand_name + self.model + " is the car"
c = Car()
print(c.get_description())
print("Accessing Private Method: ", c.__engine())
print("Accessing Private variable: ", c.__engine_name)
Download