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 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
随机推荐
- Python数据分析:Numpy学习笔记
Numpy学习笔记 ndarray多维数组 创建 import numpy as np np.array([1,2,3,4]) np.array([1,2,3,4,],[5,6,7,8]) np.ze ...
- D. Shortest Cycle(floyd最小环)
D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- sprintf简介
sprintf 基本用法 输入一段有特点的字符串 #include <cstdio> #include <cstring> using namespace std; int m ...
- 队列(Java实现)
队列的特点是先进先出. 基于链表的队列 public class LinkedListQueue<Item> { private Node first; // 指向最早添加进队列的元素 p ...
- java线程基础方法详解
一.线程状态转换 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行 ...
- zabbix自定义监控,自定义图表。
zabbix server:hostname=Zabbix server ip:192.168.100.7 zabbix agent: hostname=host3 ip:192.168.100.3 ...
- Linux 下安装 Tomcat 服务器和部署 Web 应用
一.上传Tomcat服务器 二.安装Tomcat服务器 2.1.解压tomcat服务器压缩包 2.2.配置环境变量 tomcat服务器运行时是需要JDK支持的,所以必须配置好JDK用到的那些环境变量 ...
- Sass安装与Webstorm File Watcher配置
一.Sass安装 ruby安装 mac系统默认安装了ruby,可以直接跳过此步骤,linux和windows需要安装ruby环境. windows安装ruby环境: 到ruby官网下载自己系统适用的版 ...
- 通过反射将一个java对象的属性值转换为一个Map
将一个java对象的属性值转换为一个Map: import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.P ...
- Qt编写自定义控件14-环形进度条
前言 环形进度条,用来展示当前进度,为了满足大屏UI的需要特意定制,以前有个叫圆环进度条,不能满足项目需要,只能重新定做,以前的进度间距不能自适应分辨率,而且当前进度对应的反的进度不能单独设置颜色,即 ...