Untitled
unknown
plain_text
3 years ago
1.3 kB
5
Indexable
#Creating an empty class named "Employee" class Employee: #Define a class attribute ("Global attribute" within the class.) MIN_salary = 30000 #<--- no self #(Not necessary but most class object has this: init constructor) def __init__(self, name, salary=0): #Create the name and salary attributes #Listing all attributes here and also links them with the inputs. self.name = name self.salary = salary #Creating conditionings for attributes if applicable if salary >= Employee.MIN_salary: self.salary = salary else: self.salary = Employee.MIN_salary """Alternative constructor""" @classmethod #cls refers to "class", will call __init__(). def from_str(cls,datestr): parts = datestr.split("-") year, month, day = int(parts[0]), int(parts[1]), int(parts[2]) # Return the class instance return cls(int(year), int(month),int(day)) """Defining attributes in class definition""" def set_name(self, new_name): self.name=new_name def set_salary(self,new_salary): self.salary=new_salary #Created an object emp of class Employee emp = Employee() #Setting up attributes for your class object emp.set_name("Eric Lau") emp.set_salary(50000) print(emp.salary)
Editor is loading...