Python 官方文档:入门教程 => 点击学习
这篇文章将为大家详细讲解有关python继续找对象的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。面向对象三大特征:封装、继承、多态1、封装(提高程序的安全性)class Car:&nb
这篇文章将为大家详细讲解有关python继续找对象的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
class Car: def __init__(self,brand): self.brand=brand def start(self): print('自行车已被蹬跑')car=Car('自行车')car.start()print(car.brand)
运行结果
自行车已被蹬跑
自行车
一开始它报错说没有定义name,我找老大一会不知道哪错了,原来是第六行
self.name
,那个时候写成,了。
看在stu里边有哪些方法?就这样写
在类的外部可以通过_Student(类名)_ _age(不希望被访问的)进行访问
class Student: def __init__(self,name,age): self.name=name self.__age=age #年龄不希望在类的外部使用,所以加了两个_ def show(self): print(self.name,self.__age)stu=Student('张三',20)stu.show()#在类的外部使用name和ageprint(stu.name)print(dir(stu))print(stu._Student__age)
张三 20张三['_Student__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__fORMat__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'show']20
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age)class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_noclass Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyearstu=Student('张三',20,'1001')teacher=Teacher('李四',34,10)stu.info()teacher.info()
张三 20
李四 34
此时只能输出学号,不满足需求
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age)class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): print(self.stu_no)class Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyearstu=Student('张三',20,'1001')teacher=Teacher('李四',34,10)stu.info()teacher.info()
1001
李四 34
看下边的重载
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age)class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): super(Student, self).info() print(self.stu_no)class Teacher(Person): def __init__(self,name,age,teacherofyear): super(Teacher, self).__init__(name,age) self.teacherofyear=teacherofyearstu=Student('张三',20,'1001')teacher=Teacher('李四',34,10)stu.info()print('----------------------------')teacher.info()
运行结果
张三 20
1001
----------------------------
李四 34
把教龄输出
class Person(object): def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age)class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): super(Student, self).info() print(self.stu_no)class Teacher(Person): def __init__(self,name,age,teachofyear): super().__init__(name,age) self.teachofyear=teachofyear def info(self): super().info() print('教龄',self.teachofyear)stu=Student('张三',20,'1001')teacher=Teacher('李四',34,10)stu.info()print('----------------------------')teacher.info()
运行结果
张三 20
1001
----------------------------
李四 34
教龄 10
Java就是静态语言,Python就是动态语言
1初始化init
2new
两个下划线开始,两个下划线结束就是特殊属性
绑定两个属性
class A: passclass B: passclass C(A,B): def __init__(self,name,age): self.name=name self.age=age#创建C类的对象x=C('Jack',20)#x是C类的一个实例对象print(x.__dict__)
{'name': 'Jack', 'age': 20}
点击加号那里,就会释放,点击减号就会缩成这样,这说明了被缩起来的内容都是隶属于这个类的。
看最左侧出现了箭头,他的意思是重写person类中的方法
关于“python继续找对象的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
--结束END--
本文标题: python继续找对象的示例分析
本文链接: https://www.lsjlt.com/news/307217.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0