我有几个问题:
任何人都可以帮助我更好地理解与C#相关的上述概念吗?
答案 0 :(得分:24)
对象是类的实例。
类是对象的定义。在实例化之前,它实际上不会成为对象。由于无法实例化抽象类,因此无法创建该类型的对象。需要定义子类才能创建对象。
答案 1 :(得分:12)
是的,类的每个实例都是一个对象。
类(无论是否抽象)不是对象。它们是类型。
答案 2 :(得分:6)
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.
答案 3 :(得分:2)
永远不能实例化抽象类(因此永远不能成为对象)。如果创建继承自抽象基类的类并实例化它,它将具有抽象类的属性以及它自己的属性。创建抽象基类背后的目标是“强制”派生类来实现某些功能(类似于接口,但不完全相同)。