Deconstructor
Differences of constructors and methods To learn Java, you must understand the constructor. Because the constructor can provide many special methods, this is often confused by beginners. However, there are many important differences in constructors and methods. Author: Robert Nielsen of the original station: www.javaworld.com we say that the constructor is a method, like the platypus is an Australian speaking feeding animals. (Press: Foreigners like to make a metaphor, I will also be translated). To understand the duck beast, you must first understand the difference between it and other feeding animals. Similarly, to understand the constructor, then the difference between the constructors and methods should be understood. All people who study Java, especially for those who want to certify exams, and understand constructors. The following will be briefly introduced, and finally uses a simple summary. Different constructors for functions and roles are instances of a class. This process can also be used when creating an object: PlatyPus P1 = New PlatyPus (); instead, the method's role is to perform Java code. Different constructors and methods of modifiers, return values, and naming are in the following three convenient differences: modifiers, return values, named. As with the method, the constructor can have any access to modifications: public, protected, private, or no modification (usually called Package and Friendly). Different from the method, the constructor does not have the following non-access nature modifications: Abstract, Final , native, static, or synchronized. The return type is also very important. The method can return any type of value or no return value (Void), the constructor does not return the value, nor does the VOID. Finally, talk about the names of both. The constructor uses the same name as the class, while the method is different. According to habits, methods typically start with lowercase letters, and constructors typically start with uppercase letters. The constructor is usually a noun because it is the same as the class name; the method is usually closer to the verb because it illustrates an operation. "this" usage constructor and method use keyword this. Method references this point to an example of a class that is performing a method. The static method cannot use this keyword, because the static method does not belong to the instance of the class, so this is nothing to point to. This of the constructor points to another constructor in the same class, another constructor of different parameter lists, let's take a look at the following code: public class platypus {string name; platypus (String Input) {name = input;} platypus () {this "John / mary doe");} public static void main (string args []) {platypus p1 = new platypus ("digger"); PlatyPus P2 = new platypus ();}} In the above code, there are 2 The constructor of different parameter lists. The first constructor, the member name given to the class, the second constructor, calls the first constructor, gives the member variable name "john / mary doe". In the constructor, if you want to use the keyword This, then, must be placed in the first line, if not, will cause a compilation error. "Super" usage constructor and method, use keyword super points to the superclass, but the method is not the same. Methods Use this key to perform methods in the overloaded superclass.
Look at the following example: Class Mammal {Void getBIRTHINFO () {system.out.println ("born alive.");}}}}}} Class PlatyPus Extends Mammal {void getBIRTHINFO () {System.out.Println ("Hatch from Eggs") System.out.Print ("a Mammal Normal IS"); Super.getBIRTHINFO ();}} In the above example, use super.getBIRTHINFO () to call the overloaded method in the supermarket MAMMAL. The constructor uses Super to call the constructor in the superclass. And this line of code must be placed in the first line, otherwise compiling will be wrong. Looking down below: Public class superclassdemo {superclassdemo () {}} class child extends superclassdemo {child () {super ();}} In this, there is no practical example, the constructor Child () contains Super, Its role is to instantiate the constructor superclassdemo in the superclass and add it to the Child class. The compiler automatically adds the code compiler to automatically add code to constructors. For this, Java programmers may confuse. When we write a class without a constructor, the compiler will automatically add a constructor without parameters, for example: public class example {} will be compiled after compiling: public class example {example () {} } On the first line of the constructor, the compiler will also be plus, for example: public class testorstructors {testconstructors () {}} The compiler will add the code, as follows: Public class testicstructors {testconstructors () {Super;}} Carefully think about it, know the following code public class example {} will pass the compiler plus code shape as: public class example {example () {super;}} inherited constructor is inherited. Subclasses can inherit any method of superclasses. Take a look at the following code: public class example {public void syhi {system.out.println ("hi");} example subclass extends Example {} Subclass Exmeample {} class subclass automatically inherits the SAYHI method in the parent class However, the constructor example () in the parent class cannot be inherited. to sum up