Features of the Java language class

Source: Internet
Author: User
Tags control characters instance method

I. Overview of the content (main content)

* Private members and public members of the class

* Overloading of methods

* Construction Method

* Static method of Class

* Application of the object

Ii. Private members and public members of the class

Private Member: Add Private access control modifier

Public member: Add-on access control modifier

If you do not have a mechanism to restrict access to members of a class, you are likely to cause incorrect input. To prevent such a situation from occurring, the Java language provides private member access control modifiers to the private: That is, if you precede the member declaration of a class with the modifier private, you cannot access members inside the class from outside of the class, but only through the class itself, and not by any other class, including subclasses of the class, to obtain or reference, thus achieving the protection of the highest level.

I have been in a small example of the above situation, that is, when declaring a class member property of an integer of type int and the scope of its range is (1-100), does not add the private access control modifier, in the test main, the input value of the value exceeded the access, The program does not error, but does not conform to business logic. Because it can be called directly outside of the class and the class cannot declare the variable, some conditions are limited. So it is easy to cause the wrong operation. So later, when declaring the variable, add the private access control modifier, and the getter and setter that provide the member variable use public methods that are decorated publicly. Outside of the class, you can call the appropriate method on the object of the class to set the value, instead of directly manipulating the value, you can manipulate the value in the method. This can also be said to be the encapsulation of classes, security issues.

* * Default Access control: does not write any access control characters, it means that the member can be accessed and called by the class in the same package, how a subclass and its parent class are in different packages, the subclass cannot access the default access control members in the parent class, that is, no class in the other package can access the default control member. The same is true for classes.

Iii. Overloading of methods

Overloading of methods is a method of implementing polymorphism. In object-oriented programming languages, some methods have the same meaning, but with different parameters, these methods use the same name, which is called the overload of the method (overloading). That is, overloading refers to a method with the same name within the same class, which has different functions if the number of parameters is different, or if the number of arguments is the same, but the types are different. When the program runs, it selects the appropriate method execution according to the corresponding parameters.

is the same name on the line, the other parameters of the type, number, parameter order is different is the method of overloading.

The Java language does not allow the number of arguments or the exact same parameter types, only overloads that return different types of values.

Public double Setcylinder (double r,int h);

public void Setcylinder (String str);

Iv. Construction Methods

Because if the member variables are assigned by the appropriate method after the object is created, the code is not concise enough, so the Java language provides a special member method-the constructor method-in the class. It is all the work that is initialized when the object is created, and the amount of code is concise.

The name of the constructed method must be the same as the name of the class, the constructor does not return a value, but it cannot be decorated with a modifier with no return value type of void, because the class itself is the return value of the constructor of a class, and if void is not a construction method, it is a common method. The system is not automatically called. When the method definition is constructed, it is automatically called when the object is created, so the construction method does not need to be called directly in the program, but is executed automatically when the object is produced. This is different from other methods, and the general approach is to use the object for invocation when needed.

* * Say a lot of, in fact, when creating an object, add the member property in the object's parentheses, he will automatically call the appropriate construction method to assign the object, do not need to be assigned by setter or the. xxx property, but you must design the appropriate construction method.

* * Special points for construction methods:

(1), the method name of the constructor method is the same as the class name.

(2), the construction method has no return value, but cannot use void.

(3), the main function of the construction method is to complete the initialization of the class object work.

(4), the construction method is generally not explicitly called by the programmer, but new to invoke.

(5), when you create an object of a class, the system automatically calls the constructor of the class to initialize the new object.

* * Attention to issues

(1), if you create a class object without adding parameters, he will default to call the default constructor method, it is no parameter, the method body is also no code.

(2), the construction method can also be overloaded, and the principle of other methods is the same, depending on the number of parameters, or different types to match the appropriate construction method in multiple construction methods.

(3), calling another constructor from one of the constructor methods: for certain operations, the Java language allows another constructor to be called within a class from within a constructor method. This shortens the program code and reduces the development time. This process must be implemented using this keyword. For example

Public Student () {

This ("Luhan", 25, "male");

System.out.println ("default constructor called")

}

Public Student (String name,int age,string gender) {

System.out.println ("called a parametric constructor");

THIS.name = name;

This.age = age;

This.gender = gender;

}

Then create the object in the main function of the test directly new, do not need to write a large set of parameters in parentheses; Student Student = new Student ();

* * There are also two points of note:

1. When another constructor method is called in one of the constructor methods, it must be called using the This key. Otherwise, an error will occur at compile time.

2. The This keyword must be written in the first line of the construction method.

(4), public construction method and private construction method: If the construction method is designed as a private method and called in the public constructor, it can be called through the This keyword, then it can be called outside the class, because the private law can not be called outside the class, but can be called outside the , so in the public method of this private method, and the outside of the class calls the public method can implement the private construction method to implement the call.

V. Static members and instance members

Static modifier, which modifies members in a class. A member that is modified by static is called a static member, also known as a class member, and a member without a static adornment is called an instance member.

 1. Instance members (without static adornments): Instance members are owned by individual objects and cannot be shared with each other. Created objects have their own storage space to hold their own values, not to be shared with other pairs. Therefore, if you modify the value of an object, it does not affect the value of other objects. , because these member variables are independent and exist in different memory, the member variables that have attributes are referred to as instance variables in Java.

