In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "characteristics of Java constructors", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn the "characteristics of Java constructors"!
Catalogue
What is a constructor
Second, the characteristics of constructor
III. Examples
Default constructor
5. Overloading of constructor
VI. The use of constructors
What is a constructor
Java constructor, also known as constructor, is a special function in JAVA. Same as the function name, no value is returned.
Function: generally used to initialize member properties and methods, that is, after the new object is generated, the properties and methods of the object are called.
In real life, as soon as many things appear, they are born with certain attributes and behaviors. For example, as soon as a person is born, he will cry as soon as he is born; as soon as the car is produced, it will have color, appearance, running, and so on.
These, we can define these natural properties and behavior in the constructor, when new instantiates the object, also have these properties and methods, do not have to redefine, thus speeding up programming efficiency.
The constructor runs as soon as the object is established, initializes the object, including properties, and executes the statements in the method.
The general function is executed only when the object is called, and the function is added to the object in the way of ". Method name.
An object is created and the constructor is run only once.
A general function can be called multiple times by this object.
Second, the characteristics of constructor
1. The function name is the same as the class name
2. There is no need to define the return value type. (unlike the return value of the void type, void does not have a specific return type; the constructor does not even have a type)
3. You cannot write return statements. (there is no return value type, so no return statement is required.)
Note: general functions cannot call constructors, only constructors can call constructors.
III. Examples
1. Only one method is defined in the no-parameter constructor class. The constructor is always called along with the new operation.
When the new object is called, the corresponding constructor is called to execute this method. You don't have to write ". Method name.
Package javastudy; public class ConfunDemo {public static void main (String [] args) {/ / output Hello World. As soon as the new object is created, the corresponding constructor Confun () is called and the println statement is executed. Confun c1=new Confun ();}} class Confun {Confun () {/ / define the constructor and output Hello World System.out.println ("Hellow World");}}
Output:
Hellow World
2. With parameter constructor, when new object, the real parameter value is passed to private variable, which is equivalent to completing the setter function.
When package javastudy; public class ConfunDemo3 {public static void main (String [] args) {/ / instantiates an object, the Person constructor is called directly in new Person () and the argument is transferred, which is equivalent to the setter function Person z = new Person ("aerchi", 18); z.show ();}} class Person {private String name; private int age / / there is a parameter constructor to realize the function of passing parameter values to private member variables. When instantiating an object public Person (String npenint m) {name=n; age=m;} / / getter / /, after completing the sett function, you need getter to get the actual parameter values. Public String getName () {return name;} public int getAge () {return age;} / / get the private value and print out public void show () {System.out.println (name+ "\ n" + age);}}
Output:
Aerchi
eighteen
In the above code, we can also put the output statement in the show () method directly in the constructor, and when we new the object, we can directly output the value, as follows
When package javastudy; public class ConfunDemo3 {public static void main (String [] args) {/ / instantiates an object, the Person constructor is called directly in new Person () and the argument is transferred, while the output statement Person z=new Person ("aerchi", 18) is executed;}} class Person {private String name; private int age / / has a parameter constructor to realize the function of passing parameter values to private member variables, while directly outputting the value public Person (String nther int m) {name = n; age = m; System.out.println (name+ "\ n" + age);}}
Output:
Aerchi
eighteen
Or
Class ConFun {public static void main (String [] args) {Person a=new Person (18, "aerchi"); System.out.println (a.getAge () + "," + a.getName ());} class Person {private int age; private String name; public Person (int x line string y) {age=x; name=y;} public int getAge () {return age } public String getName () {return name;}}
3. After an object is established, the constructor only runs once.
If you want to assign a new value to the value of an object, use the set and get methods, which are used as general functions
As follows:
When package javastudy; public class ConfunDemo4 {public static void main (String [] args) {PersonDemo s=new PersonDemo ("Zhang San", 18); / / new object, the corresponding constructor is called and the value is passed. At the same time, you cannot new the same object multiple times, otherwise an error will be reported. S.setName ("Li Si"); / / after the object is established, when you want to change the value, use the set/get method to reset the new value s.setName ("Wang Ermazi"); / / and can call the object multiple times. S.print ();}} class PersonDemo {private String name; private int age; PersonDemo (String njinint m) {/ / establishes a parametric constructor to assign values to two private variables name and age, while outputting the value name=n; age=m; System.out.println ("name:" + name+ "\ n" + "age:" + age). } public void setName (String x) {/ / set method, which is used to assign name=x;} public String getName () {/ / get method to name again, and is used to obtain the assignment return name;} public void print () {System.out.println (name);}} of name.
Output result:
Name: Zhang San
Age: 18
Wang er pockmarked
Default constructor
When there is no constructor defined in a class, the system adds a default constructor with null parameters to the class to facilitate its initialization. It's just that the empty constructor is hidden.
As follows, the default constructor Person () {} is hidden and not displayed.
Class Person {/ / Person () {}}
When you customize the constructor in this class, the default constructor is gone.
If you still want to construct a function, you need to add it manually in the class.
5. Overloading of constructor
Constructor is also a kind of function, which also has the Overloding property of function.
Class Person {private String name; private int age; Person () {System.out.println ("age= name =" + name+ ", age=" + age);} Person (String n) {name=n; System.out.println ("Brangname =" + name+ ", age=" + age);} Person (String npenint a) {name=n; age=a System.out.println ("class PersonDemo2 name =" + name+ ", age=" + age);}} class PersonDemo2 {public static void main (String [] args) {Person p1=new Person (); Person p2=new Person ("aerchi"); Person p3=new Person ("aerchi", 18);}}
Output result:
A:name=null, age=0
B:name=aerchi, age=0
C:name=aerchi, age=18
Class Person {private String name; private int age; Person () {System.out.println ("System.out.println name =" + name+ ", age=" + age "); cry ();} Person (String n) {name=n; System.out.println (" name+ name = "+ name+", age= "+ age); cry ();} Person (String nMint a) {name=n Age=a; System.out.println ("cry name =" + name+ ", age=" + age); cry ();} void cry () {System.out.println ("Haha.");}} class PersonDemo2 {public static void main (String [] args) {Person p1=new Person (); Person p2=new Person ("aerchi") Person p3=new Person ("aerchi", 18);}
Output result:
A:name=null, age=0
Haha.
B:name=aerchi, age=0
Haha.
C:name=aerchi, age=18
Haha.
VI. The use of constructors
1. By default, all constructors of the subclass call the no-parameter constructor of the parent class (the constructor will not be inherited, but will only be called by the subclass). The parameter of the parent class is private and cannot be accessed directly. You need to use the get method in the parent class to call the private variable value.
Package javastudy; public class ConfunDemo5 {public static void main (String [] args) {Pupil z=new Pupil (); z.show ();}} class Student {/ / parent Student private String name; private int height; public Student () {this.name= "; this.height=0;} public String getName () {return name } public int getHeight () {return height;}} class Pupil extends Student {/ / subclass Pupil private int score; public Pupil () {/ / No-parameter constructor Pupil () directly inherits the no-parameter constructor Student () in the parent class, but the name and height in the parent class are score=0 of private } public void show () {System.out.print ("name:" + getName () + "\ nheight:" + getHeight () + "\ nscore:" + score); / / use the get method name directly when outputting. }}
2. Use super to call the constructor of the parent class
Super must be written on the first line of the method
Package javastudy; public class ConfunDemo5 {public static void main (String [] args) {Pupil z=new Pupil (Wang er Mazi, 100200); z.show (); Pupil w=new Pupil (); w.show ();}} class Student {/ / parent Student public String name; public int height; public Student () {this.name= "; this.height=0 } public Student (String npenint m) {name=n; height=m;}} class Pupil extends Student {/ / subclass Pupil private int score; public Pupil () {super ("Liu Dehua", 501); / / use super to call the parent class Student (String nminint m) method, passing the actual value. The super must be written on the first line of the method. If you write super () here, the Student () method in the parent class is called. Score=0;} public Pupil (String x _ minint yint z) {/ / super (x _ line y); / / call the parent Student (String ncent int m) method using super, where the parameter name in super must be the same as the parameter name in the constructor. Score=z;} public void show () {System.out.println ("name:" + name+ "\ nheight:" + height+ "\ nscore:" + score);}}
Output:
Name: Wang er pockmarked
Height: 100
Score: 200
Name: Liu Dehua
Height: 501
Score: 0
Thank you for your reading, the above is the content of "the characteristics of the Java constructor", after the study of this article, I believe you have a deeper understanding of the characteristics of the Java constructor, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.