类的无参、有参方法


 

类的方法     有什么作用???

我们知道  类是由具有相同属性和共同行为的实体抽象而来的。  对象 执行的操作是通过编写类的方法实现的。类的方法是一个功能模块;有了方法才能实现操作否则就像一个布娃娃不会说也不会动。

 

方法语法:  public 返回值类型    方法名(){

                  //方法的主体

           }

public    void    play(){     //无返回值类型

         System.out.print("狗狗在玩球");

}

方法执行后可能会返回一个结果,该结果的类型称为返回值类型。使用return语句返回值。

return  表达式;

return 语句是跳出语句的一种他做了两件事:

   * 跳出方法  所以一般放在语句的最后

   * 给出结果

public   String    eat(){       //有返回值类型

       String  food="hamburger";

       return   food;   //return  "hamburger";

}

     就这样用 :)

方法调用:语法     对象名.方法名();

package bank.com; public class Lion { public  String corlor="yellow"; public  void run(){ System.out.println("奔跑的狮子"); } public String eat(){ return "喜欢吃 hamburger"; } public String getCorlor() { return corlor; } public String show(){ return "这是个"+getCorlor()+"狮子"; } public void setCorlor(String corlor) { this.corlor = corlor; } }

测试类:

package bank.com; public class LionTest { public static void main(String[] args) { Lion on =new Lion(); System.out.println(on.show()); on.run(); System.out.println("发现一个"+on.eat()+"狮子"); } }

定义带参方法

<访问修饰符>  返回值类型 <方法名>(<参数列表>) {

         //方法的主体

         }

访问修饰符指该方法允许被访问的权限范围,只能是 public  protected或private.

语法    数据类型  参数1 ,数据类型 参数2,数据类型 参数3

 调用带参方法:

调运带参方法和调运无参方法相同 

语法 :对象名.方法名(参数1,参数2,参数3)

实参的类型,顺序都和形参的一一对应。

下面看一个例子:

package bank.com; public class Add { String []names=new String[30];//学生姓名数组
    public void  AddName(String name ){//有参方法 //增加学生姓名
 } public void showname(){//无参方法 //显示全部学生姓名
 } }

带多个参数的方法:查找姓名的方法

package bank.com; public class SreachName { String names[]=new String [30]; public boolean seachName(int start,int end ,String name){ boolean find=false; for (int i=start-1;i<end;i++) { if(names[i].equals(name)){ find =true; break; } } return find; } }
package bank.com;
import java.util.Scanner;
public class SeachTest {
    public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		SreachName as=new SreachName();
		System.out.println("请输入开始查找的位置:");
		int s=input.nextInt();
		System.out.println("请输入结束查找的位置:");
		int e=input.nextInt();
		System.out.println("请输入查找的姓名:");
		String name=input.next();
		System.out.println("**********查找结果*******");
		if(as.seachName(s, e, name)){
			System.out.println("找到了");
		}
		else{
			System.out.println("没找到");
		}
	}
}

  

数组作为参数的方法

package bank.com; public class StudentBiz { /** * 求平均分 * 参赛成绩数组 */
    public double CalAvg(int[] scores){ int sum=0; double avg=0.0; for(int i=0;i<scores.length;i++){ sum=sum+scores[i]; } avg=(double)sum/scores.length; return avg; } /** * 最高分 */
    public int CalMax(int[]scores){ int max=scores[0]; for(int i=0;i<scores.length;i++){ if(scores[i]>max){ max=scores[i]; } } return max; } }

测试类:

package bank.com; import java.util.Scanner; public class Testo1 { public static void main(String[] args) { StudentBiz st=new StudentBiz(); int []scores=new int[5];//保存比赛成绩
        Scanner input=new Scanner(System.in); System.out.println("请输入参赛者的成绩:"); for (int i = 0; i < scores.length; i++) { scores[i]=input.nextInt(); } //输出平均分
        double avgScore=st.CalAvg(scores); System.out.println("平均成绩:"+avgScore); //输出最高分
        int  maxScore=st.CalMax(scores); System.out.println("最高值:"+maxScore); } }

 

对象作为参数的方法

public class Student { public int id; public String name; public int age; public int Score; public void show(){ System.out.println(id+"\t"+age+"\t"+name+"\t"+Score); } }
public class StendentBiz { Student[] students=new Student[30]; /** * 增加学生 */
    public void addStudents(Student stu){ for(int i=0;i<students.length;i++){ if(students[i]==null){ students[i]=stu; break; } } } public void show(){ System.out.println("本班学生列表:"); for(int i=0;i<students.length;i++){ if(students[i]!=null){ students[i].show(); } } System.out.println(); } }
public class Test { public static void main(String[] args) { //实例化学生对象并初始化
        Student Student1=new Student(); Student1.id=10; Student1.name="张三"; Student1.age=18; Student1.Score=34; Student Student2=new Student(); Student2.id=10; Student2.name="林三"; Student2.age=38; Student2.Score=64; //新增学生对象
        StudentsBiz  st=new StudentsBiz(); st.addStudents(Student1); st.addStudents(Student2); st.show();//显示学生信息
 } }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM