Encapsulation interpretation of the [discussion] function

Source: Internet
Author: User
Tags modify reset advantage
Encapsulation | function dingding Let me talk about what is called function encapsulation??
I can't quite say it myself ... Can only do self understanding ^ ^
In fact, encapsulation, the literal interpretation is sealed up and installed, in fact, it is indeed such as a function MyFunc (a,b) {
return a+b;
It is obvious that the use of MyFunc is to return the number two and
So
MyVar = MyFunc (1,2); MyVar is equal to 3. This is done in the encapsulation, because when you want to get two numbers added, you just call MyFunc (); and then you have to add two of the number can be, the specific myfunc is how to add, you do not have to know. This is the effect of encapsulation. Of course the addition who will do it, So obviously a bit of grandstanding ^ ^ "that if you calculate by two resistors in parallel circuit, the total resistance is how much??
OK, if your electricity is good, you don't want to know about it, but your electricity is the same as mine, or bad. Let's just use someone else's encapsulated functions. function resistance (R1,R2) {
if ((R1+R2) ==0) Trace ("Error: Denominator cannot be ' ' 0 '");
else return r1*r2/(R1+R2);
} r=resistance (5,10); That way, you don't have to worry about the electricity formula that's killing you, just call resistance () and pass the two resistor values you know. ^ ^ How, encapsulation is not very good understanding, the image is: You put a set of data to a black box, after the black box to get another set of data , the black box is the encapsulated function and the entrance of the black box is the parameter of the function .... You just have to know what the entrance is, how do you manage the black box?
OK, say almost, do not understand in ask, my ability can only express so little content: D
ding8285
The general purpose of encapsulation is that once you have written a function, you can use this function wherever you go,
Just know the interface is OK, do not need to know how the function is done, such as you will not use gotoAndPlay (); It? Yes, he's a. MM company encapsulated functions
But you don't know exactly how to do it. Next, the concept of the interface, or with gotoAndPlay (), you will tell me how to use?
Oh you'll tell me gotoAndPlay (1)
So the 1 inside is the parameter of this function of course, besides this parameter, do you know gotoAndPlay () other parameters?
You'll answer I have such as gotoAndPlay ("XX frame label") gotoAndPlay ("xx scene")
Because of these parameters, this function is the external interface we only know gotoAndPlay (10) The timeline jumps 10 frames to play
and gotoAndPlay () How it works inside, we don't need to know,
As long as we know what the interface is and what we can do with this interface. For example, you wrote a function
function Movetoright (MC) {
mc._x++;
Movetoright is the encapsulation function, MC is the external interface
This function you get anywhere, just give it a MC parameter can be passed
Movetoright is the function name MC is the parameter below we give an example to say the difference and advantage of encapsulating functions and not encapsulated functions
function ReSet () {
_root.mc._alpha = 50;
}
ReSet ()
or Mc.onenterframe = Movetoright, is the effect of the alpha of the MC is achieved?? Is this method good?
You might say, "nothing bad." First declares a function and then calls that function. The idea is very clear!
But does it really feel good?? Isn't there a bad place??
Oh, actually it is not good why?
Imagine, if your MC's name suddenly want to change, change to MYMC, you still want to achieve the effect you want, what should you do
Do you want to find this function and replace the MC with MYMC?
Change 2 places First: Frame second: The name of the MC
And then you suddenly find that to make the other 5.6 MC in the scene to achieve the same effect, how to do?
is not repeated in the function plus _root.mc1._alpah=50;_root.mc2._aplha=50;
Isn't that a stupid way? If you're smart, OK, you'll think of creating a loop inside a function
for (var i = 0; i<5;i++) {...} hehe but when you have more than 5, 6, is it going to change again?
In the end, you have a slightly changed scene, you have to go to find the Reset function, and repeatedly modified
In other words, this function is not useful in any other file, but also for other source files.
and change, in the end just for the scene of some MC transparency to 50, it is necessary to constantly modify the function content
If you admit that this is silly, then you have to learn the simplest way to encapsulate a function, you create a parameter for the function (note: Here the MC is the parameter, not the reference to the instance in the original scene)
function ReSet (MC) {
Mc._alpha = 50;
So where do you want the MC Transparent for 50, as long as the name of the MC passed through the parameters can be for example, your scene has 6 MC, named MC1~MC6;
ReSet (MC1);
ReSet (MC2);
ReSet (MC3);
ReSet (MC4);
ReSet (MC5);
ReSet (MC6);
Even if you have 600, don't be afraid, use a loop to get it done
for (var i = 1; i<=600;i++) {
ReSet (this["MC" +i]);
}
So, this function will live, even if you end the project, the next project, still use the
Set the transparency of some MC to 50, just use this function ok
This function is called the encapsulated function, the parameter MC, is your external interface
No matter how the MC of your scene changes, from beginning to end, you don't need to change the contents of the function------------the concept of encapsulation, and the usefulness of encapsulation, as well as the benefits of encapsulation, everybody understand
Well, maybe you don't get it. Let's take another example of an abstract point. For example, if you're moving, create a moving function.
function Move () {
sofa, Position = against wall;
bed. Position = corner;
table. Position = by window;
}
When you have a chair, do you want to find the moving function, and then add the chair. Position = by door??? (The position is casually said)
Again, if you think the table is wrong, you have to modify the table. Location = where??
Is it too troublesome to do so, if you are a decoration brigade, each family's furnishings can not be the same,
Do you have to change your moving function every time, so it's a stupid idea?
If you decorate 50 families, then when one of them problems, want you to redesign, you have to find, corresponding to its moving function to modify
But...
function move (furniture, location) {
Furniture-location = place;
}
Does that solve the problem?
Move (table, window);
Moving (bed, corner);
Move (chair, door);
....
In fact, the meaning of encapsulation is to keep the things that don't change and expose the things that change
The advantage of encapsulation is that it can make the program durable, multi use, maintenance convenient, etc.
The move function above, no matter you move 50 families, 100 families, with that a function, it is all done, is not to facilitate a lot of
If you are decorating the company's designer, such a plan must be the big hand praise ... Before that, do not say also know, must be fired, hehe
You designed the plan, and of course you need someone to follow it.
Move (table, window);
Moving (bed, corner);
Move (chair, door);
....
All right, here's the last word.
"Encapsulation is a concept, is a good program idea, is a benign programming method"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.