Object orientation of Javase series (I.)

Source: Internet
Author: User

As a Java programmer, what we do every day is OOP (object-oriented), it can be said that everything is object, Java is an object-oriented programming language, because the basic object-oriented knowledge is also a more complex module, so Bo Master I am going to use a number of articles to introduce the object-oriented foundation in Java, Hope to help beginners.

As a programming language of the OOP type, the two most important concepts in Java are classes (class) and objects (object), and we sometimes refer to objects as instances (instance). Class, which is an abstraction of a group of objects, we can see him as a collection of high school mathematics, whereas an object (instance) is a concrete entity, and we can think of an object as an element in a collection. The definition of the class is as follows:

[modifier] class name {

0 to multiple constructor definitions ....

0 to multiple member variables ....

0 to multiple methods ....

}

As you know, there are three common types of members in a class: constructors, member variables, methods. Now let's start with these three members to understand the classes in Java.

Constructor: A constructor is a basic way for a class to create objects, and if a class does not have a constructor, it cannot create an instance, and the Java language provides a function that when the programmer does not write a constructor for a class, the system will provide a default constructor for the class, once the programmer has written a constructor for the class, The system will no longer provide the constructor. The role of the constructor is to perform initialization operations when the object is created. If you don't understand it, take a look at the following example:

public class Apple {

Let's set the Apple two properties: Origin and weight
Public String Producing_area;
public int weight;
Next we're going to provide the constructor for this Apple class
Public Apple (String producing_area, int weight) {//parenthesis is the formal parameter list
This.producing_area=producing_area;
This.weight = weight;
}
This time we're going to instantiate an Apple class with two parameters
public static void Main (string[] args) {
Apple Apple = new Apple ("Yunnan", 200);
Finally we can visit the two parameters we set for the object
System.out.println ("Origin is:" +apple.producing_area+ "" + "Weight is:" +apple.weight+ "G");
}
}

The above code runs as follows:

The origin is: Yunnan weight is: 200g

As we can see from the above code, we provide the constructor for the Apple class, which has two parameters, which we can set and access when we instantiate the class.

For constructors, let me introduce you to the constructors ' overloads. What is overloading? With the same constructor name and a different list of parameters, we become overloads of the constructor. For example, the Apple class we just mentioned, we take the constructor to load the way for this class to provide a variety of instantiation of the way:

public class Apple {
public static String Producing_area;
public int weight;
Next we're going to provide a parametric constructor for this Apple class.
Public Apple (String producing_area, int weight) {
This.producing_area=producing_area;
This.weight = weight;
}
We provide an argument-free constructor for this Apple class.
Public Apple () {}

  //This time we can provide two ways to instantiate the Apple class

public static void Main (string[] args) {
Apple apple1 = new Apple ("Yunnan", 20);
Apple apple2 = new Apple ();
}

}

The code above shows that the purpose of the constructor is to make the instantiation of the class more diversified, and we can create an instance of the class based on the requirements of the project.

The constructor is done, we go on to the member variables, we are in the class (note: I will slowly talk about the scope of the Java variable) created by the variable, is a member variable, the member variable is divided into two kinds, a static modified class variable, class variable belongs to this class, The variable that is not modified with static is an instance variable, which belongs to an instance of the class. A class variable is a service to a class, and an instance variable is a service to an instance of a class. So when this class is in the preparation phase, the class variable is generated, and when the class is destroyed, the class variable is destroyed. The instance variable is created when the object is created and destroyed when the object is destroyed. In general, a class accesses a class variable, an instance accesses an instance variable, but an instance can also access a class variable (which, in fact, translates into a class-access class variable at the bottom). The syntax format is as follows: Class . Class variable instance . Instance variable instance . Class variable. What I want to say here is that we can't use classes to access instance variables for the simple reason that Java virtual machines first create classes before they create instances. Then it is possible that after the Java virtual machine has finished creating the class, there is no time to create the instance variable, and the class to access the instance variable will be an error. Finally, I want to tell you that the member variable does not need to be initialized, and we can always assign a value to it whenever we use a member variable. (There is a definition of instance variables and class variables in the code above, you can see for yourself)

Finally, I would like to introduce you to the method, which is the abstraction of class or object behavior, what does it mean? Most methods perform a function. The method is not independent, it either belongs to the class, or it belongs to an instance of the class, which means that the method cannot run independently and can only be called by a class or object. Definition of the method:

[modifier] Method return value method name (formal parameter list) {

0 to multiple executable statements .....

}

Here I give you a demonstration of the code, but also the method of overloading to show you:

public class Test {

public void Test () {
System.out.println ("This is a method of no argument ...");
}
public void Test (String a) {
System.out.println ("This is the overloaded test method" + "" +a);
}
public static void Main (string[] args) {
Test T = new Test ();
T.test ();
T.test ("The test method is overloaded");
}
}

Here are two ways to print a word on the console, this is the function of this method, here we also use the method of overloading, the method is overloaded with the method name of the same parameter list is different. Finally, we introduce the modifier of the method, the modifier of the method can be: Public protected private abstract static final where public protected private can only appear one, with static F Inal a combination of methods, but only one can appear in abstract and final. The return value type of a method can be any data type contained in the Java language, including the base data type and the reference type.

To summarize, today I briefly introduce you to the three common members of a class, and tomorrow I will continue to introduce you to other interesting objects in Java, and I hope you will reap some of the benefits.

Due to my limited level, you are welcome to peer criticism.

Object orientation of Javase series (I.)

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.