创建关于汽车的类

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类学习的更多相关文章

  1. python类学习以及mro--多继承属性查找机制

    版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...

  2. Python 类学习的一些Tips

    这里不详细介绍类,只总结一些小萌新在学习python 类时会有的一些疑点. 类的私有性 在python中,属性和方法的访问权限只有两种,公开的,和私有的.在给属性命名时用两个“__”下划线作为开头,就 ...

  3. python类,魔术方法等学习&&部分ssti常见操作知识点复习加深

    python类学习&&部分ssti常见操作知识点复习加深 在做ssti的模块注入的时候经常觉得自己python基础的薄弱,来学习一下,其实还是要多练习多背. 在python中所有类默认 ...

  4. python入门学习:8.类

    python入门学习:8.类 关键点:类 8.1 创建和使用类8.2 使用类和实例8.3 继承8.4 导入类 8.1 创建和使用类   面向对象编程是最有效的软件编写方法之一.在面向对象编程中,你编写 ...

  5. Python学习笔记之面向对象编程(三)Python类的魔术方法

    python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...

  6. 学习python类

    类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...

  7. 60分钟Python快速学习(给发哥一个交代)

    60分钟Python快速学习 之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空 ...

  8. !!对python列表学习整理列表及数组详细介绍

    1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...

  9. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

随机推荐

  1. 用JavaScript实现快排

    啥是快排这里就不讲了,主要还是把原来c++学的东西抓紧转化过来 快排运用的是分而治之的思想,确定一个中值,把大的放右边,小的放左边,然后再左右分别对左右的左右(雾)进行处理 需要注意的一点是,这玩意远 ...

  2. 查看PublicKeyToken和生成PublicKeyToken

    http://hi.baidu.com/honfei/item/7777500b20d8ff8a02ce1bd2

  3. [NLP-CNN] Convolutional Neural Networks for Sentence Classification -2014-EMNLP

    1. Overview 本文将CNN用于句子分类任务 (1) 使用静态vector + CNN即可取得很好的效果:=> 这表明预训练的vector是universal的特征提取器,可以被用于多种 ...

  4. STL漫谈

    从现在开始,想写一个关于STL工具的各种tip类的东西,记录下那些细节,以免以后使用STL工具时出错. 1.关于map,如果需要第一个键值需要放进一个结构体,那么结构体是需要写好其自定义的排序规则的, ...

  5. C/C++程序基础-标准头文件的结构

    1:标准头文件有如下结构,请解释. #ifndef _INCvxWorksh #define _INCvxWorksh#ifdef _cplusplus extern "C"{ # ...

  6. Leetcode题目56.合并区间(中等)

    题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 ...

  7. Linux安装软件的时候出现乱码?

    在Linux的中文操作系统下使用xmanager进行软件安装的时候,可能出现乱码界面,可以通过以下方法进行解决 1 修改环境属性  vi /etc/sysconfig/i18n LANG=" ...

  8. Nginx之最简单的反向代理机制分析

    注:当前分析基于 Nginx之搭建反向代理实现tomcat分布式集群 的配置. 1. 用到的指令 下面介绍在上面的配置中用到的指令. upstream 指令 语法:upstream name { .. ...

  9. Docker镜像搭建ubuntu下samba目录共享

    第一种方法:(未使用) yum install docker // 下载镜像 docker pull dperson/samba // 启动镜像,具体看文档,但重要的配置是以下的注释 docker r ...

  10. LaTeX统计文章字数

    今天学会了用LaTeX命令统计文章字数,命令如下: textcount doc_full_path.tex 效果如下: