Interesting Tutorial: Discussing classes, attributes, methods, and instances from a female perspective

Source: Internet
Author: User

I believe everyone has heard of the story of female creation, but the details may not be too clear. I will give a brief description below. Please listen carefully, this has a great relationship with the theme we are going to talk about today.

Nvwa is the goddess of a human being. After pangu was launched, it traveled around the world. Although she is a god, she will also be lonely and look at her beautiful reflection in the calm water. As a result, she decided to crack the mud according to her own morphology. Nvwa is clever, and it will soon be a lot of muddy people. They are almost the same as her, but she gave them a pair of legs to replace the dragon's tail. Nvwa breathed a sigh of relief toward the moles, who were then filled with vigor and "active", turned into a group of small things that can walk upright, speak, and be smart and smart, nvwa calls them "people ". Nvwa injects Yang into some of them-a aggressive male in nature, so they become men; and in others, she injected Yin-a soft female in nature, and they became women. Nvwa wants people to spread across vast land, but she is tired and slow. So she came up with a shortcut. She turned a rope into the mud at the bottom of the river until the bottom of the rope wrapped in a layer of soil. Then she raised the rope and waved it to the ground. where there was a place where mud points landed, those mud points became little people. In this way, nvwa has created people full of land.

This story contains profound program syntax rules. After reading the analysis below, you will surely believe that the author of the story may be a programmer in ancient times!

<The code looks very long because I added a detailed comment>

1. Class: If nvwa belongs to a class, it can be called "God ". Code:
-- God. --

