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条回答
戒情不戒烟
2楼-- · 2019-01-21 14:32

"Car" is a class. My car, sitting in my driveway, is an instance (object).

查看更多
来,给爷笑一个
3楼-- · 2019-01-21 14:32

class is a blueprint/template which you use to create objects. An object is an instance of a class.

enter image description here

查看更多
Fickle 薄情
4楼-- · 2019-01-21 14:32

We can also understand the concept of class and object as: as a class is a template so lets have following two examples: Example 1: a recipe of a cake is a template so its a class and cakes that are made following recipe are the objects. Example 2: A brick maker is a class and bricks are objects

查看更多
小情绪 Triste *
5楼-- · 2019-01-21 14:32

Your question details have pretty good definitions of all the terms. Here's an analogy I found pretty useful - I've listed it in a kind of top-down approach:

class Employee. This is a kind of, as you say, 'blueprint' or 'template' that contains generic details about all kinds of employees in an organization - let's consider the director, a project manager, a developer, a contractor, or a member of the housekeeping staff. They're all employees : hence, they're all instances of the class, or objects.

All the objects have certain attributes in common - they're all allotted, say, an employee ID. They all draw a salary. They all have a designation. One could call these the member variables, as they exist for all objects, but their values are clearly different based on what object they're members of.

And finally, there are some functions that need to be performed for all employees - say onBoarding() or calculateSalary().

查看更多
叼着烟拽天下
6楼-- · 2019-01-21 14:33

Panda DNA is a class. A Panda running around, eating and performing Panda-like activities is an object.

查看更多
Emotional °昔
7楼-- · 2019-01-21 14:34

I always define them as blueprint and product.

A blueprint describes the complete product in every detail, the product is the result that comes out of the machine.

查看更多
登录 后发表回答