欢迎光临散文网 会员登陆 & 注册

Java oop 的题与答案6:使用程序模拟英雄金币数,get与set知识,访问修饰符(有图)

2019-12-14 15:59 作者:诗书画唱  | 我要投稿

//使用程序模拟英雄金币数,使用构造函数设置默认值为300,设置set方法可以给每个用户增进金币,

//get获取每个用户的金币数,定义静态变量统计总金币数,创建5个对象,并赋值,

//保存到数组了,求总金币和平均金币

package a;


public class student // public可不写,不写就有默认的修饰访问符


{

public static void main(String[] args) {

student[] stdAry = new student[5];

stdAry[0] = new student("张三", 80);

stdAry[1] = new student("李 四", 70);

stdAry[2] = new student("王 五", 60);

stdAry[3] = new student("王 三", 60);

stdAry[4] = new student("王 四", 60);

for (int i = 0; i < stdAry.length; i++) {


renshu++;


}


System.out.println("总金币为" + student.jinBiShuzs);

System.out.println("平均金币为" + student.jinBiShuzs / renshu);


}


private static int zongchengji;

private static int jinBiShu = 300;// 金币数

private static int jinBiShuzs = 0;

private static int renshu = 0;

private String name;// 姓名

private static int getZongchengji() {

return zongchengji;

}


public static void setZongchengji(int zongchengji) {

student.zongchengji = zongchengji;

}


public static int getJinBiShu() {

return jinBiShu;

}


public static void setJinBiShu(int jinBiShu) {

student.jinBiShu = jinBiShu;

}


public static int getJinBiShuzs() {

return jinBiShuzs;

}


public static void setJinBiShuzs(int jinBiShuzs) {

student.jinBiShuzs = jinBiShuzs;

}


public static int getRenshu() {

return renshu;

}


public static void setRenshu(int renshu) {

student.renshu = renshu;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}




public student(String name, int jinBiShu) {

jinBiShuzs = jinBiShuzs + jinBiShu+300;

this.name = name;

this.jinBiShu = jinBiShu;// 在构造方法里进行赋值

System.out.println("姓名为" + name);


}


}

运行效果:

get与set知识:

访问修饰符:




Java oop 的题与答案6:使用程序模拟英雄金币数,get与set知识,访问修饰符(有图)的评论 (共 条)

分享到微博请遵守国家法律