Java中的面向对象、继承与接口(封装)

Java面向对象编程有四(三)大特性:继承、封装、多态、(抽象)。今天就从这三个方面进行学习。

最简单的思路,首先,继承,封装,多态是什么意思,然后要了解到,他们到底有什么用。每一项设计都是有意义,有作用的,知道为什么用,有什么用,才能更好的理解。(这点有人总结的很好,what, why, how)

1、封装

What?

GeeksforGeeks:Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Other way to think about encapsulation is, it is a protective shield that prevents the data from being accseed by the code outside this shield.

概念:简单来说,封装就跟其字面意思一样,是将东西包装起来。那么包装的是什么呢?用什么包装起来呢?可以理解为用一个类,在这个类中我们把类中的变量和数据封装起来,让外界无法访问,也看不到。只有这个类自身的方法可以访问这些变量和数据。在封装中,别的类是无法看到这个封装好的类中的数据的,这种情况也叫作data-binding,(数据是封装类中的数据,可以理解为把数据(data),还有类(code)bind在一起了)。

Why?

1. data hiding:封装之后,用户是不知道类的内部实现的。用户只知道我们使用setter方法传进去一个值并且这个值会被初始化而已。(类的内部用户不可见)

2.Increased Flexibiliy: 增加了灵活性。我们可以通过封装,很简单的把类当做(read only)或者(write only)的。不提供set就是read only,不提供get就是write only。而且当有需求要求改变我们的setter方法的时候,我们只需要改变setter方法就可以了。

3.测试和维护很简单。通过封装,就像我们建立了一个一个盒子一样,这样当我们做unit test的时候会更加简单。同时,因为代码都被封装为不同的unit, 当application中某一部分需要改变的时候,我们直接改变那个unit就可以,不会影响到其他的不分。

4.Reusable:封装好的代码可以被一个application或者across mutiple application重用。比如说我们建立了一个class,并且对他进行好了封装,在整个application的生命周期中,或者当其他的application需要相同的class的时候,我们都可以直接reuse这个class来实例化得到我们需要的对象。


How?

大致来说,在java中封装有几种实现的方法。

接口(封装所有的行为),类,access modifiers(访问修饰符?中文应该是这么叫),方法中的setter还有getter(与modifiers一同使用)

具体例子可见自己的代码。

other:(http://www.codejava.net/java-core/the-java-language/what-is-encapsulation-in-java-the-what-why-and-how)

Encapsulation vs. Abstraction

So far you got a proper understanding about  abstraction  and encapsulation in Object-Oriented Programming with Java. To summary:

  • Abstraction is the process of modeling real things in the real word into programming language. In Java, the process of abstraction is done using interfaces, classes, abstract classes, fields, methods and variables. Everything is an abstraction.
  • Encapsulation is the process of hiding information details and protecting data and behavior of an object from misuse by other objects. In Java, encapsulation is done using access modifiers (public, protected, private) with classes, interfaces, setters, getters.

The Truth About Abstraction in Java

  • Abstraction is not about interfaces or abstract classes. Abstraction is the progress of modeling real world objects into programming language. Hence interfaces and abstract classes are just two techniques used in this progress.
  • In an Object-Oriented Programming language like Java, everything is an abstraction: interface, class, field, method, variable, etc.
  • Abstraction is the fundamental concept on which other concepts rely on: encapsulation, inheritance and polymorphism

面试中可能会出现的考察点(如果之后遇到了,可以回来更新这篇博客):

这个部分过于基础,可能面试中的考察并不多,大部分是在考察别的方面时,顺便考察。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值