JAVA基础——面向对象三大特性:封装、继承、多态

JAVA基础——面向对象三大特性:封装、继承、多态

封装

封装就是使用特殊的语法,对成员属性和成员方法进行包装,达到保护和隐藏的目的但是一定注意,不能把成员全部封装死,就失去意义了被封装的成员主要是供类的内部使用被特殊语法封装的成员,会有不同的访问的权限

好处

只能通过规定的方法访问数据。

隐藏类的实例细节,方便修改和实现。

封装的级别

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" 
lang="" 
cid="n6" 
mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

封装的级别:

成员 ==> 公有的
_成员 ==> 受保护的 (约定俗成,而python没有具体实现)
__成员 ==> 私有的
公有的 public 受保护的 protected 私有的 private
在类的内部 OK OK OK
在类的外部 OK No(python可以) No</pre>

封装的实现

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" 
cid="n8" 
mdtype="fences" 
style="box-sizing: border-box; overflow: 
visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"></pre>

公有的封装:

定义:默认定义的成员都属于公有成员
特征:公有的成员可以在任何位置进行访问和操作
受保护封装
定义:在成员名称前面加一个下划线 _成员名称
特征:受保护的成员和公有成员一样可以在任何位置进行访问,但是一般不要随便访问和操作受保护成员
私有的封装
定义:在成员名称前面加两个下划线 __成员名称
特征:私有的成员只能在当前类的内部去访问和操作,不能在类的外部进行操作</pre>

封装的实现步骤

需要注意:对封装的属性不一定要通过get/set方法,其他方法也可以对封装的属性进行操作。当然最好使用get/set方法,比较标准。

查看对象的成员

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" 
lang="" 
cid="n10" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
# 查看对象的所以成员
#print(ym.__dict__)  # 可以获取当前对象的所有成员信息
# print(Person.__dict__) # 可以获取当前类的所有成员信息
​
#{'name': '扬子', '_age': 28, '_Person__sanwei': '60 55 60'}</pre>
了解:
  1. 在python中并没有实现受保护的封装,属于开发者的约定俗成。

  2. python中的私有化封装是通过改名策略实现的,并不是真正的私有化

继承

什么是继承?

文化的传承,技艺的传承,衣钵的继承。。。

计算机中的继承

在面向对象中,一个类去继承父类,那么这个类就拥有了父类中的所有成员(除了私有成员)

概念:

  • 被其它类继承的类,这个类称为 父类 也叫做 基类 或者 超类

  • 继承其它类的类,这个类称为 子类,也叫做 派生类

  • 继承是类与类的一种关系,是一种“is a”的关系。比如“狗”继承“动物”,这里动物类是狗类的父类或者基类,狗类是动物类的子类或者派生类。如下图所示:

    注:java中的继承是单继承,即一个类只有一个父类。

继承的意义:

提高代码的重用性,建立新的类与类的关系,方便其它逻辑的操作

继承的好处

子类拥有父类的所有属性和方法(除了private修饰的属性不能拥有)从而实现了实现代码的复用;

语法规则

只要在子类加上extends关键字继承相应的父类就可以了:

方法的重写

子类如果对继承的父类的方法不满意(不适合),可以自己编写继承的方法,这种方式就称为方法的重写。当调用方法时会优先调用子类的方法。

重写要注意:

返回值类型

方法名

参数类型及个数

都要与父类继承的方法相同,才叫方法的重写。

重载和重写的区别:

方法重载:在同一个类中处理不同数据的多个相同方法名的多态手段。

方法重写:相对继承而言,子类中对父类已经存在的方法进行区别化的修改。

继承的初始化顺序

初始化父类再初始化子类

先执行初始化对象中属性,再执行构造方法中的初始化。

基于上面两点,我们就知道实例化一个子类,java程序的执行顺序是:

父类对象属性初始化---->父类对象构造方法---->子类对象属性初始化--->子类对象构造方法

下面有个形象的图:

final关键字

使用final关键字做标识有“最终的”含义。

final 修饰类,则该类不允许被继承。

final 修饰方法,则该方法不允许被覆盖(重写)。

final 修饰属性,则该类的该属性不会进行隐式的初始化,所以 该final 属性的初始化属性必须有值,或在构造方法中赋值(但只能选其一,且必须选其一,因为没有默认值!),且初始化之后就不能改了,只能赋值一次。

final 修饰变量,则该变量的值只能赋一次值,在声明变量的时候才能赋值,即变为常量。

继承语法格式

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" 
lang="" 
cid="n35" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
class 父类():
pass
​
class 子类(父类):
pass
</pre>

继承的特征

  • 在不指定继承的父类时,所有类都继承自object类(系统提供) 了解

  • 子类继承了父类后,就拥有了父类中的所有成员包括魔术方法(除了私有成员)

  • 子类继承父类后,并不会把父类的成员复制给子类,而去引用

  • 子类继承父类后可以重写父类中的方法,叫做 重写

  • 子类重写父类的方法,依然可以使用super().父类方法名()的方式调用父类的方法

  • 子类中如果定义了父类中不存在的方法,称为对父类的扩展

  • 一个父类可以被多个子类继承,还可以存在 链式继承

  • 链式继承:A类继承了B类,B类继承了C类,C类继承了D类

