- 发布时间:
- 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定义一个普通方法调用外部..