use*_*468 11 .net c# oop class
可能重复:
对象和实例之间的差异
我有几个问题:
任何人都可以帮助我更好地理解与C#相关的上述概念吗?
jde*_*aan 10
有一个常见的类比可能更清楚地表明了类和对象的概念之间的区别.
一堂课就像一个食谱.一个对象是蛋糕.从一个食谱,你可以建立许多蛋糕.配方只能包含提示(是抽象的),并为您自己创建配方(实现类)留出空间,然后您可以从中创建蛋糕(对象).
所以一个类不是一个对象,它是该类的哪些对象的描述.抽象类包含对从此抽象类派生的类的实现有用的方法和其他信息.无法从抽象类创建/实例化对象(因为它的定义不完整).
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52347 次 |
| 最近记录: |