|
| 1 | +class student: |
| 2 | + def __init__(self,name,age): |
| 3 | + self.name = name |
| 4 | + self.age = age |
| 5 | + def info (self): |
| 6 | + print(f"Name : {self.name} \n Age : {self.age}") |
| 7 | + def studyInfo(self,level , grade): |
| 8 | + self.level = level |
| 9 | + self.grade = grade |
| 10 | + return f"Level of study is : {level} , \n Grades are : {grade}" |
| 11 | +college = student('Ali Haider' , 21) |
| 12 | +# print(college.info()) |
| 13 | +print(college.studyInfo('Bscs' , 'OKO')) |
| 14 | + |
| 15 | +# Encapsulation |
| 16 | +class bankBalance: |
| 17 | + def __init__(self, owner , balance): |
| 18 | + self.owner = owner |
| 19 | + self.__balance = balance # private can not be access directly it can be get only get by calling method |
| 20 | + def deposit(self , amount): |
| 21 | + if amount > 0: |
| 22 | + self.__balance += amount |
| 23 | + def get_balance(self): |
| 24 | + return f"{self.__balance} Your Total amount is " |
| 25 | +amount = bankBalance('Ali', 100) |
| 26 | +print(amount.deposit(1600)) |
| 27 | +# print(amount._balance) |
| 28 | +print(amount.get_balance()) |
| 29 | + |
| 30 | +# Inheritance |
| 31 | +class Animal : |
| 32 | + def __init__(self , name , bread): |
| 33 | + self.name = name |
| 34 | + self.bread = bread |
| 35 | + print('Constructor called') |
| 36 | + def crow(self , milk , color): |
| 37 | + print('crow speak') |
| 38 | + print( f"{milk} : {color}" ) # local variable k sath self use nahi hota self is only use for globelly constructor |
| 39 | + def got(self, milk , color): |
| 40 | + return f"{milk} in Kg : {color} in Color" |
| 41 | + |
| 42 | +class birds(Animal): |
| 43 | + def Birds_Name(self, name): |
| 44 | + return f"Name is {name}" |
| 45 | +# animal = Animal('bird', 'Green') |
| 46 | +# print(animal.crow(10, 'black')) |
| 47 | +# print(animal.got(39,'Green')) |
| 48 | +alll = birds('haider' , 'OKKKKKKKKKKKK') |
| 49 | +print(alll.got(40, 'yellow')) |
| 50 | + |
| 51 | +# pholimorphisim |
| 52 | +class White: |
| 53 | + def wallColor(self): |
| 54 | + return 'wall Color is Whte' |
| 55 | +class Black: |
| 56 | + def wallColor(self): |
| 57 | + return'Black color' |
| 58 | +for obj in (White(),Black()): |
| 59 | + print(obj.wallColor()) |
| 60 | + |
| 61 | + |
| 62 | +class CreditCardPayment: |
| 63 | + def pay(self, amount): |
| 64 | + print(f"Paid {amount} using Credit Card.") |
| 65 | + |
| 66 | +class PayPalPayment: |
| 67 | + def pay(self, amount): |
| 68 | + print(f"Paid {amount} using PayPal.") |
| 69 | + |
| 70 | +def payment_processing(payment_method , amount): |
| 71 | + payment_method.pay(amount) |
| 72 | +payment_processing(CreditCardPayment() , 100) |
| 73 | +payment_processing(PayPalPayment() , 2000) |
| 74 | + |
| 75 | +######################################################## |
| 76 | + |
| 77 | +class Car: |
| 78 | + total_car = 0 |
| 79 | + def __init__(self , brand , Model): # constructor |
| 80 | + self.__brand = brand # we make the brand as private now it can not be access by with class but can get access by method |
| 81 | + self.__Model = Model |
| 82 | + Car.total_car += 1 # to count how many a constructor is call or to find how may cars are created |
| 83 | + print('Automatically called a Constructor') |
| 84 | + def get_brand(self): # getter method to get private attribute atteribute can not be directly ass but we can get by it mothod |
| 85 | + |
| 86 | + return self.__brand + '!' |
| 87 | + def set_brand(self , new_brand): |
| 88 | + if new_brand > 0: |
| 89 | + __brand = new_brand |
| 90 | + else: |
| 91 | + print('Enter in String') |
| 92 | + def car_info(self): |
| 93 | + return f"Brand : {self.__brand} & Model : {self.__Model} bettery_size" |
| 94 | + def fule_type(self): # polymorphism |
| 95 | + return 'Petrol & Deisal' |
| 96 | + @staticmethod # this is use to make any method intoa staticmethod mean it can be access easily without wiring using self into method mena it can be easily access by using class withoud converting it into object when use staticmethod do not use self these are also called decorators |
| 97 | + def general_description(): |
| 98 | + return 'Static mrthod' |
| 99 | + @property # want to hide property can only be access by the method |
| 100 | + def model(self): |
| 101 | + return self.__Model |
| 102 | + |
| 103 | + |
| 104 | +# car = Car('Honda' , 'Civic') |
| 105 | +class Electric_Car(Car): |
| 106 | + def __init__(self, brand , Model , Bettery_Size): |
| 107 | + super().__init__(brand,Model) |
| 108 | + self.Bettery_size = Bettery_Size |
| 109 | + def fule_type(self): # polymorphism |
| 110 | + return 'Electric Charge' |
| 111 | +# print(car.brand) |
| 112 | +# print(car.Model) |
| 113 | +# new_car = Car('Tayota', ' Safari') |
| 114 | +# print(new_car.brand) |
| 115 | +# print(new_car.Model) |
| 116 | +# print(car.car_info()) |
| 117 | +my_tesla = Electric_Car('Tesla', 'Model S' , '39KWh') |
| 118 | +# print(my_tesla.brand) |
| 119 | +# print(my_tesla.car_info()) |
| 120 | +# print(my_tesla.get_brand()) |
| 121 | +# print(my_tesla.set_brand(192)) |
| 122 | +# print(my_tesla.get_brand()) |
| 123 | +# print(my_tesla.__brand) |
| 124 | +# print(my_tesla.__brand) |
| 125 | +# polymorphism mean a method can have same name but different behaviour like fule type in electric car and fule type in petrol car or deisal car |
| 126 | +print(my_tesla.fule_type()) |
| 127 | +car = Car('Tesla', 'Model S' , ) |
| 128 | +c = Car('Tesla', 'Model S' , ) |
| 129 | +c = Car('Tesla', 'Model S' , ) |
| 130 | +ca = Car('Tesla', 'Model S' , ) |
| 131 | +print(car.total_car) |
| 132 | +print(car.fule_type()) |
| 133 | +print(car.general_description()) |
| 134 | +print(Car.general_description()) |
| 135 | +print(car.model) |
| 136 | +print(isinstance (car , Car)) |
| 137 | +print(isinstance (car , Electric_Car)) |
| 138 | +print(isinstance (my_tesla , Electric_Car)) # to check is the object is the instance of class in boolean form |
0 commit comments