[Javase Learning Note the relationship between]-6.2 class and object

Source: Internet
Author: User

In this section, let's look at the relationship between classes and objects.


We learn the Java language in order to describe things in real life in the Java language. So how do we describe the narrative? This leads to the class, which, when actually implemented, is embodied in the form of a class.

So how to describe the things in real life?

We usually only focus on two aspects in real life, one is attribute, and the other is behavior.

So very natural. The descriptive narrative of a computer produces a detailed object.

For example, we should describe a car. So how do we describe the narrative? By focusing on two aspects above, it is very easy for us to describe the following from two aspects:

Properties: Number of tires, color. Brands and so on.

Behavior: Start, execute, stall and so on.

We use the Java language to implement this descriptive narrative. The following are examples of classes:

Class Car{int num;//This is the tire Number property string color;//which is the color property of string brand;//which is brand void Start () {System.out.println ("car Started");} void run ()//This is the execution behavior {System.out.println (num+ "..." +color+ "..." +brand);}}
The above code is very clear description of a simple car, assuming here, congratulations. Your car is in hand. (a joke)

Through the above class we can see that to describe any thing in real life, only to understand the occurrence and behavior of the thing and defined in the class can be.

So what's the object we're talking about? Here we give a definition of an object that is actually an individual of the real existence of that kind of thing.


So what does the relationship between a class and an object look like?

Class: A descriptive narrative of a thing.

Object: An instance of this class of things. Created in Java through new.

Let's look at a test. Let's drive our own car.

Class Cardemo{public static void Main (string[] args) {//Create an instance of car in the calculation by using the New keyword. Car MyCar = new car ();//mycar is a reference variable of a class type that points to an object of that class. Mycar.num = 4;mycar.color = "Red"; Mycar.brand = "BWM"; Mycar.start (); Mycar.run ();//To use the contents of the object, it may be through the object. The form of the member is called.}}

With the code above, we created an example of a car car, which means we made a BMW car mycar that belonged to us. It has 4 wheels, red, and we can start it, and it's not a problem to go out and walk around with it.



[Javase Learning Note the relationship between]-6.2 class and object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.