Like we do computer this piece, all know such a thing, the current computer programming language is divided into two major chunks, one for the process, and two for the object-oriented. Java is a purely object-oriented language. Learning about one months of Java, under the Java class and objects have a basic understanding. Next I came to talk about their own views, the big God do not spray, novice small white to the.
Now that we are talking about classes and objects, first of all, we need to define a concept. That is, what is a class? What is an object?
First let's talk about what is called class. Class is a relatively abstract concept, how to define it, is a problem. We usually call it that template. Yes, it represents a thing that has the same attributes, behaviors, and so on. For example, stools, tables, computers, people. Here, we do not specify a specific thing, but the general name of such things. The class in Java, in fact, is such a generic. So what is an object? For example, this computer, this person, this table. Classes and objects have an inseparable relationship. Class, which is a generic template for objects that have the same property behavior. Object, which is a product of instantiation of a class. The ability to differentiate between what is a class and what is an object is the first and most critical step in learning Java and all object-oriented programming languages.
Now that we know the classes and objects, we're going to figure out the format of the classes and objects in Java.
How do I define a class? A class, what do you want to include in the content? Let's just say it. Person, head, glasses, nose, mouth. These are the characteristics of the human class. In Java, we often call it attributes. In addition to these contents, we also know that people will think, to eat to sleep. These are the behaviors that people produce. Inside Java, we call it a method. And a class, which is usually made up of two parts: a property and a method. In other words, to define a class, we need to clarify both the properties and methods of the class. A class is defined in a format that is generally the public class name {}. Inside the curly brackets are the properties and methods of the class we want to populate. The general format of the definition property is the access modifier data Type property name. The general format of the definition method is the access modifier that returns a value type method name (parameter type argument name). In fact, it is possible to have no parameters in the method parentheses.
Next, let's implement a definition of a class to better understand it. We might as well define a student class student. Student class properties, have name, age, school number and credit, students of the method, there is learning, play. The specific implementation is as follows:
In this program, I purposely used all the access modifiers. It can be noted that the access modifiers that define each property are not the same. In Java, there are four kinds of access modifiers, more than the one we know about C + +. The four access modifiers are: public,protected, default, Private. The order in which the four access modifiers are arranged, that is, the order in which the access permissions are large to small. From public start, public access to the largest, public modified members, can be called in any class, regardless of the same package or different packages, is the most permissions of a modifier. Next comes the protected. Members that are modified by protected can be called in the class that defines them, in the same class as the package. If you want to call in another class in a different package, this class is required to be a subclass of the class where the member resides. In the case of an inheritance relationship, a member modified by protected can be called in a different class. Members that are decorated with the default modifier can also be called in the same package, but there is no cross-package invocation even if there is an inheritance relationship. Private as the most stringent modifier, the member it modifies can only be called in the current class, and if you want to invoke the private volume, you must use the public method to pass the private volume. For example, when the Name property in a class is private, we can define a public method for the common String setName (), using it to return the Name property. In this class, in addition to the attributes, we see there are two methods, study and play. We notice that the access modifier for the method is generally public. In fact, the access modifiers of the method are available for other types. However, as a concrete implementation of the role of a class outside, if it cannot be called externally, then the connection between the class and the outside is broken, then its meaning is lost. In addition, the return value types are not the same, note that when the return value type is not empty, you need to return a value of the same type. Furthermore, if you need to use external data in this method, you can pass it in the form of an argument to the method. If it is not required, then the parameter can be empty.
In this way, a class is defined. What we're going to do next is instantiate an object out of it. The keyword for object instantiation is new, and the format of the instantiated object is the class name Object name =new class name (). In fact, this statement is not accurate. Specifically, it will be mentioned in the following article. Next, we will instantiate a Stu and assign an initial value:
As you can see, we instantiate a Stu and give it a corresponding initial value, and perform a series of behaviors.
Let's focus on the Student stu=new Student (); This line of code, as we have just said, is the basic format of the instantiated object class name Object name =new class name (), as we have said earlier, this statement is actually not accurate. The correct argument should be that the format of the instantiated object is the class name object name =new constructor method name (). So, here's the question, what is the construction method? First, the construction method is a method. But it is not the same as other common methods, it is a special method. Its function is to instantiate the object, even in the case of its parameters, you can implement some properties of the object to assign the initial value. So, how do you define a construction method? The basic format for defining a construction method is the public class name (parameter type argument name). We note that this is slightly different from the format for defining common methods. Note that the definition constructor does not need to add the return value type, and the method name must be the class name, but not as simple as defining a normal method just to satisfy the naming convention. Next, we're going to talk about some of the construction methods. (1) I believe that careful friends have found, in the class, I do not define the construction method, but the instantiation of the object when there is no error. Because, when defining a class, if you do not define the constructor yourself, the system will give you a default constructor that can only be used to instantiate the object. (2) If you define a constructor method in your class, the system will not provide a constructor method or a system-provided construction method that is overridden by the defined constructor method. (3) A class can define multiple construction methods. (4) The construction method will create a new object every time it is called. We continue to look back at this line of code that instantiates the object. In fact, Java divides memory into two types, called stack memory, which is called heap memory. Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another. Heap memory is typically used as a storage for objects and arrays created by new. In this line of code, the Student Stu function is to open a memory space in the stack memory Stu this object name. The memory space created by new in heap memory stores the properties and methods within the student class. The two are associated by an equal sign. The function of the equals sign is to pass the memory address of the stored student property and method as information to the Stu location of the stack memory and save it. This is the concrete process of constructing a method to instantiate an object. We might as well take a look at an example of structural methods to enhance understanding:
In this code, we can see the different forms of the two constructor methods in this class.
Next, I want to talk about the use of classes and objects in conjunction with another knowledge point in Java. As we all know, C + + has this pointer, Java is the abolition of the pointer, in Java, this is actually a bit similar to the function of this pointer. We might as well take a look at the following code:
The results of the operation are as follows:
In fact, this can also be used to invoke a constructor method in addition to calling member variables and member functions. It can automatically select the conforming construction method according to the type of the contained parameter. To expand a bit here, if there are two classes that have an inheritance relationship, then when the subclass instantiates the object, it needs to call the constructor of the parent class, which requires super, which is similar to this. On this point, will be in the future of the blog detailed.
Speaking so much, I am sure that everyone who sees this blog should have a basic understanding of the knowledge of classes and objects. In fact, the knowledge of classes and objects is much more than that, and I am only talking about fur. If there are common sense errors, please the great God generous point, the new people to change, so as not to fraught. Finally, hopefully this blog will give you a little bit of inspiration to help people who know nothing about this knowledge to understand the classes and objects in Java. The newcomer to the small white king.
Classes and objects in Java programming