0
Can someone explain "instance" class?
2 ответов
+ 8
Sure. This is the easiest way for me to explain it so people can better understand.
Think about how we create buildings or other physical objects in real life. You have a blueprint that has the object mapped out, its measurements, and instructions that can be used when it's being built. In programming, the 'class' is the blueprint, and then you create 'objects' from that blueprint. Each individual object you create from that blueprint is an instance of that blueprint. You can do whatever you want to the individual object (instance), and it won't affect the others or the original blueprint that created it.
Hopefully that helps. To sum it up, an object is an instance of a class. The class is the template that lets us easily create those objects whenever we need them.
+ 6
class Dog { // Class definition
}
Dog fido; // Declaration of a Dog object, memory is allocated
fido = new Dog(); // Once the Dog object fido is created "new" you now have an instance of the Dog class. This is where the values to this particular Dog, "fido", can be set.