[ZT] Dynamic Objects

xiaoxiao2021-03-06  18

Speaking of protected by protected, I inevitable, I will think of a concept - object-oriented. So what is object-oriented? In fact, I personally think that the concept of object-oriented is, it has been developing, and today, the word orientation may make it call the abstract more appropriate. When you have just established an object-oriented concept, it is not very clear that the creator is not very clear about what is object-oriented. To make a clear object-oriented (programming, or design), you may have to see what the past software code is like.

I. Software developed in BC inside the first ten twenty years, basically the process. There are two-oriented core content, one is the control flow, and the other is the data stream. In this period, the biggest development estimate of the software industry is two "subjects" of the data structure and algorithm. Both of these respectively correspond to two "flows". Inside the process-oriented software code, the execution body is a process or function. The process represented by a process is an action, the action of the action (here is still object-oriented object) is some data, and the data may be obtained by the parameters, perhaps through the global variable, there are some constants or predefined values. If we think about it carefully, we will find that this is a "moving" structure system. For example, Basic is more famous "Line (x1, y1) - (x2, y2)", translated into a natural language is "drawing one (X1, Y1) to the straight line of (x2, y2). " Similar examples have a lot, such as "Printf ("% S / R / N "," Hello World! ") In the C language;".

But where is the subject?

II. Inside the century-oriented code does not highlight a subject, many times this subject does not exist, just like the above example, the subject is a screen. But if we need to draw a straight line in the printer? (Or print a "Hello World".) In the process-oriented code, we have to write a printline function. (C language in the file is fprintf.) If we have a straight line to the remote device, then write a remoteline, if ... don't need me, you will feel trouble. Around this problem, people have begun to think: Can I write the subject? Can you make us less duplicity? Later, there was an object-oriented thing, in the world-oriented world, the most of the world, there is a subject, such as "G. Drawline (Pen, PT1, PT2) we are familiar with." Since we have "subject", we can make different things, with similar methods. If the light is changed to h, only solve the problem with "in this window" and "in that window", if we want him to draw in other types of space, we also need to allow the subject to allow the subject. Otamed. However, more problems we have to solve still the same concept, such as the G and Screen of the printer can draw lines, so there is a concept such as inheritance, packaging. Is this what is object-oriented?

III. Reform and opening up with the birth of the object-oriented concept, the spring breeze bathed. As God said that there is light, there is light. God said that there is a poisonous snake, so there is a poisonous snake. God said that there must be apple, so there is apple. As Aapent and Eve have eaten the apple created by God, the apple created by God has been "punished". I really don't understand, since God does not want Adam and Eve to eat this apple, why should I create such a thing? In fact, God creates this apple. Of course, I don't want them to "eat" this apple. Creating this apple is actually in order to produce romantic love and the moving story of the Millennium. If you just see this apple, you will see it next, you will see the painful punishment. If you see a moving story behind, then a good word such as romantic sweetness will be full of your brain. Object-oriented, his core significance is not what you have in packing something, nothing is inherited, and most importantly he allows us to build a software in an abstract way. For example, when we write code:

Stream.write (buff, 4, buff.length - 4); or hashbuff = Hasher.computehash (buff);

Do we need to care about Stream in the end, what is the algorithm for Hasher? If we are started to end, use the same Stream object and Hasher object in the operation of the appropriate thing, whether the task should be able to complete correctly? It should be able to do correctly because this is exactly our expectation. If we let us design a stream, should you consider this class from this perspective? If we define this stream variable, should it be more abstract? Consider such a function: Void DoSomething (FileStream Street, MD5cryptoserviceProvider Hasher, Byte [] BUFF) {...}

If it is written as the following form, it will be more flexible and more compliant with the true meaning of object-oriented (face-oriented): Void Dosomething (stream stream, hashalgorithm hasher, byte [] buff) {...}

In other words, all packages, inheritance, interfaces, etc. are actually present in order to provide abstract capabilities. If we regard protected as a secret that protects "the existence of certain methods", it is wrong. Protecting these secrets strictly say that it should be a cryptographic responsibility, not an object-oriented responsibility.

IV. Reviewing the historical core is an abstraction, but we see that the process of actual development is not the case. We have too many mistakes in the past, such as this object-oriented object-oriented object-oriented, it is too easy to let us think that the core of this technology is object-oriented. So many times we wrote a "object-oriented" program flooded excessive object, flooding inheritance, and unknown package. And many developers, including me, have considered the so-called object-oriented object to encapsulate some elements, and then derive all things from a base class. It is like a base class called object, derives living lives and deaths, live born in bacterial virus plant animals, animals have monkey chicken dogs and people, people have Zhang San Li Siwang, two, (still a baby). Yes, object-oriented can of course include these, but this is not all, not at all. At the time, when we write some things, you don't need to care about the specific object, just know what you should be one. For example, the example in the previous section, DOSMETHING just know that Stream is a stream, and Hasher is a hash algorithm provider is enough. As for the specific stream and haveh algorithm, it should not be our concern, but the user who uses our code. In this way, we can do not consider too much, too specific, and changing problems when designing this section. Think carefully, do we really understand the core of the object? V. Package protection is what object-oriented package is not to protect your secret, but to prevent the use of errors, it is to clearly divide the problem. In terms of "Protection", further speaking, it does not make authorization to use a member (hereinafter referred to as user), but the designer (hereinafter referred to as Designers) Make authorization in the field of extension. Now let's take a look at the example written by Wayfarer:

