教育路上
摘要:java子程序,作为主程序的一个类,等同于在后台为实现主程序。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。
2023-09-08 09:30周子尧
package 项目三; public class Student1 {//子程序,作为主程序的一个类,等同于在后台为实现主程序 private String name;//名字 private float score1; private float score2; private float score3;//定义三科分数及类型 float ave; float sum; void setname(String name) { thisname=name; } void setscore1(float score1) { thisscore1=score1; } void setscore2(float score2) { thisscore2=score2; } void setscore3(float score3) { thisscore3=score3; } String getname() { return name; } float getscore1() { return score1; } float getscore2() { return score2; } float getscore3() { return score3; } float getave(float ave) { thisave=ave; return ave; } float getsum(float sum) { thissum=sum; return sum; } Student1(){ } Student1(String name,float score1,float score2,float score3){ thisname=name; thisscore1=score1; thisscore2=score2; thisscore3=score3; } void show() { Systemoutprintln("姓名:"+name+" 语文成绩:"+score1+" 数学成绩"+score2+" 英语成绩:"+score3+" 平均分:"+ave+" 总分:"+sum); } }//所有的this*,表示访问类中的成员变量,用来区分成员变量和局部变量 //return表示返回指定类型的值 package 项目三; import 项目三Student1; public class Test1 {//主程序,用于实现相关的输出 @SuppressWarnings("unused") public static void main(String[] args) { Student1 s1=new Student1("张三",905f,865f,695f);//张三数据 s1show(); Student1 s2=new Student1(); s2setname("李四");//李四数据 s2setscore1(805f); s2setscore2(925f); s2setscore3(955f); s2show(); Student1 s3=new Student1("王五",935f,885f,935f);//王五数据 s3show(); float ave=(s1getscore1()+s1getscore2()+s1getscore3())/3;//计算平均分 float sum=(s1getscore1()+s1getscore2()+s1getscore3());//计算总分 }//调用类中的数据实现功能 }
访客的评论 2023/12/09 19:27
文中描述的是准确的吗,如何报名!