当前位置:网站首页>经典面试问题——OOP语言的三大特征
经典面试问题——OOP语言的三大特征
2022-07-26 03:05:00 【陈亦康】
即:面向对象的三个比较重要的思想
目录
封装
官话:将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口进行交互
通俗讲,不让类外看到实现的细节,通过技术手段对这些细节包装一个外壳,同时提供几个公开的接口,让你进行交互即可(例如:手机,内部的具体零件,不会让你观察到,使用者只能看到外壳,通过外壳的显示屏,充电口进行交互)简而言之——套壳屏蔽细节
实际上通过private来实现
例如:

继承
面向对象的思想中提出了继承的概念,专门用来进行共性抽取,实现代码复用。
通俗来讲,就是将两个或多个类(“子类”或者叫“派生类”)的共同的特点抽取出来,放在一个类(“父类”或叫“基类”或叫“超类”)里面 ,对具有共同特点的代码实现重复利用,大大的减少了代码量。
例如:人与人之间具有相同的属性(都有名字,年龄...),但同时也有不一样的地方(每个人具体的特点,有人会弹钢琴...)



父与子的继承顺序:
故名思意,肯定是先有父后有子,所以在子类构造对象时,他们的初始化顺序是先完成父类的初始化,再调用子类的构造方法,{ super()将父类初始化好的内存地址传入 },最后完成子类初始化。
super关键字:
1.super.data访问父类中的属性
2.super.func() 访问父类的方法
3.super() 访问父类的构造方法
注意:
- this()用来调用本类的构造方法,super()用于调用父类的构造方法,这两不能同时出现!
- 父类与子类同名时,在子类调用同名变量时采用就近原则(调用子类)
- 当你未给子类提供构造方法时(或者是父类有不带参数的构造方法,子类也有构造方法,会自动给子类补上super),编译器会自动补上一个不带参数的构造方法(前提是,父类的构造方法中不带参数,因为编译器只会自动不上不带参数的构造方法),如下:
class A{
A(){
System.out.println("A");
}
}
class B extends A{
B(){
super();//如果程序原没写构造方法,编译器会自己提供一个这样的不带参数的构造方法
}
}
public class Test{
public static void main(String[] args){
new B();
}
}class A{
A(){
System.out.print("A");
}
}
class B extends A{
B(){ //程序会自动补上super,最终打印AB
System.out.print("B");
}
}
public class Test{
public static void main(String[] args){
new B();
}
}class A{
A(int a){
System.out.println("A");
}
}
class B extends A{
//若父类构造方法带参数,子类不会自动补构造方法
//程序编译失败
}
public class Test{
public static void main(String[] args){
new B();
}
}经典笔试题:以下代码会打印什么?(初始化顺序是什么?)
class Character{
public int data1;
public int data2;
static{
System.out.println("父类的静态内部类初始化完成!");
}
{
System.out.println("父类的实例内部类初始化完成!");
}
public Character(){
System.out.println("父类的构造方法初始化完成!");
}
}
class Art extends Character{
public int data3;
public int data4;
static{
System.out.println("子类的静态内部类初始化完成!");
}
{
System.out.println("子类的实例内部类初始化完成!");
}
public Art(){
super();
System.out.println("子类的构造方法初始化完成!");
}
}
public class Test{
public static void main(String[] args){
Art art = new Art();
}
}运行结果:

分析:

多态
通俗来讲就是:不同人的通过同一种工具可以做出不同的事情
例如:一个音乐生和一个美术生看到一架钢琴的反应是不同的,音乐生可能会上去弹钢琴,而美术生则可能将他画下来...
实现多态的条件:
- 在父子的继承关系下;
- 子对父进行重写
- 用一个方法调用父类被重写的方法
代码如下:
//人类
class Human{
private String name;
Human(String name){
setName(name);
}
public void action(){
System.out.println(name + "看到一架钢琴~");
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
//美术生
class Art extends Human{
Art(String name){
super(name);
}
public void action(){
System.out.println(getName() + "开始绘画钢琴~");
}
}
//音乐生
class Music extends Human{
Music(String name){
super(name);
}
public void action(){
System.out.println(getName() + "开始弹钢琴~");
}
}
public class Test{
public static void function(Human human){
human.action();
}
public static void main(String[] args){
Human human1 = new Art("美术生");
Human human2 = new Music("音乐生");
function(human1);
function(human2);
}
}分析:

发生重写的条件:
- 方法名相同;
- 返回类型相同;
- 形参类列表相同(个数、顺序、类型都要一致!);
- static 和 private 修饰的方法不能重写;
- 子类访问修饰符必须大于等于父类访问修饰符(后面会出文章专门讨论);
此时会发生动态绑定(运行时绑定),其实此过程编译的时候还是调用父类的,但运行时发生动态绑定,运行子类的;也就是说,父类引用了子类的对象,调用了这个重写的方法,如下图:


拓展:
向下转型,也有,也就是子类当父类用也可,但是比较危险,如下代码:
public class Test{
public static void function(Human human){
human.action();
}
public static void main(String[] args){
Human human = new Human("音乐生");
Music music = (Music)human;
}
}运行结果:

可以这样理解,不是所有人都是音乐生,所以需要如下改法:(有的人是音乐生)
public class Test{
public static void function(Human human){
human.action();
}
public static void main(String[] args){
Human human = new Music("音乐生");//这样写便是有些人是音乐生
if(human instanceof Music) {//保证安全性,向下转型
Music music = (Music) human;
};
}
}
边栏推荐
- From the annual reports of major apps, we can see that user portraits - labels know you better than you do
- cmd cpm 命令汇总
- Study notes of pytorch deep learning practice: convolutional neural network (Advanced)
- Design of golang lottery system
- Convert rich text to normal text
- How to design test cases according to the requirements of login testing?
- File operation (I) -- File introduction and file opening and closing methods
- 手把手教你依赖管理
- [SQL] 自连接的用法
- I hope you can help me with MySQL
猜你喜欢

Information system project managers must recite the core examination site (50). The contract content is not clearly stipulated

对于稳定性测试必需关注的26点

My friend took 25koffer as soon as he learned automation test. When will my function test end?

Nahamcon CTF 2022 babyrev reverse analysis

STM32——DMA笔记

Influence of middle tap change on ZVS oscillation circuit

Parallelloopbody in opencv

案例:使用keepalived+Haproxy搭建Web群集

How to close the case prompt icon of win11? Closing method of win11 case prompt Icon

The difference between the world wide web, the Internet and the Internet
随机推荐
多线程编程
Win11隐藏输入法状态栏方法
Difference between soft link and hard link
[SQL] 自连接的用法
My friend took 25koffer as soon as he learned automation test. When will my function test end?
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
Method of manually cloning virtual machine in esxi6.7
这种动态规划你见过吗——状态机动态规划之股票问题(上)
Execution process behind shell commands
如何正确计算 Kubernetes 容器 CPU 使用率
Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
对于稳定性测试必需关注的26点
[SQL] CASE表达式
图像识别(七)| 池化层是什么?有什么作用?
Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
GoLang日志编程系统
案例:使用keepalived+Haproxy搭建Web群集