单继承和多继承

单继承

单继承:一个类只能继承一个父类的方式。

语法格式:

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded" 
lang="" cid="n60" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
class 父类():
pass
​
class 子类(父类):
pass
</pre>

多继承

多继承:一个类去继承多个父类的方式。

语法格式:

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded"
 lang="" cid="n65" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
class 父亲():
pass
​
class 母亲():
 pass

class 子类(父亲,母亲):
pass
</pre>

菱形继承(钻石继承)

<pre spellcheck="false"
 class="md-fences md-end-block ty-contain-cm modeLoaded"
 lang="" cid="n67" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 
A
 B   C
 D
# D类去继承了B类和C类,然后B类和C类又分别继承了A类,这种继承关系称为 菱形继承</pre>

问题:在这种菱形继承关系中,类与类的关系,及super()调用时的顺序

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded"
 lang="" cid="n69" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">'''
//在定义类后,程序会自动生成一个继承的列表 MRO (Method Realtion Order) 方法关系列表
//MRO列表生成原则:
1\. 子类永远在父类的前面
2\. 同一等级的类,按照子类中的继承顺序摆放
3\. 先子类,后父类的顺序原则,最终的类时系统提供的object类
MRO的调用方法
类名.mro()
'''
C.mro()
# [<class 'C'>, <class 'F'>, <class 'M'>, <class 'HuMan'>, <class 'object'>]
​
# super()在调用时,并不是查找父类,而是去MRO列表上找上一个类。
# super()方法在调用时,会自动把当前self传入到上一级的类的方法中</pre>

类关系检测 issubclass()

issubclass() 检测一个类是否时另一个类的子类

<pre spellcheck="false" 
class="md-fences md-end-block ty-contain-cm modeLoaded"
 lang="" cid="n73" 
mdtype="fences" 
style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
# 检测一个类是否是另一个类的子类
res = issubclass(D,B) # True 检测D类是不是B类的子类
res = issubclass(D,C) # True 检测D类是不是C类的子类
res = issubclass(D,A) # True 检测D类是不是A类的子类
res = issubclass(A,D) # False 检测A类是不是D类的子类</pre>

多态

对于同一个方法,由于调用的对象不同,产生了不同形态的结果

示例:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n78" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 多态 普通版本
# 对于同一个方法,由于调用的对象不同(或者传入的对象不同),最终实现了不同的结果
​
# 定义电脑类
class Computer():
 # 在电脑类中定义一个 sub 的规范的接口 方法
 def usb(self,obj):
 obj.start()
​
# 定义鼠标类
class Mouse():
 def start(self):
 print('鼠标启动成功,可以双击单击嗨起来。。。')
​
# 定义键盘类
class KeyBord():
 def start(self):
 print('键盘启动成功了,赶紧双击666。。。')
​
# 定义 U盘 类
class Udisk():
 def start(self):
 print('U盘启动了,赶紧检查一下我的种子还在不在。。。')
​
# 实例化对象
c = Computer()  # 电脑对象
m = Mouse()     # 鼠标对象
k = KeyBord()   # 键盘对象
u = Udisk()     # u盘对象
​
​
# 把不同的设备插入到电脑的usb的接口中
c.usb(m)
c.usb(k)
c.usb(u)</pre>

引用多态

父类的引用可以指向本类的对象;

父类的引用可以指向子类的对象;

这两句话是什么意思呢,让我们用代码来体验一下,首先我们创建一个父类Animal和一个子类Dog,在主函数里如下所示:

注意:我们不能使用一个子类的引用来指向父类的对象,如:

这里我们必须深刻理解引用多态的意义,才能更好记忆这种多态的特性。为什么子类的引用不能用来指向父类的对象呢?

我在这里通俗给大家讲解一下:就以上面的例子来说,我们能说“狗是一种动物”,但是不能说“动物是一种狗”,狗和动物是父类和子类的继承关系,它们的从属是不能颠倒的。

当父类的引用指向子类的对象时,该对象将只是看成一种特殊的父类(里面有重写的方法和属性),反之,一个子类的引用来指向父类的对象是不可行的!!

方法多态

根据上述创建的两个对象:本类对象和子类对象,同样都是父类的引用,当我们指向不同的对象时,它们调用的方法也是多态的。

创建本类对象时,调用的方法为本类方法;

创建子类对象时,调用的方法为子类重写的方法或者继承的方法;

使用多态的时候要注意:如果我们在子类中编写一个独有的方法(没有继承父类的方法),此时就不能通过父类的引用创建的子类对象来调用该方法!!!

注意: 继承是多态的基础。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容