java foundation - talk about your understanding of object-oriented

 

I. Introduction

This article follows the core knowledge, mainly to help you better understand object-oriented programming; Here Insert Picture Description

Two object-oriented process oriented VS

 

2.1 process-oriented programming

 

Process-oriented programming (Process Oriented Programming) which is meant to be process-oriented programming , what ?, what is the process?

Here Insert Picture Description

 

Process-oriented programming is actually implemented around the execution of the code ; we now want to draw an analogy I did not wash yesterday's clothes to wash (joke, this is definitely not me, I use pure honor !!!), I to perform the following steps:

Clothes poured into the bucket; pouring water into the bucket; Nei adding spices laundry detergent and wait for 30 minutes scrubbing brush .....

 

That turned the code is:

1 // This is a wild way --- fitted dress 
containClothes () .......
// 2 This is a wild way ----- pour
pourWater () ......... .
// 3 this is a wild approach ------- pour liquid detergent
pourWashingLiquid () ............
// 4 this is a wild way ----- scrubbing brush
washClothes () .............

Good trouble ah, I wash clothes turned out to perform such steps, admire the older generation what scrubbing brush ability

Here Insert Picture Description

 

2.2 Object-Oriented Programming

OOP (Object Oriented Programming, OOP) which means that object-oriented programming , slightly slightly ...... This is not nonsense it, yes ah phrase is nonsense, we need to understand the core concepts of what is an object?

Here Insert Picture Description

 

对象这个名称其实很普通啊,比如你家里人经常会说,孩子啊,最近有对象了,奶奶等等着报孙子里,...........,这是真的么?难道java中面向对象编程就是指生活中相处的对象?那我编写个女孩子对象,是不是就有对象啦,呵呵............,你想的真美好(跟代码生孩子去吧!!!);

 

面向对象中的对象其实就是面向事物,《Think in java》中有一句话叫啥来着?万物皆对象...额,一切皆对象...额,没错啥都是对象;好吧如果你是初学者去面试,就别吹这就话了,你想跟老程序抢饭碗啊!!!

 

Here Insert Picture Description

面向对象编程我们已经理解了,就是类似根据生活中中的事物编程,嗯?回到刚刚洗衣服的话题上,生活中的事物编程.......聪明如我这样的孩子已经想到了,本宝怎么会自己洗衣服呢!!!太low了,我要叫一个对象来给我洗衣服,我就不叫其他人帮我洗了,会遭天妒,还是叫最普通的把,比如洗衣机这个对象;

 

我把衣服倒进洗衣机; 我把洗衣液倒进洗衣机; 开启启动程序,洗衣机麻烦你洗洗衣服啊哈哈;

可以看见我们简化了一些步骤吖,不用拿桶了,不用自己洗衣服;转换成代码就是这样

 

// 1 知识追寻者这个对象的方法---倒衣服
zxzxz.pourClothes()........
// 2 知识追寻者这个对象的方法---倒洗衣液
zszxz.pourWashingLiquid()............
// 3 洗衣机这个对象的方法-----洗衣服
WashingMachine.washClothes().............

面向对象中的对象在java编程中就是Object,每个对象都有自己的类型也就是Class,你可以这样理解,一个类型有多个对象实例(instance),比如,洗衣机有小天鹅洗衣机对象,海尔洗衣机对象;

 

三 抽象

面向对象的本质其实就是抽象;啥是抽象啊,好深奥的感觉,膜拜当年的大佬用了这么高大尚的术语;抽象我们通俗的理解就是将具体的功能和行为属性抽象出来看待成一个实体;说了这么多,你可能还是一脸懵逼........

 

Here Insert Picture Description

 

就以洗衣机为例子,我现在要将洗衣机有容纳东西的功能,有排水的功能,有洗衣服的功能......好了这么多功能和属性我将它们都抽象出来形成一个实体;那么代码就是如下

public class WashingMaching {

public void contain(Object obj){
  // do contain
}

public void drainWater(){
  // do drain water
}

public void washingClothes(Clothes clothes){
 
}

}

四 创建对象

 

说了那么多关于对象的事情,但是永恒的主题你要告诉我对象是怎么创建的吧, 看如下代码,创建对象是通过 new 这个关键字创建;我们创建了一个对象叫做 wm,也就是实例,它属于 WashingMaching 这个类;执行drainWater()这个方法,表示实例wm执行了其成员方法;

 

// create object
WashingMaching wm = new WashingMaching();
// invoke method
wm.drainWater();

Five three principles of object-oriented

 

 

5.1 Package

In fact, the package is to attribute the behavior of objects hidden, so free from outside interference ; I was cold child, do not use your dirty thoughts I guess! ! ! I inherent magical abilities you never know; package hidden purpose is to prevent being damaged by other objects ; just get along from person to person, there must be space for privacy, do not you are exhibitionist? ? ?

Here Insert Picture Description

 

5.2 Inheritance

 

Inheritance is a subclass inherits the parent class, subclass will have all the attributes of the parent class; say a joke, a certain young man was wandering the streets, suddenly one day an army surrounded him, on a little fat man told him that the emperor driving collapse you, you are the only son of the emperor, fast back to the palace for the emperor ascended the throne with me, inherit the family business now! ! ! Are single inheritance in java, all the objects have a parent class is Object; use extends keyword indicates inheritance; to add a new method can be extended objects function in succession, override the parent class method can be implemented subclass-specific features;

Here Insert Picture Description

 

More than 5.3 state

 

Polymorphic object can represent a plurality of status shows that the state of the object can be interchangeable between the sub-classes and superclasses; Here is a simple chestnut: small droplets, dew, rain, mist, essentially water, we believe that they have inherited water; water droplets can be converted into water, this is the upward transition (ie parent close), now small water droplets on the performance of the two states, one is the state of water, one is small droplets of state; if you want to convert water into the morning dew, the transformation is down (closer to the sub-class), but under the restructuring is risky, morning dew is water, the water will certainly do just the morning dew in this state it? Obviously not, water can also be converted to raindrops, mist, etc. ....., usually downcast is to be done to determine the type, use the keyword instanceof judgment;

Here Insert Picture Description

 

 

 

 

Guess you like

Origin www.cnblogs.com/zszxz/p/12057962.html