Class God {// class: defines the keyword of the 'class' and describes the characteristics of a group of common objects. static var category: String = "God"; // static: defines the keyword of 'classvariable 'or 'class'. It describes the overall attributes or methods of a group of classes. It indicates that all gods can be called "Gods ". Static var: Object = God; // It indicates who the God belongs to. The ancient Chinese god seems to have no affiliated relationship. Public var: Good and Evil: String; // public: defines the keyword of the 'public' attribute or method, which describes the attributes or methods of the class that can be accessed by external objects. Private var _ name: String; // private: defines the keyword of the 'private' attribute or method, which describes the attributes or methods to be encapsulated by the class, // The variable or method defined by it can only be accessed (called) by methods inside the class or subclass ). // The reason for using private is described in detail below. private var _ shape: String; // Add "_" before the variable name to indicate that it is an attribute with the method of obtaining/setting. private var _ Gender: String; public function set name (n: String): Void {// set: the keyword of implicit setting of class-related attributes in Action Script 2.0. the following describes in detail _ name = n; // The internal method of the class can access the private attribute '_ name'} public function get name (): String {// get: the keyword of the class-related attributes is implicitly obtained in Action Script 2.0. var n: String =" The great god "+ _ name; // The internal method of the class can access the private attribute '_ name' return n;} public function set form (x: String ): void {_ shape = "magic" + x;} public function get shape (): String {return _ shape;} public function set gender (n: String ): void {if (n = "male" | n = "female") {_ gender = n ;}} public function get gender (): string {return _ gender;} function God (sh: String, n: String, x: String, s: String) {// constructor, is the bond between the connection class and the instance. It accepts the externally set parameters and // initializes the instance to be generated. its name is the same as the name of the class containing it. actually, I really think... it can be compared Of 'uterine '.. // sh: Good and Evil, n: name, x: shape, s: Gender Good and Evil = sh; // directly assigned a value for the property of good and evil. Name = n; // call the 'set name' method to assign a value to the '_ name' attribute. Body = x; // call the 'set body' method to assign values to the '_ body' attribute. Gender = s; // call the 'set gender 'method to assign a value to the' _ gender 'attribute. Before assigning a value, the system first determines whether s is 'male' or 'femal '. // It is an advantage of setting attributes implicitly.} Public function loneliness (): Void {trace ("\" I am so lonely. How nice it is to have someone with me! \ ""); // Why not go to the [vive-flash] Forum? Make an advertisement!} Create public function (o: String): Object {// o: created target if (o = "persons") {var clay man = clay man (); var gas = ["yang", "Yin"]; var r = Math. floor (Math. random () * 2); // get a random number, or 0 or 1 var then gas = gas [r]; var person = blow tone (mud, then gas ); // The internal method of the class can call the 'private' method to name (person, "person"). // nvwa calls them 'people '... return person;} // if... of course, God has other creative skills ~} Public function: Object {trace ("dashboard... "); var mud = new Object (); mud. body = "human legs"; // according to the myth above, nvwa is 'snake-shane '... trace ("A clay man has been created! "); Return ;} Name of public function (o: Object, n: String) {// o: Object to be named, n: name o. category = n; trace ("a person was born! ");} Private function blow tone (o: Object, s: String): Object {// o: clay man, s: Gas // This method is indeed a god's private method, I don't understand it all the time. If you can blow your breath out of money... trace ("blow breath... "); // -- the hacker has the following skills -- o. walking upright = function () {trace ("fighting... ");} trace (" the clay man learns to walk upright! "); O. Can speak = function () {trace (" talking... ");} trace (" the clay man learns the language! "); O. Smart and smart = function () {trace (" Being abducted... ");} trace (" the clay man learns to think! "); // If (s =" Yin ") {// if the injected is Yin o. gender = "female";} else if (s = "yang") {o. gender = "male";} // else if youyin Youyang then renshai ~~ Trace ("muddy gender is" + o. Gender + "! "); Trace (" the clay man has the soul! "); Return o ;}}

-- God. as ended --

Here we are going to see the power of our God!

-- Female initiated. fla --

// Write the code on the first line: var nvwa = new god ("good", "nvwa", "female", "female"); // new: generate the instance keyword. It first creates a new object that is anonymous at the beginning, and then sets the features of the new object using the constructor, // finally returns the memory address of the new object. The memory address of the new 'god' is assigned to the 'nvwa variable. Trace (nvwa. name + "yes" + nvwa. form + nvwa. gender + god. class); // call nvwa's 'get name' method and return 'great nvwa '. It is an advantage of implicit property acquisition to control the returned property values. // For example, your boss is Zhang San, but it is often called "ZHANG Zong". This is also the result obtained after necessary control. // The name 'zhang san' is used only by his own talents! Therefore, there are many things in life that can be explained by 'private attribute ~ Trace ("she is" + nvwa. Good and Evil + God. Category); // directly return the attribute value of. Trace ("one day, she thinks"); nvwa. Lonely (); trace ("So she starts"); nvwa. Create ("person ");

-- Female creation. fla is over --

Test it now to see what will happen!

-- Test the start of a video --
The great goddess of God is a magical goddess of snakes and tails
She is a good God
One day, she thought
"I am so lonely. How nice it is to have a person with me! "
So she started
Dashboard...
The creation of a clay man is complete!
Blow breath...
The clay man learns to walk upright!
The clay man learns the language!
Learn to think!
Gender: male!
The clay man has a soul!
A person was born!
-- End of the test video --

So far, our female creation story is half the story, next, let's take a look at how the nvwa can use a rope to get rid of others (this is indeed more profitable than one by hand), that is, class inheritance. I hope you will not wait too long :)

In the previous article, I talked about the loneliness of nvwa. I squeezed out a "person" who could walk around and use the dirt, but I forgot to explain how the "person" talent really works. Before nvwa starts Mass Creation, let's "man" a stage for us to show his talents!

-- Female initiated. fla --

Var nvwa: God = new god ("good", "nvwa", "snake tail", "female"); trace (nvwa. name + "yes" + nvwa. form + nvwa. gender + god. category); trace ("she is" + nvwa. good and evil + god. category); trace ("one day, she thinks"); nvwa. loneliness (); trace ("So she starts"); var primitive: Object = nvwa. create ("person"); // assign the created "person" to the variable 'Primitive person '. Then, you can use the 'Primitive person' to access his attributes and methods. Trace ("---- primitive people start to show their talents ----"); trace ("primitive people are" + primitive people. gender + primitive person. class); trace ("primitive human form is" + primitive human. form); trace ("the original man will walk upright, now he (her) in:"); primitive man. standing upright (); trace ("the original man can speak, now he or she is at:"); primitive man. can speak (); trace ("the original man is smart and smart, now he (she) is:"); primitive man. smart ()

 

-- Female creation. fla is over --

How are we doing? Now let's test:

-- Nvwa startup. fla test started --

The great goddess of God is a magical goddess of snakes and tails
She is a good God
One day, she thought
"I'm so lonely, how nice it is to have someone with me! "
So she started
Dashboard...
The creation of a clay man is complete!
Blow breath...
The clay man learns to walk upright!
The clay man learns the language!
Learn to think!
Gender is female!
The clay man has a soul!
A person was born!
---- The original man began to show his talents ----
The original man is a woman
The original human form is a person's leg
The original man will walk upright, and now he or she is:
Fighting...
The original man can speak a word. Now he or she is:
Talking about love...
The original man is smart and smart. Now he or she is:
Being abducted...

-- Nvwa leader. fla test ended --

How do you feel when you see the test results? Well, it's still 'people's beginning, nature is evil ', right?

Let's get down to the truth. It is said that nvwa wants to make people spread across vast land, but there is little hope for them to be made one by one like her. In addition, we can also see that although each person looks different, their souls are similar, and they are all taken by nvwa's breath. If everyone needs to take a sip from nvwa, it will be a big blow if they are not exhausted! What Should nvwa do? It is said that the nvwa has taken a rope and dumped it to create a lot of people. It seems that they have to start with this 'string.

This rope is so unusual, I think it is the result of nvwa putting the template of creation into it. Legend has it that nvwa is a person created based on her own appearance. It is certain that 'people' must have inherited many features from God.

-- Start with person. --

Class owner extends God {// extends: defines the key words of the inheritance relationship between the class and the class. The subclass can retain or discard the features of the parent class. Static var category: String = "people"; static var: Object = God; // No, people are controlled by God. static var: array = ["yang", "Yin"]; static function sets gender (): String {var immediately = person. gas [Math. floor (Math. random () * 2)]; if (then gas = "Yin") {return "female";} else if (then gas = "yang ") {return "male" ;}} public function set form (x: String): Void {// after all, the human and the god form are very different, and the god form is ever changing, // The human body is similar in general, so the child class overrides the 'set form () 'method of the parent class. // x: The shape. if (x = undefined) {x = "Normal";} // If the detailed description of the 'body' is not provided, the default value is "normal '. _ = X + "";} public function get (): String {// This method is a bit strange. It should have been inherited directly, but I have to write it again for unknown reasons. Or is set and get paired? Return _ shape;} function person (x) {// The Birth of a person cannot be compared with the birth of God. Rewrite the constructor // x: Form // compared with God, the human constructor has only one parameter, because the good and evil of a person are related to the later growth process. // gender is determined by natural rules, the name was later named by someone else... only the body is born with heaven. // good and evil = undefined is written here to remind everyone that good and evil are actually undefined. // name = undefined form = x; Gender = person. set gender (); // call the class method to initialize gender trace ("a person is born! ") ;}/// The following is the inherited method of public function loneliness (): Void {// The Person Who is lonely, all gods. However, the loneliness of 'people' expands to super. loneliness (); // super: a keyword pointing to a parent object. Because 'people' share the same thing with the loneliness of God, the 'loan' method is called first. Trace ("even chatting with me! "); // God will convert 'loan' into 'create', but a person will convert 'loan' into 'having chat '~~} Public function creation (o: Object): Object {// a person is not as creative as a god... override the 'create' method if (o = "fire") {var o = new Object (); o. name = "fire"; o. bake = function (oo: Object) {trace (oo + "baked! ") ;}; Trace (" invent the opportunity to get fire! "); Return o;} // other creative power... slightly} // public function ()... there is no difference between a person and a god's 'dash'. It fully inherits the public function (o: Object). {// The Private method becomes the public method. // alas, generally, sub-classes expand or enhance the features of the parent class, but the sub-classes from God to people are a little special. // they expand only some small functions that are boring, the big power like this is not inherited! If you blow your breath out of silver... if (o = undefined) {// if there is nothing to blow trace ("Ah, this sleep well "); // wait for a while.} else if (o = "Candle") {trace ("candle burned out"); // blow the candle ~} Else if (o = this) {// this: the keyword of the object. // If the blowing object is a self-trace ("Where is it? I know a little... what do you want to ask? "); // Alas, I will inevitably blow my own ~} Else if (o instanceof people. Owner) {// if the blowing object is the master of humans, trace ("no father or mother" + o. Name + "dear! "); // Yes, this is another shot ~} // The following is a human's own method: public function walking upright () {trace ("fighting... ");} public function () {trace (" talking... ");} public function smart () {trace (" Being abducted... ");}}

// -- End with person. --

Now the "creation movement" has come to an end. Let's see how it works:

-- Female creation. fla --
(Appended after the original code)

Var ancient people = new people ("beautiful"); ancient people. name = "ancient man"; // assign values to the attributes of the Instance's own name to ancient man. hunting = function (o: String): Void {trace ("shot one" + o + "! ");} // Define your own method trace for the instance (" ---- ancient people start to show their talents ---- "); trace (ancient man. name + "yes" + ancient man. gender + person. category); trace (ancient man. the name + "is" + ancient man. form); ancient man. lonely (); trace ("ancient man will walk upright, now he (her) in:"); ancient man. standing upright (); trace ("ancient man can speak, now he or she is:"); ancient man. can speak (); trace ("ancient smart and smart, now he (she) in:"); ancient man. smart and SMART (); ancient man. clay man (); ancient man. blow breath (); ancient man. blow breath ("Candle"); ancient man. blow breath (ancient man); ancient man. blow breath (nvwa); ancient man. hunting ("deer ");

-- Female creation. fla is over --

If you want to see how the ancient man is doing, test it on your own. So far, our story is almost finished. Nvwa is now very easy to create people. You only need to "new people ()", and you don't need to take a sigh of relief. If you are energetic, "new People ('Healthy ')". If you are physically unhealthy, "new People ('weak ')". If you understand the Inheritance Mechanism of Action 1.0 syntax, you must know that the instance methods in the class are written on the class "prototype" (that is, prototype, in this way, instances created by the class can share the same method. In the class we just created, The Methods Modified by the "public" or "private" keyword are actually written on "prototype. Therefore, nvwa no longer needs to define "methods" for every "person", that is, "blow breath" for everyone.

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.