python类学习
创建关于汽车的类
class Cars():
def __init__(self, brand, country):
self.brand = brand
self.country = country
def show_brand(self):
print(self.brand.title() + " is a popular automobile brand.")
def show_country(self):
print("Its headquater is in " + self.country.title() + ".")
my_car = Cars('toyota', 'Japan')
my_car.show_brand()
my_car.show_country()
输出
Toyota is a popular automobile brand.
Its headquater is in Japan.
列子
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine = cuisine_type def describe_restaurant(self):
print("There is a restaurant called " + self.restaurant_name.title() + ", its cuisine is " + self.cuisine.title() + ".") def open_restaurant(self):
print("The restaurant " + self.restaurant_name.title() + " is opening...") restaurant = Restaurant('xianghui xin', 'xiang cuisine')
restaurant.describe_restaurant()
print("I like " + restaurant.restaurant_name.title() + '.')
restaurant.open_restaurant()
print("Let's go " + restaurant.restaurant_name.title() + " and have lunch.")
输出
There is a restaurant called Xianghui Xin, its cuisine is Xiang Cuisine.
I like Xianghui Xin.
The restaurant Xianghui Xin is opening...
Let's go Xianghui Xin and have lunch.
类的默认属性值和修改方法1
class Car():
def __init__(self, make, model, year):
"""初始化汽车描述属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def read_odometer(self):
print("My car is " + self.make.title() + " " + self.model.title() + ", production date is " + str(self.year) + ".")
print("This car has " + str(self.odometer_reading) + "KM mileage on it.") my_new_car = Car('audi', 'a8l', 2019)
my_new_car.read_odometer() my_new_car.odometer_reading = 1000
my_new_car.read_odometer()
输出
My car is Audi A8L, production date is 2019.
This car has 0KM mileage on it.
My car is Audi A8L, production date is 2019.
This car has 1000KM mileage on it.
修改方法2
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title() def read_odometer(self):
print("This car has" + " " + str(self.odometer_reading) + "KM mileage on it.") def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer !") def increment_odometer(self, miles):
self.odometer_reading += miles my_new_car = Car('audi', 'a8l', 2019)
get_descriptive_name = my_new_car.get_descriptive_name()
print(get_descriptive_name)
my_new_car.read_odometer()
my_new_car.update_odometer(100)
my_new_car.read_odometer()
my_new_car.update_odometer(90)
my_new_car.increment_odometer(10)
my_new_car.read_odometer()
输出
2019 Audi A8L
This car has 0KM mileage on it.
This car has 100KM mileage on it.
You can't roll back an odometer !
This car has 110KM mileage on it.
父类和子类
class Car():
"""传统汽车的属性"""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title() def read_odometer(self):
print("This car has" + " " + str(self.odometer_reading) + "KM mileage on it.") def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer !") def increment_odometer(self, miles):
self.odometer_reading += miles class ElectricCar(Car):
"""电动汽车的独特属性"""
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = 70 def describe_battery(self):
print("This car has a " + str(self.battery) + "-kWh battery.") my_car = Car('audi', 'a8l', 2019)
get_my_car = my_car.get_descriptive_name()
print(get_my_car)
my_car.read_odometer() my_tesla = ElectricCar('tesla', 'mode s', 2019)
get_my_tesla = my_tesla.get_descriptive_name()
print(get_my_tesla)
my_tesla.describe_battery()
输出
2019 Audi A8L
This car has 0KM mileage on it.
2019 Tesla Mode S
This car has a 70-kWh battery.
python类学习的更多相关文章
- python类学习以及mro--多继承属性查找机制
版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...
- Python 类学习的一些Tips
这里不详细介绍类,只总结一些小萌新在学习python 类时会有的一些疑点. 类的私有性 在python中,属性和方法的访问权限只有两种,公开的,和私有的.在给属性命名时用两个“__”下划线作为开头,就 ...
- python类,魔术方法等学习&&部分ssti常见操作知识点复习加深
python类学习&&部分ssti常见操作知识点复习加深 在做ssti的模块注入的时候经常觉得自己python基础的薄弱,来学习一下,其实还是要多练习多背. 在python中所有类默认 ...
- python入门学习:8.类
python入门学习:8.类 关键点:类 8.1 创建和使用类8.2 使用类和实例8.3 继承8.4 导入类 8.1 创建和使用类 面向对象编程是最有效的软件编写方法之一.在面向对象编程中,你编写 ...
- Python学习笔记之面向对象编程(三)Python类的魔术方法
python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 60分钟Python快速学习(给发哥一个交代)
60分钟Python快速学习 之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空 ...
- !!对python列表学习整理列表及数组详细介绍
1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...
- Python Tutorial 学习(八)--Errors and Exceptions
Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
随机推荐
- Java进阶知识16 Spring创建IOC容器的两种方式
1.直接得到 IOC 容器对象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app ...
- Cogs 734. [网络流24题] 方格取数问题(最大闭合子图)
[网络流24题] 方格取数问题 ★★☆ 输入文件:grid.in 输出文件:grid.out 简单对比 时间限制:1 s 内存限制:128 MB «问题描述: 在一个有m*n 个方格的棋盘中,每个方格 ...
- a a[0] &a &a[0]的理解
数组中几个关键符号(a a[0] &a &a[0])的理解(前提是 int a[10])(1)这4个符号搞清楚了,数组相关的很多问题都有答案了.理解这些符号的时候要和左值右值结合起来, ...
- .net core Redis库 CSRedis
由于servicestack.redis收费,基于有人说StackExchange.Redis 使用会出现一些问题比如会超时, 找到了CSRedis这个库,很强大很实用.另外有兴趣的朋友还可以试试另一 ...
- Love to be loved by you & Just one last dance
http://baike.baidu.com/link?url=wOnBuPncIH5b5oWc0ZREXCU8x6XPYqlZazTLarTjE8eOpdtpv57YMeB_kgXQq4BcCeh2 ...
- Codeforces 915 F. Imbalance Value of a Tree(并查集)
F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
- javaScript基础用Number()把其它类型转换为Number类型
一:基本类型 字符串 把字符串转换为数字,只要字符串中包含任意一个非有效数字字符(第一个点除外)结果都是NaN,空字符串会变为数字零 console.log(Number("12.5&quo ...
- Java和python中的面向对象
Python与Java中的示例类 Java类是在与类同名的文件中定义的.因此,必须将该类保存在一个名为Car.java的文件中.每个文件中只能定义一个类. public class Car { pri ...
- postman设置环境变量,实现一套接口根据选择的环境去请求不同的url
一个系统,有本地,开发,测试,生产等不同的环境,如果写不同的url配置多套会比较麻烦,可以设置不同的环境实现不同的url之间的切换.配置之后如下: 第一步: 第二步: 添加环境变量 ps::不同的环境 ...
- [Tex学习笔记]章节用罗马字母编号
微信扫描如上二维码关注跟锦数学微信公众账号. 详情请见那里.