Java programmer must READ: Basic (4)

Source: Internet
Author: User
Tags define object copy instance method variables variable
Program | programmer Java programmer must read: Basic articlesTime: 2001/09/13 13:31 Author: ZSC Pacific Network College

 
2.3 What is a class

A class is actually a prototype of a type of object that defines variables and methods.

In the real world, you often see many objects of the same type. For example, your bike is just one of many bikes in the real world. With object-oriented technology, we can say that your bike is an example of a Bicycle object class. Usually, bikes have some status (current gear, two wheels, etc.) and behavior (change gears, brakes, etc.). However, the status of each bike is independent and different from other bicycles.

When manufacturers make bicycles, manufacturers use the common characteristics of bicycles to make many bicycles according to the same blueprint. If making a bicycle is going to produce a new blueprint, the efficiency is too low.

In object-oriented software, as well, many objects of the same kind can be used to share some features, such as rectangles, employee records, video clips, and so on. Just like the bicycle manufacturer, you can use the same kind of objects to be similar to the fact that you can create a blueprint for these objects. The software blueprint for an object is called a class.

The class of bicycles needs to define some instance variables to include the current stall, current speed, and so on. This class defines and provides an implementation method for instance methods that allow cyclists to change gears, brakes, and change the tempo of pedals, as shown in Figure 5:


(Figure 5)


When you create a bike class, you can create any bike object from this class. When you create an instance of a class, the system allocates memory for the object and the instance variables. Each instance is defined in the class for a copy of all instance variables. As shown in Figure 6:



(Figure 6)


In addition to instance variables, classes also define variables for the class. A class variable contains information that is shared by all instances of the class. For example, suppose all bicycles have the same number of stalls. In this example, you define an instance variable to hold the number of stalls. Each instance will have a copy of the variable, but the values are the same in each instance. In such cases, you can define a class variable to contain the number of stalls so that all instances of the class share the variable. If an object changes a variable, it changes all objects of that class. Class can also define class methods. You can call the class method directly from the class, but you must invoke the instance method in a particular instance. As shown in Figure 7.


(Figure 7)

Related Article

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.