In this tutorial I will show you how to write basic object oriented programming in Python programming language.
I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details. If you want to advertise on my website, kindly contact me also at my email address also. Thank you.
My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com
My mobile number here in the Philippines is 09173084360.
Program Listing
class Dog:
#class attribute
breed = "German Shepherd"
#instance attributes (pangalan,edad)
def __init__(self,name,age): # name and age are called paramaters
self.pangalan = name
self.edad = age
#instance method
def run(self):
return f"{self.pangalan} is running"
#instance objects
preppy = Dog("Preppy",3) #instantation statement
browny = Dog("Browny",8) #instantation statement
#accessing instance attributes
print()
print("\tBasic OOP in Python")
print()
print(f"{preppy.pangalan} is {preppy.edad} years old")
print(f"{browny.pangalan} is {browny.edad} years old")
#accessing class attribute
print(f"Preppy is a {preppy.breed}")
print(f"Browny is also a {browny.breed}")
#accesing instance method using preppy object
print(preppy.run())
#accesing instance method using browny object
print(browny.run())
print()
print("\tEnd of Program")
print()
No comments:
Post a Comment