全国
Java--教育路上

栏目导航

全国站 > Java > 课堂作业
需求

为成员变量赋值private 构造方法

摘要:为成员变量赋值private 构造方法,为 成员 变量 赋值 private 构造方法。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

发布时间:
2024-05-07 18:44
信息来源:
网络推荐
浏览次数:
295
为成员变量赋值private 构造方法

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();    //调用对象的方法
        
}

}

上一篇:
创建一个圆柱体的类,求圆柱体..
下一篇:
私有化Father类 private
相关推荐
最近更新
学校免费发布信息关闭
如有图片请发邮件到:edu63@foxmail.com,审核后显示

 换一张

确认提交
完善补充本文信息关闭
非常感谢您帮助完善补充本文信息


 换一张

确认提交