Static, this, super, and final usage in Java (2)

Source: Internet
Author: User
II. This & Super
In Previous exampleWe have discussed various static usage. Using static to define methods or members provides some convenience for programming, to some extent, it is similar to the global functions and global variables in C language. However, it doesn't mean that with this convenience, you can use it everywhere. If so, you need to seriously consider whether you are using object-oriented programming, whether your program is object-oriented. Now, let's start to discuss the meanings and usage of these two keywords.
In Java, this usually refers to the current object, and super refers to the parent class. When you want to reference something of the current object, such as a method of the current object or a member of the current object, you can use this to achieve this purpose. Of course, another purpose of this is to call another constructor of the current object, which will be discussed soon. If you want to reference something of the parent class, it is not super. Since this has some similar characteristics and inherent relationships with super, we will discuss them here, hoping to help you differentiate and master them.
In the general method
The most common situation is that a parameter name in your method has the same name as a member of the current object. In this case, to avoid confusion, you need to explicitly use the this keyword to specify that you want to use a member. The usage is "This. member name. In addition, you can also use "this. "method name" to reference a method of the current object, but this is not necessary at this time. You can directly use the method name to access that method, the compiler will know which one you want to call. The following code demonstrates the above usage:
  1. Public ClassDemothis {
  2. Private StringName;
  3. Private IntAge;
  4. Demothis (StringName,IntAge ){
  5. Setname (name );// You can add this to call the method, such as this. setname (name); but this is not required.
  6. Setage (AGE );
  7. This. Print ();
  8. }
  9. Public VoidSetname (StringName ){
  10. This. Name = Name;// You must specify that you want to reference the member variable.
  11. }
  12. Public VoidSetage (IntAge ){
  13. This. Age = age;
  14. }
  15. Public VoidPrint (){
  16. System. Out. println ("name =" + name + "age =" + age );// This does not need to be used in this line, because there is no obfuscation
  17. }
  18. Public Static VoidMain (String[] ARGs ){
  19. Demothis dt =NewDemothis ("Kevin", "22 ");
  20. }
  21. }

This code is very simple and you should be able to understand it without explaining it. In the constructor, you can use this. Print () instead of print. Next we will modify this program to demonstrate the usage of super.

  1. ClassPerson {
  2. Public IntC;
  3. Private StringName;
  4. Private IntAge;
  5. Protected VoidSetname (StringName ){
  6. This. Name = Name;
  7. }
  8. Protected VoidSetage (IntAge ){
  9. This. Age = age;
  10. }
  11. Protected VoidPrint (){
  12. System. Out. println ("name =" + name + "age =" + age );
  13. }
  14. }
  15. Public ClassDemosuperExtendsPerson {
  16. Public VoidPrint (){
  17. System. Out. println ("demosuper :");
  18. Super. Print ();
  19. }
  20. Public Static VoidMain (String[] ARGs ){
  21. Demosuper DS =NewDemosuper ();
  22. DS. setname ("Kevin ");
  23. DS. setage (22 );
  24. DS. Print ();
  25. }
  26. }

In demosuper, the re-defined print method overwrites the print method of the parent class. It first does something of its own, and then calls the overwritten method of the parent class. The output shows this:
Demosuper:
Name = Kevin age = 22
This method is commonly used. In addition, if the members of the parent class can access the quilt class, you can use it like this and use "super. in the parent class, but you often do not access the Member names in the parent class in this way.
In the constructor
Constructor is a special method that is automatically called during object initialization. In the constructor, this and super also have various usage methods described above, and they also have special features. Please refer to the following example:

  1. ClassPerson {
  2. Public Static VoidPRT (StringS ){
  3. System. Out. println (s );
  4. }
  5. Person (){
  6. PRT ("A person .");
  7. }
  8. Person (StringName ){
  9. PRT ("A person name is:" + name );
  10. }
  11. }
  12. Public ClassChineseExtendsPerson {
  13. Chinese (){
  14. Super();// Call the parent class Constructor (1)
  15. PRT ("A Chinese .");// (4)
  16. }
  17. Chinese (StringName ){
  18. Super(Name );// Call the constructor with the same parameters of the parent class (2)
  19. PRT ("His name is:" + name );
  20. }
  21. Chinese (StringName,IntAge ){
  22. This(Name );// Call the constructors with the same parameters (3)
  23. PRT ("his age is:" + age );
  24. }
  25. Public Static VoidMain (String[] ARGs ){
  26. Chinese Cn =NewChinese ();
  27. CN =NewChinese ("Kevin ");
  28. CN =NewChinese ("Kevin", 22 );
  29. }
  30. }

In this program, this and super no longer use ". "connects a method or member, but directly follows the appropriate parameter, so its meaning changes. Super is used to call constructors with the same form in the parent class, such as 1 and 2. If this is followed by a parameter, the constructor with the same parameter is called, for example, 3. Of course, in each overload constructor in Chinese, the usage of this and super in general methods can still be used. For example, you can replace it with "This. PRT (because it inherits the method in the parent class) or "super. PRT (because it is a method in the parent class and can be accessed by the quilt class), it can still run correctly. However, it seems a bit fascinating.
At last, I wrote so much. If you keep in mind the phrase "this usually refers to the current object, super usually refers to the parent class", then this article achieves the goal, others you will learn and master in future programming practices. For the inheritance mentioned in this article, refer to the relevant Java tutorial.
The significance and usage of the last final will soon be posted. Please read and correct me.
 

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.