当前位置:网站首页>Detailed explanation of the difference between construction method and method
Detailed explanation of the difference between construction method and method
2022-06-11 09:19:00 【lwj_ 07】


Conclusion !!!
Although there is no construction method in the student class But in the test code Student Object is also created . The reason is that when there is no constructor in the class, the system will construct a parameterless constructor by default
The difference between construction method and common method structure is as follows :
[ List of modifiers ] Constructor name ( List of formal parameters ){
Construct method body ;
// You usually assign values to attributes in the constructor body , Complete the initialization of the property .
}
Be careful :
First of all : The modifier list is currently uniformly written as :public It must not be written as public static
second : Constructor name and class name must be consistent
Third : The constructor does not need to specify the return value type , You can't write void, Writing words has become a common method .The common method structure is :
[ List of modifiers ] return type Method name ( List of formal parameters ){
Method body ;
}
Be careful : The modifier list is : public static
How to call the constructor ?
The difference between a constructor and a normal method call :
Common method :
public class OOTest01
{
public static void main(String[] args){
// Calling normal methods
dosome(); // In the same class, there is no Class name .dosome(); It's fine too
// Common method
public static void dosome(){
System.out.println("dosome");
}
}Construction method :
Use new Operator to call the constructor .
Grammar format :
new Constructor name ( List of formal parameters );// Student s =new Student(); among Student(); It's actually a construction method
Students :
public class Student
{
// Student number
int no;
// full name
String name;
// Age
int age;
// Current Student There is no constructor defined in this class
// But the system will default to Student Class provides a parameterless constructor
// The construction method without parameters ( Default constructor ) Write it down as follows :
public Student(){
System.out.println(" The parameterless construction method is implemented ~");
}
}Test code :
public class OOTest01
{
public static void main(String[] args){
// Call constructor
new Student(); // First execution
// Calling normal methods
dosome();
// establish Student Object of type
Student s =new Student(); // Second execution new Student()
//System.out.println(s.name); // null
// Output " quote s"
// As long as the output is not null This indicates that this object must have been created .
// Output results : [email protected]
System.out.println(s);
}
// Common method
public static void dosome(){
System.out.println("dosome");
}
}Running results :

When defining a constructor with parameters :
public class Student
{
// Student number
int no;
// full name
String name;
// Age
int age;
// Current Student There is no constructor defined in this class
// But the system will default to Student Class provides a parameterless constructor
// The construction method without parameters ( Default constructor ) Write it down as follows :
/*
public Student(){
System.out.println(" The parameterless construction method is implemented ~");
}
*/
// Define a construction method with parameters
public Student(int i){
System.out.println("~");
}
}Test code :
public class OOTest01
{
public static void main(String[] args){
// Call constructor
new Student(); // First execution
// Calling normal methods
dosome();
// establish Student Object of type
Student s =new Student(); // Second execution new Student()
// As long as the output is not null This indicates that this object must have been created .
// Output results : [email protected]
System.out.println(s);
}
// Common method
public static void dosome(){
System.out.println("dosome");
}
}The operation results are as follows :

When defining a parameterless constructor and a parameterless constructor :【 Similar method overloading mechanisms 】
public class Student
{
// Student number
int no;
// full name
String name;
// Age
int age;
// Current Student There is no constructor defined in this class
// But the system will default to Student Class provides a parameterless constructor
// The construction method without parameters ( Default constructor ) Write it down as follows :
public Student(){
System.out.println(" The parameterless construction method is implemented ~");
}
// Define a construction method with parameters
public Student(int i){
System.out.println("~");
}
}Test code :
public class OOTest01
{
public static void main(String[] args){
// Call constructor
new Student(); // First execution
// Calling normal methods
dosome();
// establish Student Object of type
Student s =new Student(); // Second execution new Student()
// As long as the output is not null This indicates that this object must have been created .
// Output results : [email protected]
System.out.println(s);
}
// Common method
public static void dosome(){
System.out.println("dosome");
}
}Running results :

When giving test code Student(); When parameters are passed :
public class OOTest01
{
public static void main(String[] args){
// Call constructor
new Student(100); // First execution
// Calling normal methods
dosome();
// establish Student Object of type
Student s =new Student(100); // Second execution new Student()
// As long as the output is not null This indicates that this object must have been created .
// Output results : [email protected]
System.out.println(s);
}
// Common method
public static void dosome(){
System.out.println("dosome");
}
}Running results :

reflection :【 In the future, try to write the parameterless construction method 】
1、 reflection : When the instance variable is not assigned manually , In fact, the system will assign values by default ,
So when is the default assignment performed ?
Do you assign values to these instance variables when the class is loaded ?
No ( Class loading occurs in the method area At this time, it is not in the stacking area new An object )
Instance variables are initialized during the execution of the constructor , Complete the assignment .
【 Because when new Object time Such as new Student(); Student(); It's actually a construction method 】
public class User
{
int id;
String name;
int age;
// Parameterless construction method
public User(){
// There are actually three lines of code that you can't see .
// Although there is no code written in the body of the parameterless constructor ,
// But in fact, the default value initialization of instance variables is carried out in this method body ( Instance variables can be modified )
/*
id =0;
name ="junker";
age =0;
*/
}
// There are parameter construction methods
public User(int i){
/*
id =0;
name =null;
age =0;
*/
}
}
public class OOTest02
{
public static void main(String[] args){
User u =new User();
System.out.println(u.name);//junker
User a =new User(100);
System.out.println(a.name);//null
}
}Constructor code walkthrough
public class Vip
{
// Membership number
long no;
// Membership name
String name;
// Birthday
String birth;
// Gender
boolean sex;
// Parameterless construction method
public Vip(){
}
// There are parameter construction methods
public Vip(long huiyuanHao){
no =huiyuanHao;
// Default name =null;birth =null;sex =false;
}
public Vip(long huiyuanHao,String xingMing){
no =huiyuanHao;
name =xingMing;
}
public Vip(long huiyuanHao,String xingMing,String shengRi){
no =huiyuanHao;
name =xingMing;
birth =shengRi;
}
public Vip(long huiyuanHao,String xingMing,String shengRi,boolean xingBie){
no =huiyuanHao;
name =xingMing;
birth =shengRi;
sex =xingBie;
}
}public class OOTest03
{
public static void main(String[] args){
Vip v1 =new Vip();
System.out.println(v1.no);//0
System.out.println(v1.name);//null
System.out.println(v1.birth);//null
System.out.println(v1.sex);//false
Vip v2 =new Vip(11111L);
System.out.println(v2.no);//11111L
System.out.println(v2.name);//null
System.out.println(v2.birth);//null
System.out.println(v2.sex);//false
Vip v3 =new Vip(22222L," Little sheep ");
System.out.println(v3.no);//22222L
System.out.println(v3.name);// Little sheep
System.out.println(v3.birth);//null
System.out.println(v3.sex);//false
Vip v4 =new Vip(33333L," The Wolf ","60-6-6");
System.out.println(v4.no);//33333L
System.out.println(v4.name);// The Wolf
System.out.println(v4.birth);//60-6-6
System.out.println(v4.sex);//false
Vip v5 =new Vip(44444L," Iron man ","10-1-1",false);
System.out.println(v4.no);//44444L
System.out.println(v4.name);// Iron man
System.out.println(v4.birth);//10-1-1
System.out.println(v4.sex);//false
}
}边栏推荐
- What are the types of garment ERP system in the market?
- Interview question 02.02 Return the penultimate node
- 机器学习笔记 - 使用TensorFlow的Spatial Transformer网络
- Pytorch installation for getting started with deep learning
- Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
- 86. separate linked list
- Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron
- Interview question 17.10 Main elements
- 1854. the most populous year
- 83. delete duplicate elements in the sorting linked list
猜你喜欢
![[FAQ for novices on the road] about data visualization](/img/a1/d15e286c3c886a8d3a4ac3eb165748.png)
[FAQ for novices on the road] about data visualization

kubelet Error getting node 问题求助

报错Version mismatch between installed depthai lib and the required one by the scrip.

Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s

Talk about how to customize data desensitization

Pytorch installation for getting started with deep learning

MSF SMB based information collection

Typescript -- preliminary study of variable declaration

机器学习笔记 - 使用TensorFlow的Spatial Transformer网络
随机推荐
openstack详解(二十一)——Neutron组件安装与配置
844. compare strings with backspace
【软件】ERP体系价值最大化的十点技巧
OpenCV OAK相机对比及介绍
远程办公最佳实践及策略
[image processing] spatial domain image enhancement
Some learning records I=
1854. the most populous year
[ERP system] how much do you know about the professional and technical evaluation?
206. reverse linked list
MySQL startup error "bind on tcp/ip port: address already in use"
openstack详解(二十四)——Neutron服务注册
2161. divide the array according to the given number
Opencv oak-d-w wide angle camera test
[software] ten skills to maximize the value of ERP system
83. delete duplicate elements in the sorting linked list
Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
Sword finger offer II 041 Average value of sliding window
Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron