Advance Python OOPS |
This is a Advance python opps class. If you don't know about basic of python oops please first read basic python oops. Mainly three types of oops class single inheritance, multiple inheritance and multi level inheritance. OOPS concepts mainly design for code easy to read. it is also known as code readable.
#codebugfree.com
class Employee():
company = "visa"
sam = Employee()
class freelancer():
def action(self):
print("the value of action is sustanable")
return
company = "fiver"
level = 2
rahul = freelancer()
class programmer(freelancer,Employee):
name = "samir"
samir = programmer()
print(samir.level)
print(samir.company)
print(rahul.company)
samir.action()
This is a multiple inheritance. Multiple inheritance is very confuse inheritance. It is not make our program readable. Multiple inheritance is not supported other programming language like java. This is the main reason and main problem to programmer that does not use multiple inheritance. All the programmer is recommand multiple inheritance does not use your programmer . Avoid multiple inheritance.
first we define class Employee(). Class variable is company = visa object is sam = Employee(). Second class is freelancer() and define def fuction using self. Print the value of action is sustanable and return None. Class variable is company = fiver his level = 2 object is rahul = freelancer. At last class is main class class is programmer in programmer class combine other two clas freelancer and Employee. Class variable is name = samir and object is samir = programmer.
IN OUTPUTÂ print(samir.level) samir is programmer class object and programmer class have no level attribute or variable. Programmer class now go combined class freelancer and search there variable level. Find level and print 2 samir.level = 2. Print samir.company search programmer class then search comied class firsr freelancer and print samir.company = fiver. rahul.company is also fiver last samir.action () is called a fuction the function of value is sustanable
Multilevel Inheritance
class Person():
name = "samir"
country = "NEPAL"
def takebreath(self):
print("i am a co2")
class Employee(Person):
company = "google"
def takebreath(self):
print("i am employee 02")
super().takebreath()
class Programmer(Employee):
company = "gun"
language = "python"
def takebreath(self):
print("i am agood programmer. i take breath is c03")
super().takebreath()
samir = Employee()
print(samir.company)
samir = Person()
samir = Programmer()
samir.takebreath()
print(samir.name)
Multilevel Inheritance is very comfortable to read and write a program. It is support a all programming language. It has more than one class and all class combined with in other class is known as multilevel inheritance class.Â
We define person class. IT is a main class and not combine any other class in person class. Class variable is name = samir and country = nepal. define def fuction using takebreath print i am a co2. We define class Employee and combined Person class. class variable is company = google. Define the def fuction with takebreath using self and printi am employee 02. NOTE that super().variable is method that call super class variable means first class or main class. Self is means myself run program same for all object. Here main class is class Employee. another class is Programmer and combined Employee. Variable of Programmer class is Company = gun and language is python. Define def fuction with super takebreath using self. Print i am a good programmer and take breath is co3. and using supertakebreath. INOUTPUT it is your problem solve it. COPy this program in your temimal> describe the output in comment.
class Employee():
company = "nepal technology"
owner = "samir"
#codebugfree.com
employer = "rahul"
employersalary = 5000
bonussalary = 500
@property#getter method also
def totalsalary(self):
return self.employersalary + self.bonussalary
@totalsalary.setter
def totalsalary(self,val):
self.bonussalary = val - self.employersalary
rahul = Employee()
print(rahul.employersalary)
print(rahul.bonussalary)
rahul.totalsalary = 10000
print(rahul.totalsalary)
print(rahul.employersalary)#
print(rahul.bonussalary)
Getter and Setter method in python oops concept. Employee class have company and owner variable nepal Technology and Samir employer is rahul employer salary and bonus salary is 5000, 500. Now here using property decorators.Â
It is also known as setter method. to find total salary return employer salary + bonus salary. it is employer property getes from company. If any reason or country policy decrease his salary for vat. We cannot decrease his salary. That case we use setter method @totalsalary bonus salary = val - employer salary. INOUTPUTÂ
5000
500
10000
5000
5000
The diamond shape problem
It is amost common problem. It is a multiple inheritance problem. It is a confusion type problem.SUch class A is main class and not combine other class , class BÂ is combined with A , class C is combined with A at last class D is combined with A. it is claled a Diamond shape problem
Practice problemÂ
In this program there is some error. You have find some error and write new program.       Write the progam of vector that inculed i j and k with variable.Â
# IN THIS PROGRAM HERE IS A ERROR IT IS YOUR QUESTIONS TO FIND EROR AND SOLVE THIS PROGRAM.
# CODEBUGFREE.COM
class C2dvector():
def __intit__(self,i,j):
self.icap = i
self.jcap = j
def __str__(self):
return print (f"{self.icap}i + {self.jcap}j")
class C3dvector(C2dvector):
def __init__(self,i,j,k):
super().__intit__(i,j)
self.kcap = k
def __str__(self):
return print(f"{self.icap}i + {self.jcap}j + {self.kcap}k")
v2d = C2dvector(1,2)
v3d = C3dvector(1,3,5)
print(v2d)
print(v3d)
NOte:Â Write this solution in comment box.Â
0 Comments
Don't comment a spam message