Class Base {protected void print () {console.write ("this isprotected method in base class!");}}

Class Derived: base {public new void print () {base.print ();}}

Class OtherClass {[Starread] static void main (string [] args) {derived d = new derived (); d.print (); console.readline ();}}

This example is really very easy to confuse, once, I have been troubled by such problems. Before we resolved this trouble, we must first figure out the following two questions: What is protected? What is New?

Protected is a good answer, which means that the member is allowed to be used in derived class, but does not allow the user code to use this class to be used directly. In fact, it is the limited authorization of designers and rejects to users. NEW is not difficult to answer, for example, he is in order to cause a renovated illusion without Override. If you really think so, I went into the illusion of the vortex. In fact, the role of New is not a trick, allowing you to cause a variety of false icons, or attempt to bypass some authorization to use and design. The role of New is just to solve a naming conflict problem, that is, the members specified by New NEW do not work with the work of the base class, just very sorry to renowned himself, this Print is non-PRINT. If you really try to use New to make Trick's words, you will finally hit your front teeth. Before I raise the example of "Impressing your Intogeny", please allow me to give a scene that correctly use the new keyword. I don't know if you have a real study? NET FRAMEWORK INTERFACE? If studied, this problem is not very difficult to answer.

Public interface ifoo {bool bubble ();

Public class boo {public void bubble () {}}

Public class foo: boo, ifoo {public bool bubble () {}} The above code will generate a warning on the foo bubble function, but still can be compiled. Why can I compile? This question is left to the reader. There are two ways to solve this warning: one is explicitly realizing the interface ifoo; but if I don't want to implement the interface through an explicit manner, then I can only add a new modifier in front of the Foo bubble function, tell the compilation I know that they have conflicts, but I still want to choose to use this way. The two bubbles the same name gives us a sense of connection between them, in fact, New Bool Bubble () is close to the meaning of Bool New_bubble (), and Void Bubble inside Boo can be regarded as there is no relationship. If you feel that there is a relationship, then I will give an example to let you touch a nose ash.

Public class boo {public void bubble () {console.writeline ("Boo Sheet");}}

Public class foo: boo {public new void bubble () {console.writeline ("foo sheet");}}

Class Program {static void main (string [] args) {foo obj = new foo (); test (obj); console.readline ();}

Static void test (boo obj) {obj.bubble ();}}

Guess you will say Boo Sheet, or foo sheet? Why do you think about it in this way.

The example of Wayfarer, it is really easy to make confusion, or will make everyone feel that there is a bug that exposes the protected function, but it is not true. I can't remember that I have said before, protected is an authorization problem, not confidential, this statement should be able to partially solve your confusion. And the above Boo Sheet example shows that you don't expose the protected function because you still can't seek the use of the Print function from a base variable pointing to the DeriveD instance. If you still remember the abstract understanding of the abstract, you should realize that the DeriveD class you wrote is just a supplement to the Base class, and the user should generally use the base variable to use your object, not the derived variable, unless He believes that he needs to use Derived to provide features that Base does not provide. If I don't use the new keyword, but the derive's print function named PrintBase, is it a bug that will cause the "exposure" base class member? Obviously not. At this point, if you still can't access the Print with the base variable, you can still access PrintBase (and finally call print) with DeriveD variables. Remember that the derived is the boundaries in the field of extension? Here, a protected function of Base is exposed, which is to extend the boundaries of the problem area. Designing Base is believed that the function of Print is the internal transaction of the object, while the derived designer thinks that the Print function should be a transaction between the external and internal, and whether it is still called Print is not important. (PS: Access to derive class, is not allowed to be exposed, this is an unobedious thing, even if the NEW keyword does not exist. The truly referred to "exposure" is reflected / reflection call. Unfortunately, we still can't call it bug, because this is the "reflection" designed function, not harmful side effects caused.)

Speaking here, I think protected and new problems should have been finished. What is doubts?

Go back to the future and then talk about abstraction, as well as the interface. I have already mentioned the abstraction, I don't know if you have more feelings. What is the abstract head? Of course, nothing is not, not a void Taiji. Abstract essence is to describe a certain set of motions, which makes what kind of function makes it. If we want to think from this angle, it will find that the interface can complete such a task very well. For example, ILIST, it expresses an abstraction such as "a list", which provides a set of corresponding actions, such as "Object this [int index];" can remove or set the Nth item in the list. Only members of the ILIST interface defined can only indicate that this object is indeed called "a list". The word "service" may be able to express the meaning of the above: Class ArrayList: IList {...} This definition shows that ArrayList provides "list" related services. If we use interfaces when defining variables and parameters, rather than specific classes, then our code will have greater degrees. At this time, we have a matter of concern to provide the service I need, but not what he is.

At the beginning of the object, we walked into a misunderstanding in this area, just inheriting to solve the above needs. For example, we can see Stream derive from ISTREAM and OSTREAM in C , which can also solve the problem, may also express the meaning of "service". But I think this still causing many unnecessary troubles, such as name conflicts. Modern theory and even tell us directly that he is not a good thing. If you can replace it with reference, don't inherit, such as Decorate in DesignPattern, etc. What is the true future of future, I don't know. But at least I see the interface-oriented design idea is more advanced than "object-oriented", and people that really think about thinking is much less than those who know how to encapsulate. From this perspective, that is also a future technology, at least in the future, the technology needs to be popularized. (Now CoM is not such a thought?)

转载请注明原文地址:https://www.9cbs.com/read-44209.html

New Post(0)