Skip to content

Commit 8ceb737

Browse files
committed
anonymous3__匿名对象
1 parent ccb4421 commit 8ceb737

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @由于个人水平有限, 难免有些错误, 还请指点:
33
* @Author: cpu_code
44
* @Date: 2020-07-12 12:03:11
5-
* @LastEditTime: 2020-09-13 14:16:10
5+
* @LastEditTime: 2020-09-13 14:28:19
66
* @FilePath: \java\README.md
77
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
88
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
@@ -109,6 +109,8 @@ java编程基础 面向对象 javaAPI 集合 IO GUI JD8C 多线程 网络编程
109109
- [x] [nextInt_max__最大值](javaAPI/Scanner/nextInt_max.java)
110110
- [ ] [anonymous1__匿名对象参数](javaAPI/Scanner/anonymous1.java)
111111
- [ ] [anonymous2__匿名对象返回值](javaAPI/Scanner/anonymous2.java)
112+
- [ ] [anonymous3__匿名对象](javaAPI/Scanner/anonymous3.java)
113+
112114

113115
### [Random](javaAPI/Random)
114116

javaAPI/Scanner/anonymous3.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @由于个人水平有限, 难免有些错误, 还请指点:
3+
* @Author: cpu_code
4+
* @Date: 2020-09-13 14:22:56
5+
* @LastEditTime: 2020-09-13 14:26:48
6+
* @FilePath: \java\javaAPI\Scanner\anonymous3.java
7+
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
8+
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
9+
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
10+
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
11+
*/
12+
package javaAPI.Scanner;
13+
14+
/*
15+
创建对象的标准格式:
16+
类名称 对象名 = new 类名称();
17+
18+
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
19+
new 类名称();
20+
21+
注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
22+
使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象。
23+
*/
24+
25+
public class anonymous3 {
26+
public static void main(String[] args){
27+
28+
// 左边的one就是对象的名字
29+
Person one = new Person();
30+
one.name = "cpucode";
31+
one.showName();
32+
33+
System.out.println("-----------");
34+
35+
//匿名对象
36+
new Person().name = "cpu";
37+
new Person().showName();
38+
}
39+
}
40+
41+
class Person{
42+
String name;
43+
44+
public void showName(){
45+
System.out.println("我叫 :" + name);
46+
}
47+
}
48+
49+
/*
50+
我叫 :cpucode
51+
-----------
52+
我叫 :null
53+
*/

0 commit comments

Comments
 (0)