What is the difference between objects and classes

2019-04-21 04:42发布

问题:

Possible Duplicate:
Difference between object and instance

I have couple of questions:

  1. Every instance of a class (except an abstract class) is an object?
  2. Abstract classes cannot be instantiated, hence they are not objects?

Could anyone help me better understand the above concepts as they relate to C#?

回答1:

  1. An object is an instance of a class.

  2. A class is the definition of an object. It does not actually become an object until it is instantiated. Since an abstract class can't be instantiated, no object of that type can created. A sub class would need to be defined in order for an object to created.



回答2:

  1. Yes, every instance of a class is an object.

  2. Classes (whether abstract or not) are not objects. They are types.



回答3:

There is one common analogy to maybe clearer show what is the difference between the concepts of class and object.

A class is like a recipe. An object is a cake. From one recipe you can build many cakes. A recipe can only contain hints (be abstract) and leave room for your own creation of a recipe (implementing class) from which you can then build cakes (objects).

So a class is not an object, it's the description of what objects of that class look like. Abstract classes contain methods and other information useful for the implementation of classes deriving from this abstract class. Objects cannot be created/instantiated from an abstract class (because it's definition is not complete).



回答4:

class Cat {} // It is a cat. Just a cat. Class is a general issue.

myCat = new Cat("red", "5kg", "likes milk", "3 years old"); // It is my cat. It is an object. It is really a cat. 

yourCat = new Cat("gary", "3kg", "likes a meal", "5 years old"); // It is your cat. Another cat. Not my cat. It is really a cat too. It is an object;

abstract class Animal {} // Abstract class
animal = new Animal(); // It is not correct. What is 'animal'? Cat, dog, cow? I don't know.

class Dog : Animal {} // It is a class. It is a dog in general. 
someDog = new Dog("brown", "10 kg", "likes cats"); // It is really a dog. It is an object.


回答5:

An abstract class can never be instantiated (and so can never become an object). If you create class that inherits from an abstract base class, and instantiate it, it will have properties of the abstract class as well as its own properties. The objective behind creating an abstract base class is to "enforce" deriving classes to implement certain functionality (similar to an interface, but not quite).



回答6:

Every instance of a class (except an abstract class) is an object?

Yes. That's the definition of "instance.

Abstract classes cannot be instantiated, hence they are not objects?

Classes are not the same as instances or objects. An object is an instance of a class. Imagine a zoo simulation. An object is like a specific lion, whereas a class is more like a description of what a lion is. Abstract classes are descriptions of things that are too vague to make it reasonable to build something based on that description alone (e.g. "Animal"). That's why you can't instantiate them. Something like an Animal might be too vague for the program to request directly; instead, the program will only request more specific things like Lion, Tiger, or Mongoose.



标签: c# .net oop class