关于java:JAVA基础面向对象三大特性封装继承多态

27次阅读

共计 12924 个字符,预计需要花费 33 分钟才能阅读完成。

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,在主函数里如下所示:

留神:咱们不能应用一个子类的援用来指向父类的对象,如:

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

我在这里艰深给大家解说一下:就以下面的例子来说,咱们能说“狗是一种动物”,然而不能说“动物是一种狗”,狗和动物是父类和子类的继承关系,它们的隶属是不能颠倒的。

当父类的援用指向子类的对象时,该对象将只是看成一种非凡的父类(外面有重写的办法和属性),反之,一个子类的援用来指向父类的对象是不可行的!!

办法多态

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

创立本类对象时,调用的办法为本类办法;

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

应用多态的时候要留神:如果咱们在子类中编写一个独有的办法(没有继承父类的办法),此时就不能通过父类的援用创立的子类对象来调用该办法!!!

留神:继承是多态的根底。

正文完
 0