全国
Java--教育路上

栏目导航

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

Java 构造方法的定义

摘要:Java 构造方法的定义。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。

标签:
发布时间:
2024-04-28 07:50
信息来源:
网络推荐
浏览次数:
558
Java 构造方法的定义

package lainxi;

public class student {//定义类型和数据名称
private String name;
private float ywscore;
private float sxscore;
private float yyscore;
static String  schoolname;
private float average;
private float average1;

void stexx(String name,float ywscore, float sxscore,float yyscore) {
this.name=name;
this.ywscore=ywscore;
this.sxscore=sxscore;
this.yyscore=yyscore;

}
student(){//无参数构造方法
}

student(String name,float ywscore, float sxscore,float yyscore) {
this.name=name;
this.ywscore=ywscore;
this.sxscore=sxscore;
this.yyscore=yyscore;
}
void count() {//定义平均分的方法
this.average=(ywscore+sxscore+yyscore)/3;//三个变量的平均分

}
float getaverage() {
return average;//获取平均分
}
String dengji() {
String str="";
if(average>90)//用if嵌套
str="优秀";
else if(average>80)
str="良好";
return str;//返回值

}


package lainxi;

import lainxi.student;//导入student包
public class rest {

public static void main(String[] args) {
student.schoolname="重庆大学";//静态变量赋值
student s1=new student();//创建对象
s1.stexx("zhangsahn",89.6f,90.5f,90.9f);
s1.count();//调用平均数
System.out.println("张珊"+s1.schoolname +s1.getaverage()+s1.dengji());//打印第一个学生对象


student s2=new student();
s1.stexx("zhangsahn",89.6f,90.5f,90.9f);
s1.count();//调用平均数
System.out.println("李四"+s1.schoolname +s1.getaverage()+s1.dengji());//打印第二个学生对象

System.out.println(s1.getaverage()+s2.getaverage()/2);//两个学生的平均分
}

}


上一篇:
构造方法为成员变量赋值
下一篇:
java定义一个普通方法调用外部..
相关推荐
最近更新
学校免费发布信息关闭
如有图片请发邮件到:edu63@foxmail.com,审核后显示

 换一张

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


 换一张

确认提交