[Javase Study Notes]-relationships between classes and objects
This section describes the relationship between classes and objects.
We learn the java language to describe things in real life using java. So how can we describe it? This introduces the class, which is embodied in the form of classes in actual implementation.
So how do we describe things in real life?
In real life, we usually only focus on two aspects of thing description. One is attribute, and the other is behavior.
Naturally, computer descriptions produce specific objects.
For example, how do we describe a car? Through the above two aspects, we can easily describe the following two aspects:
Attribute: Number of tires, color, brand, etc.
Action: Start, Run, stop, and so on.
We use the java language to implement this description. The class is as follows:
Class Car {int num; // This is the tire count attribute String color; // This is the color attribute String brand; // This is the brand void start () {System. out. println ("car started");} void run () // This is the running behavior {System. out. println (num + "... "+ color + "... "+ brand );}}The code above clearly describes a simple car. If so, congratulations! Your car is ready! (Joke)
From the above class, we can see that to describe any thing in real life, we only need to clarify the occurrence and behavior of the thing and define it in the class.
So what are the objects we are talking about? Here we provide a definition that the object actually exists as an entity.
What is the relationship between classes and objects?
Class: The description of a transaction.
Object: the instance of this type of thing. It is created in java through new.
Let's look at a test to open our own car.
Class CarDemo {public static void main (String [] args) {// create a car instance in the calculation using the new keyword. car myCar = new Car (); // myCar is a reference variable of the class type, pointing to the object of this class. myCar. num = 4; myCar. color = "red"; myCar. brand = "BWM"; myCar. start (); myCar. run (); // the content of the object to be used, possibly through the object. member Form to complete the call .}}
Through the above Code, we created an example of a Car, that is, we made a BMW Car myCar of our own, which has four wheels, red, and we can start it and drive it out for a lap.