0

What is an instance?

It is really confusing me i cant really imagine anything under an instance. Thanks for the help

13th Mar 2017, 2:03 PM
S0muchM3NY
3 Answers
+ 2
object
13th Mar 2017, 2:57 PM
Meharban Singh
Meharban Singh - avatar
+ 1
You have classes and instances (of classes) At first, your Class is like a blueprint (for example a car) and an instance is an object you created FROM the class. Example with cars in C#: //This is your blueprint (class) class Car { string manufacturer; string color; int power; } //This are your instances (objects) new Car BMW; new Car Audi; new Car Subaru; So basically an instance is an object from a class. After instanciating you can access variables and methods from your instances with the "."-operator. For Example: Subaru.color = red;
13th Mar 2017, 3:38 PM
Hoargarth
Hoargarth - avatar
0
to throw in a python reference as you tagged python: class MyTestClass(object): def __init__(self, name, age): self.name = name self.age = age # just a function for an example def older_than(self, age): return True if age < self.age else False john = MyTestClass('John', 34) albert = MyTestClass('Albert', 21) print(john.age) # Prints 34 to screen print(albert.name) # prints Albert to screen print(john.older_than(albert.age)) # prints True as John is older than Albert Both 'albert' and 'john' are objects which are instances of the MyTestClass. You can create as many instances of a class as you require. the dot(.) notation is used to access members of the object instance. In this case each instance has a name and a age and also a function which compares their age to some other age. In our example we checked if john was older than albert. (ignore the return function for this if you are beginning i just kept it small not to pollute the example.)
14th Mar 2017, 2:53 AM
Elric Hindy
Elric Hindy - avatar