Mon Aug 02 2021

Class Object

File Name: class-object.py

#!/usr/bin/evn python

# Define a class as 'student'
class student:
    
    # Constructor with parameters
    # The first argument of constructor reference to the current instance of the class
    def __init__ (self, name, science, history, math):
        self.name = name
        self.marks = science + history + math
    
    # Method of the class
    def displayMarks(self):
        print("Total marks of", self.name, "=", self.marks)
        
# 'stu1' & 'stu2' are the object of the class 'student'
stu1 = student("James", 50,60,80)
stu2 = student("Jonh", 80,50,20)

# Calling method of the class 'student'
stu1.displayMarks()
stu2.displayMarks()

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.