Java oop 的題與答案6:使用程序模擬英雄金幣數(shù),get與set知識(shí),訪問修飾符(有圖)

//使用程序模擬英雄金幣數(shù),使用構(gòu)造函數(shù)設(shè)置默認(rèn)值為300,設(shè)置set方法可以給每個(gè)用戶增進(jìn)金幣,
//get獲取每個(gè)用戶的金幣數(shù),定義靜態(tài)變量統(tǒng)計(jì)總金幣數(shù),創(chuàng)建5個(gè)對(duì)象,并賦值,
//保存到數(shù)組了,求總金幣和平均金幣
package a;
public class student // public可不寫,不寫就有默認(rèn)的修飾訪問符
{
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;// 金幣數(shù)
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;// 在構(gòu)造方法里進(jìn)行賦值
System.out.println("姓名為" + name);
}
}
運(yùn)行效果:

get與set知識(shí):

訪問修飾符:



