Guess the number game:An object of the class difference

A lesson on our first taste OOP Style,Ambiguous classes and objects in exactly what is it? This lesson we use a simple little chestnut to introduce to you。

Trying to explain

We say that the relationship between classes and objects and the relationship between the design of products,Take Our house is,Certainly there will be a corresponding building design,But the design will not only correspond to a building,At least one cell must be met with a set of drawings is not it? Our "Class" is the "design"。We use this design designed object's properties、Function and so a series of content,Then to produce objects of this class by instantiating。

For example, we created a "car" category,Then we can instantiate a different object,For example, a car truck,Car is car。

Guess a small program

We used to use a specific small program like you explain the difference between classes and objects - a main game class,Guess a player class:

The next is the player's class:

Both classes get,However, the compiler found no movement - it does not matter,Because we have not told the inlet system program。

Find the main file,Come on we instantiate the main game:

Such,Probably there will be the following results after running - because we generate random numbers,Therefore, implementation of the results will be exactly the same every time。

summary

You see,We are at GuessGame Class players actually generates three objects,But in fact we are all generated from the same class,We put three players object called "Example”,They are Player This class generated,But running them in actual,Due to their different respective properties,But not exactly the same - that isObjectsAndclassDifference。

 

After-school title

Here is a code file is corrupted,We also recorded the results of running at the time of the output,Now you need to put it filled。

Remember that each line can only write a code yo ~ otherwise even cheating!

Here are the results after the execution of code:

……correct,If the result is not 10,But 24,That code is how should be amended?

 

Original article written by LogStudio:R0uter's Blog » Guess the number game:An object of the class difference

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1108.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

  1. import Foundation

    class Echo {
    var count = 0
    func hello() {
    print(“hellooooo…..”)
    }
    }

    var e1 = Echo()

    var e2 = Echo()

    var x = 0

    while x < 4 {
    e1.hello()

    e1.count += x

    e2.count = e2.count + e1.count
    x += 1
    }

    print(e2.count)

Leave a Reply

Your email address will not be published. Required fields are marked *