教育路上
摘要:为成员变量赋值private 构造方法,为 成员 变量 赋值 private 构造方法。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。
2023-01-05 10:18张渝婷
package zhang7; public class Ysystudent { private String name; private float ywscore; //将语文成绩私有化 private float sxscore; //将数学成绩私有化 private float yyscore; //将英语成绩私有化 private float average; //平均值 private float sum; //总分 void setname(String name) { //为成员变量赋值 thisname=name; } void setywscore(float ywscore) { thisywscore=ywscore; } void setsxscore(float sxscore) { thissxscore=sxscore; } void setyyscore(float yyscore) { thisyyscore=yyscore; } //为成员变量赋值 String getname() { return name; } float getywscore() { return ywscore; } float getsxscore() { return sxscore; } float getyyscore() { return yyscore; } Ysystudent(String name,float ywscore,float sxscore,float yyscore){ thisname=name; thisywscore=ywscore; thissxscore=sxscore; thisyyscore=yyscore; } //对属性赋值 public void average() { average=(getywscore()+getsxscore()+getyyscore())/3; } public void sum() { sum=getywscore()+getsxscore()+getyyscore(); } void show() { Systemoutprintln("姓名:"+name+"语文成绩:"+ywscore+"数学成绩:"+sxscore+"英语成绩:"+yyscore+"平均分:"+average+"总成绩:"+sum); } } //测试主类 package zhang7; import zhang7Ysystudent; public class Ysytest { public static void main(String[] args) { // TODO 自动生成的方法存根 Ysystudent s=new Ysystudent("xz1",905f,884f,775f); //创建学生对象 saverage(); ssum(); sshow(); //调用对象的方法 Ysystudent s1=new Ysystudent("xz2",855f,995f,886f); //创建学生对象 s1average(); s1sum(); s1show(); //调用对象的方法 Ysystudent s2=new Ysystudent("xz3",943f,874f,778f); //创建学生对象 s2average(); s2sum(); s2show(); //调用对象的方法 } }
访客的评论 2024/04/29 05:13
文中描述的是准确的吗,如何报名!