Java构造方法定义、对象实例化的过程、对象的引用和实体

Java构造方法定义:

一、在类中,构造方法是一个特殊的方法,当程序用某个类创建对象时,需要使用这个类中的构造方法进行创建;

如下演示: 通过 Structure_Method_Test() 构造方法 创建(实例化)了 类 Structure_Method_Test 的一个对象 smtest 

public class Structure_Method_Test {   
 Structure_Method_Test(){}  //构造方法创建
public static void main(String[] args) {     
 Structure_Method_Test smtest = new Structure_Method_Test();  //对象创建
  }
}

简单的说:构造方法 就是配合 new运算符 实例化一个该类的对象;

二、类中的构造方法的名字必须与类名相同,而且方法是没有类型的;

如下演示: void Structure_Method_Test(){}  //不是构造方法,是类型为void的方法

public class Structure_Method_Test {   

 Structure_Method_Test(){}  //构造方法创建

 void Structure_Method_Test(){}  //该方法不是构造方法,是类型为void的方法

public static void main(String[] args) {     
 Structure_Method_Test smtest = new Structure_Method_Test();  //对象创建
  }
}

三、在一个类中可以同时声明多个构造方法,但是这些构造方法的参数必须是不同的;

(参数不同指:参数的个数不同,名字不同、或 参数个数相同,参数列表中某个个数的类型不同 )
 如下演示:

public class Structure_Method_Test {
	
    Structure_Method_Test(int a){}        //构造方法1

    Structure_Method_Test(int a,int b){}  //构造方法2

    Structure_Method_Test(double a,int b){}   //构造方法3
    
	public static void main(String[] args) {
		
	   Structure_Method_Test smtest = new Structure_Method_Test(3,5);
	   
	}

}

在以上代码中,构造方法1 与 构造方法2 的参数个数不同 ;构造方法2 与 构造方法3 个数相同,但是参数列表中 a 的类型不同;

构造方法特别注意:

一、当程序中某个类没有声明构造方法,那么系统会默认该类只有一个构造方法,并且这个构造方法是没有参数和方法体的,是由系统默认创建的;

如下演示:

class Default_Test{

   Default_Test(){}

}

二、当程序中某个类声明了 多个构造方法,那么系统就不会再为该类创建默认的构造方法了;

如下演示:  Structure_Method_Test smtestone = new Structure_Method_Test();  是错误的

public class Structure_Method_Test {
	
    Structure_Method_Test(int a){}         //构造方法1
    Structure_Method_Test(int a,int b){}   //构造方法2
    
	public static void main(String[] args) {
           //报错,此时是没有默认的构造方法的,所以 () 中的参数必须要和构造方法中参数对应;
           Structure_Method_Test smtestone = new Structure_Method_Test(); 

	   //如下创建的构造方法是正确的	
	   Structure_Method_Test smtest;   //对象的声明
	   smtest = new Structure_Method_Test(1);  //为声明的对象进行分配变量,使用构造方法2
	  
            //声明对象的同时 为 声明的对象进行分配变量,使用构造方法3
	   Structure_Method_Test smtestone = new Structure_Method_Test(11,111); 
           
	}
}

Java构造方法实例化对象过程:

创建一个对象包括 对象的声明 和 为对象分配变量 两个部分;

一、对象的声明

一般格式为:  类名  对象名 ;

如: Default_Test  testone ;   或者  Default_Test   test1 , test2 , test3 ;

二、为对象分配变量,完成对象的实例化

使用 new 运算符和类的构造方法为声明的变量分配变量,即完成对象的实例化;

如下演示:

public class Structure_Method_Test {
	
    Structure_Method_Test(){}              //构造方法1
    Structure_Method_Test(int a,int b){}   //构造方法2
    
	public static void main(String[] args) {
		
	   Structure_Method_Test smtest;   //对象的声明
	   smtest = new Structure_Method_Test();  //为声明的对象进行分配变量,使用构造方法1
	   //声明对象的同时 为 声明的对象进行分配变量,使用构造方法2
	   Structure_Method_Test smtestone = new Structure_Method_Test(3,5);  
           
	}
}

对象的使用:

一、对象不仅可以操作自己的变量改变状态,还可以调用类中的方法产生一定的功能;

二、对象通过使用运算符 " . " ,可以实现对自己 变量的访问 和 方法的调用;(前提是对象创建之后,而不是仅仅声明)

访问格式为:  对象 . 变量名  ; 对象 . 方法名(" ") ;

三、对象体现的封装性,即当对象调用方法时,方法中出现的成员变量就是指分配给该对象的变量;

如下演示:

