What's the best way to define the words “class

2019-01-21 13:52发布

My neighbor is taking "Intro to Java", and asked me to help explain a few of the first-day concepts. I realized that since I do this everyday, I don't have the beginner's mind, and it's hard to relate some of this stuff from scratch.

The one that's actually not trivial for me to explain is "what the heck is a class?"


Best I have so far:

  • A variable holds some kind of data; one variable might be a first name, another variable might be your weight in pounds.

  • A method is a function, it does stuff, and can do stuff with those variables. A method might display your name on screen, or tell you how much weight you should lose to have a good BMI ratio.

  • An object holds both variables and methods; one object might represent you, a second object might represent me.

  • A class is kind of the blueprint or template that describes the methods and variables that will be in each object. An object is an instantiated (instance of a) class; an object is something, while the class is simply the plans to make that something.

Continuing the example, we have a Person object, which is instantiated to hold Alice's data, and another Person object instantiated to hold Bob's data, and another for Carol, and so on.


How do I tune this example to make more sense, and/or what's a better approach? The word "instantiated" feels too heavy at this point.

(I think this is a useful question, but is obviously subjective; marked as community wiki.)

29条回答
Explosion°爆炸
2楼-- · 2019-01-21 14:48

If your neighbor is into classical philosophy, classes are Plato's Forms and objects are the things we see everyday that are based on the Forms.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-21 14:48

You might find this talk by Guy L. Steele interesting: http://video.google.com/videoplay?docid=-8860158196198824415#

查看更多
倾城 Initia
4楼-- · 2019-01-21 14:49

class:: Man or Woman

object:: me, you ...

查看更多
Anthone
5楼-- · 2019-01-21 14:49

A java class is the structure or blueprint of an object and object is the instance(memory creation) of a class.

Example: Car is an object(real world entity). firstly, make the structure or design of a Car it's class and when the design of car, to convert real world Car it's an Object. the Car(object) is working now base behalf of design of a car(class).

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-21 14:50

Class: Girl

Object : that girl, this girl, my girl...umm maybe not.

Yea all girls should have the properties of a Girl (class in this case).

查看更多
仙女界的扛把子
7楼-- · 2019-01-21 14:51

Class can be defined as the blue print or template which defines attributes,actions,states for the entity and Object would be the one which would be implementation for the entity.

查看更多
登录 后发表回答