当前位置:网站首页>Daily code 300 lines learning notes day 11

Daily code 300 lines learning notes day 11

2022-07-01 05:26:00 Leeyz_ one

1.Final Variable

①final Keywords can be used for variable declarations , Once the variable is set , You can't change the value of this variable any more . Usually , from final The variables defined are constants . for example , Defined in class PI value , You can use the following statement :

final int x = 1;

② stay Java Define global constants in , Usually use public static final modification , Such constants can only be assigned when the definition is .

public static final int A_Data = 3;

standard : Is defined as final When defining constants of, you need to name them with uppercase letters , And use underline in the middle to connect .

2. The order sheet

2.1  In the sequence table , There is a member variable called length. The program also has length Represents the length of an integer array . actually , The same variable name can be used by different classes , for example : People have weight , Watermelon also has weight . Because it defines different classes 、 Different objects , There will be no conflict between them . Zhang San's weight 、 Li Si's weight , The association is strange . This description is strange , Obviously, this is the case in real life . But this also reflects the characteristics of object-oriented : It fits our human cognition better than process oriented programming , It is also far away from the bottom of the machine .

2.2  toString This method is very special , It covers Object Class . You can see , stay println It uses tempFirstList in , Because you add another string to it , The system will call it automatically tempFirstList.toString().

Quote from RI Lu Java Three hundred lines (11-20 God , Linear data structure )

3. Code

package demo1;

public class SequentialList {

	public static final int MAX_LENGTH = 10;

	int length;

	int[] data;

	public SequentialList() {//  Construction method without parameters 
		length = 0;
		data = new int[MAX_LENGTH];
	}// of the first constructor

	public SequentialList(int[] paraArray) {//  Construction method with parameters , take paraArray Assign a value to data
		data = new int[MAX_LENGTH];
		length = paraArray.length;

		// Cope data.
		for (int i = 0; i < paraArray.length; i++) {
			data[i] = paraArray[i];
		} // Of for i
	}// Of the second constructor

	public String toString() {//  rewrite toString Method 
		String resultString = "";
		if (length == 0) {
			return "empty";
		} // Of if
		for (int i = 0; i < length - 1; i++) {
			resultString += data[i] + ",";
		} // Of for i

		resultString += data[length - 1];
		return resultString;
	}// Of toString

	public void reset() {
		length = 0;
	}// Of reset

	public static void main(String args[]) {
		int[] tempArray = { 1, 4, 6, 9 };
		SequentialList tempFirstList = new SequentialList(tempArray);//  Create an object 
		System.out.println("Initialized,the list is: " + tempFirstList.toString());
		System.out.println("Again,the list is: " + tempFirstList);//  call println Automatically called toString Method 

		tempFirstList.reset();
		System.out.println("After reset,the list is: " + tempFirstList);

	}// of main

}// of class SequentialList

 4. summary

In this toString In the cycle of , If you add an equal sign to the original code , And remove the last single output , The output content will be added with a comma .

public String toString() {//  rewrite toString Method 
		String resultString = "";
		if (length == 0) {
			return "empty";
		} // Of if
		for (int i = 0; i <= length - 1; i++) {
			resultString += data[i] + ",";
		} // Of for i

		//resultString += data[length - 1];
		return resultString;
	}// Of toString

Tested repeatedly , If you want to achieve the output of all content , And without one more comma , Just write another sentence to give the last value

resultString += data[length - 1];

原网站

版权声明
本文为[Leeyz_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160217050659.html

随机推荐