public class Method_Test {
	double sum,number;
	String speak,action ;
    void behaviour (String st){
    	action = st;
    	sum = 1 + 10 ;
    	System.out.println(action+" " + sum);
    }
    
	public static void main(String[] args) {
		
		Method_Test test1 = new Method_Test();  //创建对象test1
		Method_Test test2 = new Method_Test();  //创建对象test2
		
		test1.number = 123456 ;
		test2.number = 789 ;
		System.out.println("test1的number值="+test1.number);
		System.out.println("test2的number值="+test1.number);
		test1.speak = "这个speak是属于对象test1的";
		test1.speak = "这个speak是属于对象test2的";
		System.out.println("test1的speak值="+test1.speak);
		System.out.println("test2的speak值="+test1.speak);
		test1.behaviour("我想唱歌");
		test2.behaviour("我想跳舞");
	}

}

程序输出:

test1的number值=123456.0
test2的number值=123456.0
test1的speak值=这个speak是属于对象test2的
test2的speak值=这个speak是属于对象test2的
我想唱歌 11.0
我想跳舞 11.0

在以上代码的 behaviour 方法,可以进行操作成员变量 action 与 sum ,当创建的对象 test1,test2 调用该方法时,方法中出现的成员变量分别是 test1,test2 对象的成员变量;

对象的引用和实体:

对象的引用:

   指在创建对象时,new 运算符 只能和 类的构造方法进行运算,运算的结果是一个十六进制数,这是数就说明对象的引用

   如:new Method_Test(); 的值就是一个 引用 ;

对象的实体:

  指在Java中,对象负责存放引用的,以确保对象可以操作分配给该对象的变量以及调用类中的方法,其中分配给该对象的变量也是平时所说的对象的实体

一、没有实体的对象称为空对象,空对象是不能进行使用的,即不能调用方法产生行为;

如下演示:   

public class Method_Test {
	double sum,number;
	String speak,action ;
    void behaviour (String st){
    	action = st;
    	sum = 1 + 10 ;
    	System.out.println(action+" " + sum);
    }
    
	public static void main(String[] args) {
		
		Method_Test test1 = new Method_Test();  //创建对象test1
		Method_Test test2 ;  //声明对象test2,此时对象为空对象
		
		test1.number = 123456 ;
		test2.number = 789 ;     //错误,此时对象为空对象
		System.out.println("test1的number值="+test1.number);
		test1.behaviour("我想唱歌");
		test2.behaviour("我想跳舞");  //错误,此时对象为空对象
	}

}

以上代码中,test2.number 与 test2.behaviour("我想跳舞"); 都是错误的,因为 test2 只是进行了声明,还未创建;

二、若一个类声明的两个对象具有相同的引用,那么二者具有的实体也是一样的;

如下演示:   

public class Method_Test {
	double sum,number;
	String speak,action ;
    void behaviour (String st){
    	action = st;
    	sum = 1 + 10 ;
    	System.out.println(action+" " + sum);
    }
    
	public static void main(String[] args) {
		
		Method_Test test1 = new Method_Test();  //创建对象test1
		Method_Test test2 = new Method_Test();  //声明对象test2
		System.out.println("test1的引用值="+test1);
		System.out.println("test2的引用值="+test2);
                test1 = test2 ;
		System.out.println("test1的引用值="+test1);
		System.out.println("test2的引用值="+test2);
	}

}

代码输出结果:

test1的引用值=Chapter_Four.Method_Test@7852e922
test2的引用值=Chapter_Four.Method_Test@4e25154f
test1的引用值=Chapter_Four.Method_Test@4e25154f
test2的引用值=Chapter_Four.Method_Test@4e25154f

三、Java中 “垃圾回收机制”

垃圾回收机制指系统程序会周期性的检测某个实体是否已不再被任何对象所拥有,如果发现这样的实体,则系统会自动释放实体占有的内存;

如下演示:   

public class Method_Test {
	
	Method_Test(){}
	Method_Test(int x,int y){}
	
	public static void main(String[] args) {
		
		Method_Test test1 = new Method_Test();  //创建对象test1
		Method_Test test2 = new Method_Test(1,11);;  //创建对象test2

		System.out.println("test1的引用值="+test1);
		System.out.println("test2的引用值="+test2);
		 test1 = test2 ;
		System.out.println("test1的引用值="+test1);
		System.out.println("test2的引用值="+test2);
		
	}

}

在以上代码中,执行  test1 = test2 ; 语句后,test1 的 引用就会与 test2 引用一样,而之前创建时 test1 引用中的实体将被系统进行回收;

发布了57 篇原创文章 · 获赞 10 · 访问量 7552

猜你喜欢

转载自blog.csdn.net/LagerSwan/article/details/104122983