创建关于汽车的类

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. find(expr|obj|ele)搜索所有与指定表达式匹配的元素。

    find(expr|obj|ele) 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法. 所有搜索都依靠jQuery表达式来完成.这个表达式可以使用CSS1-3的选 ...

  2. 【转载】Dijkstra算法和Floyd算法的正确性证明

      说明: 本文仅提供关于两个算法的正确性的证明,不涉及对算法的过程描述和实现细节 本人算法菜鸟一枚,提供的证明仅是自己的思路,不保证正确,仅供参考,若有错误,欢迎拍砖指正   ----------- ...

  3. AGC032F One Third

    很奇怪的一个题.看见了无从下手.概率期望好题. 给一个面积为 \(1\) 的圆,经过圆心随机幅角切直径 \(n\) 次,定义 \(f(x) = \min |S - \frac{1}{3}|\),其中 ...

  4. 数据结构实验之链表三:链表的逆置(SDUT 2118)

    题目链接 #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; ...

  5. 转:JSON Assertion 适用于json格式的响应断言

    当响应结果是json格式时,用JSON Assertion更方便判断. 1 在请求上右键添加json断言 2  编辑json Assertion 判断方式: 如果响应结果不是json格式的,fail ...

  6. 【java设计模式】-03抽象工厂模式

    抽象工厂 简述 抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类.在抽象工厂模式中,接口是负责创建一个相关对象的工厂 ...

  7. OUC_Summer Training_ DIV2_#14 724

    又落下好多题解啊...先把今天的写上好了. A - Snow Footprints Time Limit:1000MS     Memory Limit:262144KB     64bit IO F ...

  8. koa 项目实战(九)passport验证token

    1.安装模块 npm install koa-passport -D npm install passport-jwt -D 2.解析token 根目录/config/passport.js cons ...

  9. ubuntu下如何使用apt-get安装arm64的交叉编译工具链?

    答: sudo apt-get install gcc-aarch64-linux-gnu -y

  10. 微服务中的CAP定律

    说到微服务,先给大家提一下CAP分布式应用知识吧,无论你微服务使用的是阿里云开源的Dubbo还是基于Springboot的一整套实现微服务的Springcloud都必须遵循CAP定理不然你所实现的分布 ...