花了一个下午把优达的的面向对象python课刷了,回去之前做个总结。
主要是两个例子
1 电影预告网页
1.1 mian
import media
import fresh_tomatoes
toy_stort = media.Movie("Toy Story",
"A story of a boy and his toys",
"link",
"link")
# print(toy_stort.storyline)
avatar = media.Movie("Avatar","A marinr on an alien plant", "Link","http://www.iqiyi.com/v_19rrn8vkts.html?vfm=2008_aldbd&fc=828fb30b722f3164&fv=p_02_01");
# print(avatar.storyline)
# avatar.show_traile()
sanssans = media.Movie("Sanssans","A marinr on an alien plant", "Link","http://video.mtime.com/66932/?mid=225337")
# sanssans.show_traile()
moives = [toy_stort,avatar,sanssans]
# fresh_tomatoes.open_movies_page(moives)
# print(media.Movie.VALID_RATINGS)
print(media.Movie.__doc__)
1.2 media
import webbrowser
class Movie():
""" This class provides a way to store movie related information """
VALID_RATINGS = ["G","PG","PG-13","R"]
def __init__(self,movie_title,movie_storyline,poster_image_url,trailer_url):
self.title = movie_title
self.storyline = movie_storyline
self.poster_image_url = poster_image_url
self.trailer_youtube_url = trailer_url
def show_traile(self):
webbrowser.open(self.trailer_youtube_url)
2 继承例子
child 初始化可以继承自Parent
当child中的show_info没有定义,可以调用Parent的方法
class Parent():
def __init__(self, last_name, eye_color):
print("Parent Constructor Called")
self.last_name = last_name
self.eye_color = eye_color
def show_info(self):
print("Last Name - "+self.last_name)
print("Eye Color - "+self.eye_color)
class child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
print("Child Constructor Called")
Parent.__init__(self, last_name, eye_color)
self.number_of_toys=number_of_toys
def show_info(self):
print("Last Name - "+self.last_name)
print("Eye Color - "+self.eye_color)
print("number_of_toys -"+self.number_of_toys)
biily_cyrus = Parent("Cyrus","blue")
print(biily_cyrus.last_name)
biily_cyrus.show_info()
#
miley_cyrus = child("Cyrus","blue","2")
print(miley_cyrus.last_name)
print(miley_cyrus.number_of_toys)
miley_cyrus.show_info()