结论!!!
学生类当中虽然没有构造方法 但是测试代码当中Student对象也创建完成了。是因为当类中没有任何构造方法的时候系统默认构造一个无参数的构造方法
构造方法和普通方法结构的区别如下:
[修饰符列表] 构造方法名(形式参数列表){
构造方法体;
// 通常在构造方法体当中给属性赋值,完成属性的初始化。
}
注意:
第一:修饰符列表目前统一写成:public 千万不能写成public static
第二:构造方法名和类名必须一致
第三: 构造方法不需要指定返回值类型,也不能写void,写的话成了普通方法。普通方法结构为:
[修饰符列表] 返回值类型 方法名(形式参数列表){
方法体;
}
注意: 修饰符列表为: public static
调用构造方法怎么调用呢?
构造方法和普通方法调用的区别:
普通方法:
public class OOTest01
{
public static void main(String[] args){
// 调用普通方法
dosome(); // 同一个类当中不用 类名.dosome(); 也可以
// 普通方法
public static void dosome(){
System.out.println("dosome");
}
}
构造方法:
使用new运算符来调用构造方法。
语法格式:
new 构造方法名(形式参数列表);// Student s =new Student();当中 Student(); 实际上是一个构造方法
学生类:
public class Student
{
//学号
int no;
//姓名
String name;
//年龄
int age;
//当前的Student这个类当中并没有定义任何构造方法
//但是系统会默认的给Student类提供一个无参数的构造方法
// 将无参数的构造方法(缺省构造器)写出来如下:
public Student(){
System.out.println("无参数构造方法执行了~");
}
}
测试代码:
public class OOTest01
{
public static void main(String[] args){
// 调用构造方法
new Student(); // 第一次执行
// 调用普通方法
dosome();
// 创建Student类型的对象
Student s =new Student(); // 第二次执行 new Student()
//System.out.println(s.name); // null
// 输出"引用s"
// 只要输出结果不是null 说明这个对象一定是创建完成了。
// 输出结果: Student@15db9742
System.out.println(s);
}
// 普通方法
public static void dosome(){
System.out.println("dosome");
}
}
运行结果:
当定义一个有参数的构造方法时:
public class Student
{
//学号
int no;
//姓名
String name;
//年龄
int age;
//当前的Student这个类当中并没有定义任何构造方法
//但是系统会默认的给Student类提供一个无参数的构造方法
//将无参数的构造方法(缺省构造器)写出来如下:
/*
public Student(){
System.out.println("无参数构造方法执行了~");
}
*/
//定义一个有参数的构造方法
public Student(int i){
System.out.println("~");
}
}
测试代码:
public class OOTest01
{
public static void main(String[] args){
// 调用构造方法
new Student(); // 第一次执行
// 调用普通方法
dosome();
// 创建Student类型的对象
Student s =new Student(); // 第二次执行 new Student()
// 只要输出结果不是null 说明这个对象一定是创建完成了。
// 输出结果: Student@15db9742
System.out.println(s);
}
// 普通方法
public static void dosome(){
System.out.println("dosome");
}
}
运行结果如下:
当定义一个无参数构造方法和有参数构造方法时:【类似方法重载机制】
public class Student
{
//学号
int no;
//姓名
String name;
//年龄
int age;
//当前的Student这个类当中并没有定义任何构造方法
//但是系统会默认的给Student类提供一个无参数的构造方法
//将无参数的构造方法(缺省构造器)写出来如下:
public Student(){
System.out.println("无参数构造方法执行了~");
}
//定义一个有参数的构造方法
public Student(int i){
System.out.println("~");
}
}
测试代码:
public class OOTest01
{
public static void main(String[] args){
// 调用构造方法
new Student(); // 第一次执行
// 调用普通方法
dosome();
// 创建Student类型的对象
Student s =new Student(); // 第二次执行 new Student()
// 只要输出结果不是null 说明这个对象一定是创建完成了。
// 输出结果: Student@15db9742
System.out.println(s);
}
// 普通方法
public static void dosome(){
System.out.println("dosome");
}
}
运行结果:
当给测试代码Student();传参数时:
public class OOTest01
{
public static void main(String[] args){
// 调用构造方法
new Student(100); // 第一次执行
// 调用普通方法
dosome();
// 创建Student类型的对象
Student s =new Student(100); // 第二次执行 new Student()
// 只要输出结果不是null 说明这个对象一定是创建完成了。
// 输出结果: Student@15db9742
System.out.println(s);
}
// 普通方法
public static void dosome(){
System.out.println("dosome");
}
}
运行结果:
思考:【以后尽量把无参数构造方法写出来】
1、思考:实例变量没有手动赋值的时候,实际上系统会默认赋值,
那么这个默认赋值操作是在什么时间进行的?
是在类加载的时候给这些实例变量赋值的吗?
不是(类加载发生在方法区 这时候还没有在堆区new一个对象)
实例变量是在构造方法执行的过程中完成初始化的,完成赋值的。
【因为当new对象时 如new Student(); Student();实际上是一个构造方法】
public class User
{
int id;
String name;
int age;
// 无参数构造方法
public User(){
//这里实际上有三行代码你看不见。
//无参数构造方法体当中虽然什么代码都没写,
//但是实际上是在这个方法体里面进行的实例变量默认值初始化(实例变量可进行修改)
/*
id =0;
name ="junker";
age =0;
*/
}
// 有参数构造方法
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
}
}
构造方法代码演练
public class Vip
{
//会员号
long no;
//会员姓名
String name;
//生日
String birth;
//性别
boolean sex;
//无参数构造方法
public Vip(){
}
//有参数构造方法
public Vip(long huiyuanHao){
no =huiyuanHao;
//默认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,"小绵羊");
System.out.println(v3.no);//22222L
System.out.println(v3.name);//小绵羊
System.out.println(v3.birth);//null
System.out.println(v3.sex);//false
Vip v4 =new Vip(33333L,"大灰狼","60-6-6");
System.out.println(v4.no);//33333L
System.out.println(v4.name);//大灰狼
System.out.println(v4.birth);//60-6-6
System.out.println(v4.sex);//false
Vip v5 =new Vip(44444L,"钢铁侠","10-1-1",false);
System.out.println(v4.no);//44444L
System.out.println(v4.name);//钢铁侠
System.out.println(v4.birth);//10-1-1
System.out.println(v4.sex);//false
}
}