That is, you must first create the exclusive, and use the object to invoke the method. Instead of calling the method directly without passing the object. A method with this attribute, which Java calls an instance method. This shows that instance members are owned by individual objects and cannot be shared with one another.

  2. Static variables : Static variables are called "Static variables", which are also called class variables. A static variable is a variable that belongs to a class. Rather than a specific object belonging to any one class. That is, for any object of the class, a static variable is a common storage unit that is not stored in the memory space of an object instance, but is stored in the common storage unit of the class's memory space. Or, for any particular object of a class, a static variable is a common storage unit, and any object that accesses it is the same value. Similarly, any object access to a class to modify it is done in the same memory unit. A static variable is defined with a static modifier. A static variable is somewhat similar to a global variable in another language, and if the variable is not private, it can be accessed outside the class and does not need to create an instance object of the class, only the name of the class can be referenced. In other words, static variables do not need to be instantiated to be used. Of course, you can also access static variables by instantiating objects. Format used:

Class name. static variable name;

The object name. static variables;

If there are static variables, the static variables must be independent of the method, just as other high-level languages declare global variables, and must be declared outside of the function.

Example: the volume of a cylinder like the calculation is, there are different low and high, but the pi=3.14 is the same can be defined as static variables, to share, and to calculate how many of the total created objects can also be a statistic num set static variables to share, in the construction method of the call to design num++ , each new object is num plus 1, it is shared, each time it is changed, if not add static is the instance variable, for each object is private, each created is the default value.

 3. Static method

As with static variables, methods that have the static modifier decorated are static methods of the class, also known as class methods. The static method is essentially a method that belongs to the entire class, and the non-static method is the specific method that belongs to an object, and declaring a method as static has the following meanings:

(1), non-static method is a method belonging to an object, at the time of creation of this object, the method of the object in memory has its own dedicated code snippet. The static method belongs to the entire class, and its in-memory code snippets are shared by all objects, not by any one object.

(2), because the static method belongs to the entire class. So he can't manipulate and process members belonging to an object. Instead, you can only handle members that belong to the entire class, that is, static method member variables or call static member methods. Or, the instance variable and the instance method cannot be accessed in a static method.(3), this and super keywords cannot be used in static methods. Because this is the object that represents the call to the method, but now the "static method", since the object does not need to be called, this also naturally does not adapt to exist in the "static method" inside.(4), when a static method is called, it can be called using the name, or it can be invoked with a specific object.   The format is as follows: Class name. static method name (); The name of the object. static method name ();   4. Static initializer A static initializer is a group of statements surrounded by a bunch of curly braces "{}" that are modified by the static keyword. Its function is somewhat similar to the way the class is constructed, and it is used to initialize the work, but the static initializer and the class construction method organically make a fundamental difference:(1), the construction method is initialized for each object created, and the static initializer initializes the class itself.(2), the constructor method is that the new operator creates the object is executed by the system automatic call, and the static initializer generally cannot have the program to call, it is in the owning class is loaded as memory by the system call execution.(3), how many new objects are created with the new operator, how many times the constructor is called, but the static initializer is executed only once when the class is loaded into memory, regardless of how many objects are created.(4), unlike the constructor method, the static initializer is not a method, so there is no method name, return value, and parameter.Static{num++;System.out.println ("Static initializer is called, Num's initial value is" +num ");}

The class is loaded the first time it is called, not all of the classes that might be used in the loader when the program starts.

If there are multiple static initializers, their initialization in the class is executed sequentially.

In summary, the function of the static initializer is to complete the initialization of the entire class, including assigning the initial value to the static member variable, which is automatically completed when the system is loaded into memory.

 5, the application of the object

(1), in fact, the object is a reference type of variable, and the reference variable actually holds the object in memory address (also known as the object's handle), so in terms of the function of the object, the object is "a variable to the object", but in terms of its type, it belongs to "class type of variable". Therefore, in some cases, you can use the same object as the basic type variable.

(2), assignment and comparison of objects: When working with objects, it is common to create objects with the new operation and then manipulate them, but sometimes you do not create new objects using the new operator, but you can still assign them.

Example: Cylinder volu1,volu2;//declaration of two reference variables

VOLU1 = new Cylinder (1.0,2);//Creates a class object and points the reference variable VOLU1 to it.

VOLU2 = volu1;//means that the reference variable of two different names simultaneously points to an object;

So VOLU2 later called the method to change the object's data content, for VOLU1 is also effective.

Example: Cylinder volu1 = new Cylinder (1.0,2);

Cylinder VOLU2 = new Cylinder (1.0,2);

Volu1.compare (VOLU2);//The result of the comparison here is not equal, because they are two independent objects, each occupying a different space, so the first address in memory is not the same, that is, VOLU1 and VOLU2 two reference variable value is not the same.

VOLU3 = VOLU2;

Volu3.compare (VOLU3);//The comparison values here are equal, because they point to the same variable, the first address is the same, and the value is the same.

       

Features of the Java language class

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.