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条回答
forever°为你锁心
2楼-- · 2019-01-21 14:55

class == cookie cutter, object == cookie.

查看更多
女痞
3楼-- · 2019-01-21 14:55

If they are learning to program OO have them use BlueJ. They should get the differences after walking through the first tutorial.

You define the classes and when you instantiate them they actually appear at the bottom of the GUI at which point you can call methods on them.

It really helps get the point through better than any analogy you want to try. Even if you nail the analogy, it doesn't translate into code for someone who hasn't learned OO yet (even though for all of us it seems really natural and all these descriptions make great sense.)

查看更多
在下西门庆
4楼-- · 2019-01-21 14:56

An Object is a group of related data and functionality. What the group of data and functionality will consist of is defined in the class. Class is the design or specification of the object.

查看更多
倾城 Initia
5楼-- · 2019-01-21 14:57

Class : Object :: Blueprint : Building

查看更多
Emotional °昔
6楼-- · 2019-01-21 14:58

I would highly recommend telling him/her to buy a copy of a book called The Object-Oriented Thought Process by Matt Weisfeld. It's a really good conceptual introduction to OO. I've lent out my copy to a few people who were just getting into OO and they really liked it.

查看更多
登录 后发表回答