菁英科技(卓目鸟学苑)- 专注软件测试菁英教育
标题:
java-魏清-20210310
[打印本页]
作者:
5269
时间:
2021-3-10 19:58
标题:
java-魏清-20210310
package
day04;
/*
static
静态
修
饰
成
员变
量
:
修
饰
方法:
*/
public class
Demo09 {
public static void
main(String[] args) {
Voter voter1 =
new
Voter();
voter1.
name
=
"
张
三
"
;
//
没
有
static
修
饰
,用
实
例
.
变
量
调
用
voter1.vote();
//
投票
System.
out
.println(
"
当
前票
数
:
"
+Voter.
count
);
//static
修
饰
后,用
类
名
.
变
量
调
用
Voter voter2 =
new
Voter();
voter2.
name
=
"
李四
"
;
voter2.vote();
System.
out
.println(
"
当
前票
数
:
"
+Voter.
count
);
Voter voter3 =
new
Voter();
voter3.
name
=
"
王五
"
;
voter3.vote();
//
非
静态
的方法
调
用,
实
例
.
方法名
Voter.
getCount
();
//
静态
方法的
调
用,
类
名
.
方法名
for
(
int
i=
0
;i<
20
;i++){
Voter v =
new
Voter();
v.
name
=
"
选
民
"
+i;
v.vote();
Voter.
getCount
();
}
}
}
/*
选
民
类
*/
class
Voter{
String
name
;
//
选
民的名字,每
个实
例特有的
属
性
static int
count
;
//
所有
选
民共用的,
属
于
类
的
//
投票方法
public void
vote(){
if
(
count
==
20
){
System.
out
.println(
"
投票活
动结
束。
"
);
}
else
{
System.
out
.println(
name
+
"
感
谢
投票!
"
);
count
++;
}
}
//
静态
方法
public static void
getCount(){
//
非
静态
的成
员变
量,在
静态
方法中不能使用
System.
out
.println(
/*name+*/
"
当
前票
数
:
"
+
count
);
}
//
类
同名
时
,
将
包名
带
上
day01.Demo01
d
=
new
day01.Demo01();
day02.Demo01
d1
=
new
day02.Demo01();
}
欢迎光临 菁英科技(卓目鸟学苑)- 专注软件测试菁英教育 (http://www.zmnxy.com/)
Powered by Discuz! X3.4