当前位置:网站首页>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];
边栏推荐
- 3D建模与处理软件简介 刘利刚 中国科技大学
- Copy baby prompt: material cannot be empty. How to solve it?
- 导电滑环短路的原因以及应对措施
- Global and Chinese market of enterprise wireless LAN 2022-2028: Research Report on technology, participants, trends, market size and share
- QDataStream的简单读写验证
- Use and principle of reentrantlock
- CentOS 7使用yum安装PHP7.0
- How to select conductive slip ring material
- Rust hello-word
- Receiving package install and uninstall events
猜你喜欢
How to hide browser network IP address and modify IP internet access?
In depth understanding of condition source code interpretation and analysis of concurrent programming
複制寶貝提示材質不能為空,如何解决?
Memtable for leveldb source code analysis
Things generated by busybox
智慧运维:基于 BIM 技术的可视化管理系统
Single page application
Unity drags and modifies scene camera parameters under the editor
了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收
LevelDB源码分析之LRU Cache
随机推荐
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
And search: the suspects (find the number of people related to the nth person)
Global and Chinese markets of superconductor 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of search engine optimization (SEO) software 2022-2028: Research Report on technology, participants, trends, market size and share
Software intelligence: the "world" and "boundary" of AI sentient beings in AAAs system
Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
Typeorm framework
JS random verification code
使用 Nocalhost 开发 Rainbond 上的微服务应用
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Unity project experience summary
Set set detailed explanation
QT等待框制作
Rainbond结合NeuVector实践容器安全管理
0xc000007b the application cannot start the solution normally (the pro test is valid)
How to create a progress bar that changes color according to progress
AcWing 887. Finding combinatorial number III (Lucas theorem)
el-form表单新增表单项动态校验;el-form校验动态表单v-if不生效;
Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share