Classes and objects in Java programming, Java programming objects

Source: Internet
Author: User

Classes and objects in Java programming, Java programming objects

As we all know about computers, the current computer programming language is mainly divided into two major parts: process-oriented and object-oriented. Java is a pure object-oriented language. After learning Java for about a month, I will have a basic understanding of classes and objects in Java. Next, let me talk about my own opinions. You should never try again.

When talking about classes and objects, we must first define a concept. That is, what is a class? What is an object?

First, let's talk about what a class is. Class is a relatively abstract concept. How to define it is a difficult problem. We usually call it a template. Yes, it represents a thing with the same attributes and behaviors. Such as stools, tables, computers, and people. Here, we do not specify a specific thing, but a general term for such things. In Java, classes are actually generic. So what is an object. For example, this computer, this person, this table. Classes and objects are closely related. Class is a common template for some objects with the same property behavior. Object is the product of class instantiation. The ability to distinguish between classes and objects is the first step in learning Java and all object-oriented programming languages.

After learning about classes and objects, we need to understand the format of classes and objects defined in Java.

How to define a class? What should a class contain? Let's talk about it. Person, Head, glasses, nose, mouth. These are the characteristics of the human class. In Java, we generally call it an attribute. In addition to this content, we also know that people will think about eating and sleeping. These are behaviors produced by humans. In Java, we call it a method. A class is usually composed of attributes and methods. In other words, to define a class, we need to clarify the attributes and methods of the class. The format for defining a class is generally: public class name {}. The brackets are the attributes and methods of the classes we want to fill in. The general format of the defined attribute is the access modifier data type attribute name. The general format of the definition method is access modifier return value type method name (parameter type parameter name ). In fact, there are no parameters in the method brackets.

Next, we will implement a class definition for better understanding. Let's define a Student. Student attributes include name, age, student ID, credits, and student attributes, including learning and playing. The specific implementation is as follows:

 

In this program, I specifically used all access modifiers. It can be noted that the access modifiers for each attribute are different. In Java, there are four access modifiers, one more than the well-known C ++. The four access modifiers are public, protected, default, and private. The four access modifiers are arranged in a descending order of their access permissions. Starting from the public perspective, the public has the largest access permission. Members modified by the public can be called in any class, regardless of the same package or different packages, is a modifier with the highest permissions. Next is protected. Members modified by protected can be called in the class defining them and in the same package. If you want to call it in another class of different packages, the class must be a subclass of the class where the members are located. That is, when an inheritance relationship exists, the member modified by protected can be called in different classes. The member modified by the default modifier can also be called in the same class and in the same package. However, even if there is an inheritance relationship, it does not have cross-package calls. As the most rigorous modifier, private can only be called in the current class. If you want to call the private volume, you must use a public method to pass this private volume. For example, when the name attribute in the class is private, we can define a public method of public String setName () and use it to return this name attribute. In this class, apart from attributes, we can see two methods: Study and Play. We noticed that the method access modifier is generally public. In fact, the method access modifier can be of another type. However, if a class cannot be called externally, the connection between the class and the outside will be broken, and its meaning will be lost. In addition, the two return value types are also different. Note that when the return value type is not empty, a value of the same type must be returned. In addition, if you need to use external data in this method, you can pass it in the form of parameters to this method. If not, the parameter can be null.

In this way, a class is defined. Next, we need to instantiate an object. The keyword of Object Instantiation is new. The format of the instantiated object is class name. Object Name = new Class Name (). In fact, this statement is not accurate. The details will be mentioned later. Next, we will instantiate an Stu and assign the initial value:

We can see that we instantiate an Stu and assign it an initial value for a series of behaviors.

Let's focus on Student Stu = new Student (); this line of code, we just said, this is the basic format of the instantiated object class name object name = new Class Name (), we have also said that this statement is not accurate. The correct statement is that the format of the instantiated object is class name object name = new constructor name (). So the question is, what is the constructor? First, the constructor is a method. But it is not the same as other common methods. It is a special method. It is used to instantiate an object, and even assign initial values to some attributes of the object if it has parameters. So how to define a constructor? The basic format for defining the constructor is public class name (parameter type parameter name ). We note that this is slightly different from defining the format of a common method. Note that you do not need to add the return value type to the definition constructor. In addition, the method name must be a class name, rather than a normal method that must satisfy the naming rules. Next, let's talk about the points related to the constructor. (1) I believe that careful friends have discovered that in the class, I have not defined the constructor method. However, no error is reported during Object Instantiation. Because, when defining a class, if you do not define a constructor, the system will give you a default constructor that can only be used to instantiate objects. (2) If you customize constructor methods in the class, the system will not provide constructor methods or the constructor methods provided by the system will be overwritten by the defined constructor methods. (3) A class can define multiple constructor methods. (4) Each time a constructor calls a constructor, a new object is created. Let's continue to look back at the code line of the instantiated object. In fact, Java divides memory into two types: stack memory and stack memory. Some of the basic types of variables defined in the function and the referenced variables of the object are allocated in the function stack memory. When a variable is defined in a code block, java allocates memory space for the variable in the stack. When the scope of the variable is exceeded, java will automatically release the memory space allocated for the variable, and the memory space can be used for another use immediately. Heap memory is generally used to store objects and arrays created by new. In this line of code, Student Stu is used to open up a memory space in the stack memory to store the Stu object name. The memory space opened by new in the heap memory stores the attributes and methods in the Student class. The two are associated by equal signs. The equal sign is used to pass the memory address of the storage Student attribute and method as information to the stack memory Stu location and save it. This is the specific process of constructing methods to realize Object Instantiation. Let's look at a constructor example to enhance our understanding:

 

In this code, we can see the different forms of the two constructor methods in this class.

Next, I would like to talk about the usage of combining classes and objects with another knowledge point in Java. We all know that the this pointer exists in C ++, and Java abolished the pointer. this in Java is actually a bit similar to the function of this pointer. Let's take a look at the following code:

The running result is as follows:

In fact, in addition to calling member variables and member functions, this can also be used to call constructor methods. It can automatically select a constructor based on the type of the included parameters. Expand a little here. If there are two classes that have an inheritance relationship, you need to call the constructor of the parent class when the subclass instantiates the object. This requires super, the usage is similar to this. This will be explained in a later blog.

After talking about this, I believe that you should have a basic understanding of the class and object knowledge in this blog. In fact, the knowledge of classes and objects is far more than that. I am only talking about things. If you have any common mistakes, please kindly point out that new people should make changes to avoid mistakes. Finally, I hope this blog will give you some inspiration and help anyone who knows nothing about this knowledge point understand the classes and objects in Java. Congratulations to the newcomer Tom.

 

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.