Python 7 Levels, L5, Unit 51, Object Oriented Advanced

Powered By EmbedPress

Exercise 1:

class Student():
	def study(self):
		print("studying")

print(type(Student))
print(id(Student))
print(Student)
Stu = Student
stu = Stu()
stu.study()

Exercise 2:

class Student():
	school = "Sunshine Primary School"#class attributes
	
	def __init__(self,name,score):
		self.name = name#instance object attribute
		self.score = score#instance object attribute
	
	def __str__(self):
		msg = "I am from :{0},My name is :{1},score is :{2}".format(Student.school,self.name,self.score)
		return msg
	
stu = Student("David Smith",80)
print(stu)

Exercise 3:

class Student():
	school = "Sunshine Primary School"
	
	@classmethod
	def print_school(cls):
		print(Student.school)
	
Student.print_school()

Exercise 4:

class Array():
	def list_max(list1):#find max value in a list
		max1 = list1[0]
		for i in list1:
			if i > max1:
				max1 = i
		return max1
	
	def seek(a,list1):#search a list
		for i in list1:
			if i == a:
				return True
			else:
				return False

result = Array.list_max([1,3,77,3,5,66,0,-1,22])
print(result)

Exercise 5:

class Student():
	school = "Sunshine Primary School"  # class attributes

	def __init__(self, name, score):
		self.name = name  # instance object attribute
		self.score = score  # instance object attribute

	def __str__(self):
		msg = "I am from :{0},My name is :{1},score is :{2}".format(Student.school, self.name, self.score)
		return msg

	@classmethod
	def print_school(cls):#class method
		print(Student.school)
	
	@staticmethod
	def say():
		print("Study hard, make a progress everyday!")
stu = Student("David Smith",90)
print(stu)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts