java类 举例分析_java基础学习05(面向对象基础01--类实例分析)

面向对象基础01(类实例分析)

实现的目标

1.如何分析一个类(类的基本分析思路)

分析的思路

1.根据要求写出类所包含的属性

2.所有的属性都必须进行封装(private)

3.封装之后的属性通过setter和getter设置和取得

4.如果需要可以加入若干构造方法

5.再根据其它要求添加相应的方法

6.类中的所有方法都不要直接输出,而是交给被调用处调用

Demo

定义并测试一个名为Student的类,包括属性有"学号"、"姓名"以及3门课程"数学"、"英语","计算机"的成绩,包括的方法有计算3门课程的"总分"、"平均分"、"最高分"和"最低分"。

1.本类的属性及类型

b4f6ieD.png

2.定义出需要的方法(构造方法、普通方法)

BJqy5d6.png

3.类图

29d1c53805ee63599cf27c70eb6b461b.png

4.编写代码

/**

*1.定义一个Student类并测试该类

*/

class Student{

private String stuid; //学号

private String name; //姓名

private float math; //数学成绩

private float english; //英语成绩

private float computer; //计算机成绩

public Student(){

}

//定义5个参数的构造方法,为类中的属性初始化

public Student(String stuid,String name,float math,float english,float computer){

this.stuid = stuid;

this.name = name;

this.math = math;

this.english = english;

this.computer = computer;

}

public void setStuid(String stuid){

this.stuid = stuid;

}

public String getStuid(){

return this.stuid;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return this.name;

}

public void setMath(float math){

this.math = math;

}

public float getMath(){

return this.math;

}

public void setEnglish(float english){

this.english = english;

}

public float getEnglish(){

return this.english;

}

public void setComputer(float computer){

this.computer = computer;

}

public float getComputer(){

return this.computer;

}

public float sum(){

float sum = this.math + this.english + this.computer;

return sum;

}

public float avg(){

float avg = (this.math + this.english + this.computer)/3;

//float avg = this.sum()/3;

return avg;

}

public float max(){

float max = this.math;

max = this.english > max ? this.english:max;

max = this.computer > max ? this.computer:max;

return max;

}

public float min(){

float min = this.math;

min = this.english < min ? this.english:min;

min = this.computer < min ? this.computer:min;

return min;

}

}

/**

*1.编写测试类,测试以上代码

*/

class Demo01{

public static void main(String [] args){

Student stu1 = new Student("1114020116","张三",95.0f,78.0f,85.0f); //实例化Student对象并通过构造方法赋值

System.out.println("学号:"+stu1.getStuid());

System.out.println("姓名:"+stu1.getName());

System.out.println("数学成绩:"+stu1.getMath());

System.out.println("英语成绩:"+stu1.getEnglish());

System.out.println("计算机成绩:"+stu1.getComputer());

System.out.println("成绩总和:"+stu1.sum());

System.out.println("成绩平均值:"+stu1.avg());

System.out.println("最高分:"+stu1.max());

System.out.println("最低分:"+stu1.min());

}

}

Process started >>>

学号:1114020116

姓名:张三

数学成绩:95.0

英语成绩:78.0

计算机成绩:85.0

成绩总和:258.0

成绩平均值:86.0

最高分:95.0

最低分:78.0

<<< Process finished. (Exit code 0)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值