I'm a beginner in java, and I've gone through various definitions of OOP concepts, but I've never really understood the concept of a class and object, so could someone please explain this to me. and please don't just say an object is an instance of a class , what exactly does that mean? examples would help
5 回答
In most OOP languages, an "object" is a data-structure upon which you can perform a fixed set of actions or operations and a "class" is the means used to define what data an object contains (or has access to) and what operations it can perform or can be performed upon it.
Object
- An object is a thing, both tangible and intangible, that we can imagine.
Class
- A class is a template definition that dictates what objects can and cannot do
- A class is a prototype from which objects are created
Okay first off I'd recommend reading The Java (tm) Tutorials, wouldn't hurt watching videos on Udacity too.
public class Student {
private int id;
private String major;
private Integer age;
public Student(int id, String major, Integer age){
this.id = id;
this.major = major;
this.age = age;
}
public int getId() {
return this.id;
}
public String getMajor() {
return this.major;
}
public Integer getAge(){
return this.age;
}
. . .
}
So an object in this instance is the data type Integer
and the primitive object is int
. Each of id
, major
, age
are instance variables of Student
. Constructors are used to create objects, each time you initiate a constructor you are creating a new instance (every instance is different from the last).
A Class is a definition of some thing. An Object is one of those things.
One of the most common examples is a car. Say you have a class Car
. That class defines everything about the car. Size, color, make, model, and other things that describe a car are called properties.
A car can also do things, like move, accelerate, turn, reverse, shift. These are called methods. They are what the car can do (some languages call these "actions", which I think is a better term).
When you combine properties and methods into a group, you would call that a class, because together they define a car (or whatever you are defining).
An object is an instance of a car. That is the simplest way to put it. But what is an instance? According to wikipedia:
In object-oriented programming an instance is an occurrence or a copy of an object.
That probably seems cyclical, calling an instance an object and vice versa. But technically, an object is a location in memory having a value that can be referenced elsewhere. To make things simpler, we combine the two to mean the same thing. An instance is an object, an object is an instance.
Make it Short
Perhaps the shortest way I can put it is like this:
A class is a definition, and an object is something that fits that definition.
Applications in Java
Since you tagged your question with Java, I will continue my example in Java. Let's go back to the car.
The code for what we described above could look something like this:
class Car {
/* Properties */
int size;
Color c;
String make;
String model;
/* Constructor */
Car() {
// constructor code...
}
/* Methods */
void move() { }
void accelerate() { }
void turn() { }
void reverse() { }
void shift() { }
}
I've labeled each group of things here. We have the properties at the top; the things describe a car. And we have the methods at the bottom; the things that a car can do.
But in the middle I put something else. It is something that is required to have in almost every class: the constructor. What a constructor does is it instantiates a class into an object.
Using the definitions I made above, we can rewrite that last sentence like this:
A constructor puts an occurrence of a class into a location in memory.
Hopefully this helps you understand classes a little better.
A class can be seen as a definition of an object; the idea of what makes up a type of object. The object is an example of that class.
Take a (real-world, not the OS) window, such as the one I'm looking out right now. "Window" is a class - it has a defined operation (open()), and properties (height and width). This particular window I'm looking out of (this instance of a Window; this object of the Window class) can be opened by turning a crank, and it is ~60 inches high and 30 inches wide. In java:
class Window {
int width;
int height;
void open() {
}
}
Window myWindow = new Window(); //instance of the class
myWindow.width = 30;
myWindow.height = 60;
myWindow.open();