Sunday, February 18, 2018

Classes

Classes define the general behavior that a whole category of objects can have.
class RealMadrid():
  def __init__(self, name, position):
      self.name = name
      self.position= position

 def regular(self):
      print(self.name. + "is a regular player .")

 def pos(self):
      print(self.position + " is his position!")




Everything related to functions is applied to classes, the only difference is that we 'll call them methods and as you can see we added  the __init__ method. This will allow us to create a new instance. 
(1) myTeam = RealMadrid('Ronaldo','striker')

Making an instance from a class:
(1)
print('My favorite player is: ' + myTeam.name)
print('He plays as: ' + myTeam.pos)

Output:
My favorite player is Ronaldo
He plays as striker

Calling Methods:
myTeam.regular()

Output:
Ronaldo is a regular player

Another example:
class Students():
    def __init__(self, name, studentId, age):
        self.name = name
        self.studentId = studentId
        self.age = age
    def studentInfo(self):
        info = 'Name:' + self.name + ' Student ID: ' \
+ str(self.studentId) + ' Age: ' + str(self.age)
        return info

student = Students('Barry Allen', 32305, 29)
print(student.studentInfo())

Output:
Name:Barry Allen Student ID: 32305 Age: 29

Now lets say we want to set a common value for each class object:
class Students():
    def __init__(self, name, studentId, age, classNumber):
        self.name = name
        self.studentId = studentId
        self.age = age
        self.classNumber = classNumber

    def studentInfo(self):
        info = 'Name:' + self.name + ' Student ID: ' \
str(self.studentId) + ' Age: ' str(self.age)  + ' Course: ' (self.classNumber)
        return info

student = Students('Barry Allen', 32305, 29, 'CGS 1060')
print(student.studentInfo())

Output:
Name:Barry Allen Student ID: 32305 Age: 29 Course: CGS 1060

Modifying attributes:
student.name = 'Peter Cook'
print(student.studentInfo())

Output:
Name:Barry Allen Student ID: 32305 Age: 29 Course: CGS 1060

Name:Peter CooK Student ID: 32305 Age: 29 Course: CGS 1060

Using methods to modify our attributes:
def updateInfo(self, studentName):
      self.name = studentName      


Output: 
Name:Barry Allen Student ID: 32305 Age: 29 Course: CGS 1060
Name:Peter CooK Student ID: 32305 Age: 29 Course: CGS 1060
Name:Ricky Martin Student ID: 32305 Age: 29 Course: CGS 1060

Final Code:
class Students():      
  def __init__(self, name, studentId, age, classNumber):                self.name = name            



      self.studentId = studentId            
      self.age = age            
      self.classNumber = classNumber
 def studentInfo(self):             
      info = 'Name:' + self.name + ' Student ID: ' + str(self.studentId) + ' Age: ' + str(self.age) + 'Course: ' + (self.classNumber)
             return info
 def updateInfo(self, studentName):             
    self.name = studentName
student = Students('Barry Allen', 32305, 29, 'CGS 1060')print(student.studentInfo())
student.name = 'Peter CooK'print(student.studentInfo())



student.updateInfo('Ricky Martin')print(student.studentInfo())




  




No comments:

Post a Comment