菁英科技(卓目鸟学苑)- 专注软件测试菁英教育

标题: 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