Skip to content

Commit fc9d954

Browse files
committed
class_method__类方法
1 parent 6a5d247 commit fc9d954

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

README.md

Lines changed: 15 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-12 21:44:12
5+
* @LastEditTime: 2020-09-12 23:28:40
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)
@@ -131,6 +131,20 @@ java编程基础 面向对象 javaAPI 集合 IO GUI JD8C 多线程 网络编程
131131
- [x] [concat_string__拼接字符串](javaAPI/String/concat_string.java)
132132
- [x] [charAt_String__统计字符个数](javaAPI/String/charAt_String.java)
133133

134+
### [static](javaAPI/static)
135+
136+
- [x] [class_variable__类变量](javaAPI/static/class_variable.java)
137+
- [x] [class_method__类方法](javaAPI/static/class_method.java)
138+
139+
140+
### [Arrays](javaAPI/Arrays)
141+
142+
143+
144+
### [Math](javaAPI/Math)
145+
146+
147+
134148
-----------------------
135149

136150
## [异常处理]()

javaAPI/Static/class_method.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @由于个人水平有限, 难免有些错误, 还请指点:
3+
* @Author: cpu_code
4+
* @Date: 2020-09-12 23:28:50
5+
* @LastEditTime: 2020-09-12 23:29:24
6+
* @FilePath: \java\javaAPI\Static\class_method.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.Static;
13+
14+
public class class_method {
15+
public static void main(String[] args){
16+
Student s1 = new Student("cpucode", 22);
17+
Student s2 = new Student("cpu", 21);
18+
19+
s1.show();
20+
s2.show();
21+
22+
// 访问类变量
23+
System.out.println(Student.numberOfStudent);
24+
25+
// 调用静态方法
26+
Student.showNum();
27+
}
28+
}
29+
30+
class Student {
31+
private String name;
32+
private int age;
33+
34+
// 学生的id
35+
private int sid;
36+
// 类变量,记录学生数量,分配学号
37+
public static int numberOfStudent = 0;
38+
39+
public Student(String name , int age){
40+
this.name = name;
41+
this.age = age;
42+
// 通过 numberOfStudent 给学生分配学号
43+
this.sid = ++numberOfStudent;
44+
}
45+
46+
// 打印属性值
47+
public void show(){
48+
System.out.println("Student: name = " + name + ", age = " + age + ", sid = " + sid);
49+
}
50+
51+
public static void showNum() {
52+
System.out.println("num == " + numberOfStudent);
53+
}
54+
}
55+
56+
/*
57+
Student: name = cpucode, age = 22, sid = 1
58+
Student: name = cpu, age = 21, sid = 2
59+
2
60+
num == 2
61+
*/

javaAPI/Static/class_variable.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @由于个人水平有限, 难免有些错误, 还请指点:
3+
* @Author: cpu_code
4+
* @Date: 2020-09-12 22:29:45
5+
* @LastEditTime: 2020-09-12 22:38:27
6+
* @FilePath: \java\javaAPI\static\class_variable.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.Static;
13+
14+
public class class_variable {
15+
public static void main(String[] args){
16+
Student s1 = new Student("cpucode", 22);
17+
Student s2 = new Student("cpu", 21);
18+
19+
s1.show();
20+
s2.show();
21+
}
22+
}
23+
24+
class Student {
25+
private String name;
26+
private int age;
27+
28+
// 学生的id
29+
private int sid;
30+
// 类变量,记录学生数量,分配学号
31+
public static int numberOfStudent = 0;
32+
33+
public Student(String name , int age){
34+
this.name = name;
35+
this.age = age;
36+
// 通过 numberOfStudent 给学生分配学号
37+
this.sid = ++numberOfStudent;
38+
}
39+
40+
// 打印属性值
41+
public void show(){
42+
System.out.println("Student: name = " + name + ", age = " + age + ", sid = " + sid);
43+
}
44+
}
45+
46+
/*
47+
Student: name = cpucode, age = 22, sid = 1
48+
Student: name = cpu, age = 21, sid = 2
49+
*/

0 commit comments

Comments
 (0)