Java-关于类的方法的各种代码(二)

数组类型的方法返回值

package com.leiheduixiang.a;

import java.util.Arrays;
                                        //
public class Student {
	String names[]= {"Tom","Jack","Bell","Mick","Lucy"};
	public String[] OrderNames() {                              //
		Arrays.sort(names);                                     //
		return names;                                           //
	}	
}



package com.leiheduixiang.a;

public class Test {
	public static void main(String[] args) {
		Student student=new Student();
		System.out.println("排序之前的学生姓名分别是:");
		for(int i=0;i<student.names.length;i++) {             //
			System.out.println(student.names[i]+" ");
		}
		student.OrderNames();                                  //
		System.out.println("排序之后的学生姓名分别是:");
		for(int i=0;i<student.names.length;i++) {
			System.out.println(student.names[i]+" ");
		}
	}
}

对象类型的参数

package com.leiheduixiang.a;

public class Student2 {
	public float java,html,mysql;                //

}



package com.leiheduixiang.a;

public class Score {
	public float GetAvg(Student2 student) {                         //
		float avg=(student.java+student.html+student.mysql)/3;
		return avg;
	}
}



package com.leiheduixiang.a;

public class Test2 {
  public static void main(String[] args) {
	Student2 student=new Student2();                                    //
	student.java=88;
	student.html=90;
	student.mysql=81;
	Score score=new Score();
	float avg=score.GetAvg(student);
	System.out.println("该学生的平均分是:"+avg);
      }
}

对象数组类型的参数

package com.leiheduixiang.a;
                                        
public class Student {
	public float height;
	
}



package com.leiheduixiang.a;

public class Height {
	public float GetAvgHeight(Student[] stus) {
		float total=0;
		int num=0;
		for(int i=0;i<stus.length;i++) {
			float height=stus[i].height;
			if(height>50) {
				total+=height;
				num++;
			}
		}
		float avg=total/num;
		return avg;
	}
}



package com.leiheduixiang.a;

public class Test {
	public static void main(String[] args) {
		Student[] stus=new Student[3];        
		stus[0]=new Student();                     //注意要初始化
		stus[0].height=170;
		stus[1]=new Student();
		stus[1].height=168;
		stus[2]=new Student();
		stus[2].height=175;
		
		Height height=new Height();
		float avg=height.GetAvgHeight(stus);
		System.out.println("学生的平均身高是:"+avg);		
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我说、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值