Skip to content

Commit 25830ce

Browse files
committed
add python
1 parent 36fa9da commit 25830ce

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

JavaKnowledge/python3入门.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ while current_number <= 5:
404404

405405
```python
406406
def greet_user(username):
407+
"""函数功能的注释"""
407408
# 返回值
408409
return 'Hello ' + username
409410

@@ -413,6 +414,8 @@ print(greet_user(username='lili'))
413414
```
414415
同样,在Python中函数也支持参数的默认值。
415416

417+
上面三个引号的部分是文档字符串格式,用于简要的阐述其功能的注释。
418+
416419
### 函数列表参数副本
417420

418421
将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永久性的,这让你能够高效地处理大量的数据。
@@ -491,8 +494,112 @@ make_pizza(12,'mushrooms','green peppers','extra cheese')
491494
```
492495
若使用这种语法,调用函数时就无需使用句点。由于我们在import语句中显式地导入了函数make_pizza(),因此调用它时只需指定其名称。
493496

497+
还可以使用星号(`*`)导入模块中的所有函数:
498+
499+
```python
500+
from pizza import *
501+
make_pizza(16,'pepperoni')
502+
make_pizza(12,'mushrooms','green peppers','extra cheese')
503+
```
504+
`import *`会将模块pizza中的每个函数都复制到这个程序文件中。
505+
由于导入了每个函数,可通过名称来调用每个函数,而无需使用句点表示法。
506+
然而,使用并非自己编写的大型模块时,最好不要采用这种导入方法: 如果模块中有函数的名称与你的项目中使用的名称相同,可能导致意想不到的结果,因为Python可能遇到多个名称相同的函数或变量,进而覆盖函数,而不是分别导入所有的函数。
507+
508+
509+
### 别名
510+
511+
如果要导入的函数的名称与现有的名称冲突,或者函数的名称太长,可以通过别名的方式进行指定。
512+
513+
```python
514+
from pizza import make_pizza as mp
515+
mp(16,'pepperoni')
516+
mp(12,'mushrooms','green peppers','extra cheese')
517+
```
518+
519+
同样也可以通过as给模块指定别名:
520+
521+
```python
522+
import pizza as p
523+
524+
p.make_pizza(16,'pepperoni')
525+
p.make_pizza(12,'mushrooms','green peppers','extra cheese')
526+
```
527+
528+
529+
##
530+
531+
在Python中,首字母大写的名称指的是类。
532+
根据类来创建对象叫实例化。
533+
534+
dog.py
535+
```python
536+
class Dog:
537+
def __init__(self, name, age):
538+
self.name = name
539+
self.age = age
540+
541+
def sit(self):
542+
print(self.name + " Sitting")
543+
544+
def roll(self):
545+
print(self.name + " Rolling")
546+
```
547+
548+
__init__()是一个特殊的方法,每当你根据Dog类创建新实例时,Python都会自动运行它。在这个方法中形参self必不可少,还必须位于其他形参的前面。
549+
550+
为什么必须在方法定义中包含形参self呢?因为Python调用这个__init__()方法来创建Dog实例时,将自动传入实参self。
551+
552+
每个与类相关联的方法调用都自动传入实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。
553+
554+
```python
555+
import dog
556+
557+
dog = dog.Dog("xiaohei", 1)
558+
# 属性
559+
print(dog.name)
560+
# 方法
561+
dog.sit()
562+
```
494563

495564

565+
### 继承
496566

567+
一个类继承另一个类时,它将自动获得另一个类的所有属性和方法。
568+
569+
创建子类实例时,Python首先需要完成的任务是给父类的所有属性赋值。因此,子类的__init__()方法需要调用父类的方法。
570+
571+
```python
572+
class Car:
573+
def __init__(self, make, model, year):
574+
self.make = make
575+
self.model = model
576+
self.year = year
577+
578+
def get_des(self):
579+
name = str(self.year) + str(self.make) + str(self.model)
580+
return name.title()
581+
582+
583+
class ElectricCar(Car):
584+
def __init__(self, make, model, year, battery):
585+
super().__init__(make, model, year)
586+
self.battery = battery
587+
588+
# 重写父类方法
589+
def get_des(self):
590+
name = str(self.year) + str(self.make) + str(self.model) + str(self.battery)
591+
return name.title()
592+
def get_battery(self):
593+
print("battery : " + str(self.battery))
594+
595+
596+
tesla = ElectricCar('Tesla', 'Model S', 2021, 80)
597+
print(tesla.get_des())
598+
tesla.get_battery()
599+
```
497600

601+
- 创建子类时,父类必须包含在当前文件中,且位于子类前面。
602+
- 定义子类时,必须在括号内指定父类的名称
603+
- 方法__init__()接受创建子类实例所需的信息
498604

605+
super()是一个特殊函数,帮助Python将父类和子类关联起来。这行代码让子类包含父类的所有属性。

VideoDevelopment/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)