自学Python Day 15 : Lesson 41 & Lesson 42

tech2024-08-09  49

Day 15

Lesson 41

面向对象:

class(类):告诉python去创建一个新类型。 object(对象):有两种意思,事物的基本类型,或者事物的实例化。 instance(实例):你通过python创建一个类所获得的。 def:用来在类中定义一个函数。 self:在一个类包含的函数中,self是一个用来访问实例或对象的变量。 inheritance:概念,表示一个类可以继承另一个类的特征,就像你和你的父母。 composition:概念,表示一个类可以包含其他类,就像汽车轮子。 attribute:类所拥有的特性,通常是变量。 is-a:惯用语,表示一个东西继承自另一个东西(a),像在“鲑鱼”是“鱼”。 has-a:惯用语,表示由其他事情或有一个特征(a),如“鲑鱼有嘴。 – Learn_python_the_hard_way”

Lesson 42

这课只要一个概念弄清楚就好了

鱼是一个“类(class)”,三文鱼是一个“类(class)”,而一只叫Mary的鱼是一个“对象(object)”。

这里有一些练习 可以自己做一下~

首先针对类和对象,你需要学会两个说法,“is-a(是啥)”和“has-a(有啥)”。“是啥”要用在谈论“两者以类的关系互相关联”的时候,而“有啥”要用在“两者无共同点,仅是互为参照”的时候。

接下来,通读这段代码,将每一个注释为##?? 的位置标明他是“is-a”还是“has-a”的关系,并讲明白这个关系是什么。在代码的开始我还举了几个例子,所以你只要写剩下的就可以了。

记住,“是啥”指的是鱼和三文鱼的关系,而“有啥”指的是三文鱼和鳃的关系。-- Learn_python_the_hard_way

## Animal is-a object (yes, sort of confusing) look at the extra credit class Animal(object): pass ## ?? class Dog(Animal): def __init__(self, name): ## ?? self.name = name ## ?? class Cat(Animal): def __init__(self, name): ## ?? self.name = name ## ?? class Person(object): def __init__(self, name): ## ?? self.name = name ## Person has-a pet of some kind self.pet = None ## ?? class Employee(Person): def __init__(self, name, salary): ## ?? hmm what is this strange magic? super(Employee, self).__init__(name) ## ?? self.salary = salary ## ?? class Fish(object): pass ## ?? class Salmon(Fish): pass ## ?? class Halibut(Fish): pass ## rover is-a Dog rover = Dog("Rover") ## ?? satan = Cat("Satan") ## ?? mary = Person("Mary") ## ?? mary.pet = satan ## ?? frank = Employee("Frank", 120000) ## ?? frank.pet = rover ## ?? flipper = Fish() ## ?? crouse = Salmon() ## ?? harry = Halibut()

今日进度

Day 3

import sys import time def heartlost(heart) -> int: heart -= 1 print("You lost one heart") if heart == 0: time.sleep(5) print("You have ran out of heart, too bad! Game over.") sys.exit() else: time.sleep(5) print("Be careful! You only have %d heart left!" % heart) return heart def heartgain(heart): heart += 1 print("You gain one heart!") time.sleep(5) print("You have %d heart now!" % heart) return heart def forest(heart): print("You wake up and you find yourself in a dark dark forest.") time.sleep(5) print("You have two choices, 1. walk forward and try to find a road 2. try to find a flashlight") time.sleep(5) print("Please type 1 or 2:") c = input('>') if c == '1': print("You can't see anything, you accidentally step on a snack, it bite you and ran away") heart_now = heartlost(heart) heart = heart_now time.sleep(5) print("You talk forward some more, and see a little house, you knock on the door") house(heart) if c == '2': print("You knee down and try to see if there's anything on the ground, and, there's a flashlight!") time.sleep(5) print("You turn on the flashlight and see there's a little house in front") time.sleep(5) print("Will you knock on the door? 1. Yes 2. No") c = input('>') if c == '1': house(heart) else: walk(heart) def house(heart): print("You peek through the window, the light is on") time.sleep(5) print("About a minute later, no one answer") time.sleep(5) print("Do you want to just open the door? 1. Yes, open it 2. No, I want to wait") c = input('>') if c == '1': time.sleep(5) print("There's someone behind the door, he yell at you and push you out of the house") time.sleep(5) print("You fell on the ground and hurt yourself") heart_now = heartlost(heart) heart = heart_now walk(heart) if c == '2': time.sleep(5) print("You wait another one minute, someone open the door, and ofer you some food") time.sleep(5) print("You eat it all and felt good!") time.reat(5) heart_now = heartgain(heart) heart = heart_now walk(heart) def walk(heart): print("You decide to walk forward") time.rest(5) print("You saw some light in front of you. You can't wait to see what it is. You decided to 1. Run 2. Walk") c = input() if c=='1': time.rest(5) print("It's a long way to the light, you keep on running and running and... You're really tired") heart_now = heartlost(heart) heart = heart_now else: time.rest(5) print("You walk slowly to save energy, man, it's really a long way...") forest(5)```
最新回复(0)