类的无参、有参方法
?
類的方法 ? ? 有什么作用???
我們知道 ?類是由具有相同屬性和共同行為的實體抽象而來的。 ?對象 執(zhí)行的操作是通過編寫類的方法實現(xiàn)的。類的方法是一個功能模塊;有了方法才能實現(xiàn)操作否則就像一個布娃娃不會說也不會動。
?
方法語法: ?public 返回值類型 ? ?方法名(){
? ? ? ? ? ? ? ? ? //方法的主體
? ? ? ? ? ?}
public ? ?void ? ?play(){ ? ? //無返回值類型
? ? ? ? ?System.out.print("狗狗在玩球");
}
方法執(zhí)行后可能會返回一個結(jié)果,該結(jié)果的類型稱為返回值類型。使用return語句返回值。
return ?表達式;
return 語句是跳出語句的一種他做了兩件事:
? ?* 跳出方法 ?所以一般放在語句的最后
? ?* 給出結(jié)果
public ? String ? ?eat(){ ? ? ? //有返回值類型
? ? ? ?String ?food="hamburger";
? ? ? ?return ? food; ? //return ?"hamburger";
}
? ? ?就這樣用 :)
方法調(diào)用:語法 ? ? 對象名.方法名();
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("發(fā)現(xiàn)一個"+on.eat()+"獅子");}}定義帶參方法
<訪問修飾符> ?返回值類型 <方法名>(<參數(shù)列表>) {
? ? ? ? ?//方法的主體
? ? ? ? ?}
訪問修飾符指該方法允許被訪問的權(quán)限范圍,只能是 public ?protected或private.
語法 ? ?數(shù)據(jù)類型 ?參數(shù)1 ,數(shù)據(jù)類型?參數(shù)2,數(shù)據(jù)類型 參數(shù)3
?調(diào)用帶參方法:
調(diào)運帶參方法和調(diào)運無參方法相同?
語法 :對象名.方法名(參數(shù)1,參數(shù)2,參數(shù)3)
實參的類型,順序都和形參的一一對應(yīng)。
下面看一個例子:
package bank.com; public class Add { String []names=new String[30];//學生姓名數(shù)組public void AddName(String name ){//有參方法//增加學生姓名 }public void showname(){//無參方法//顯示全部學生姓名 } }帶多個參數(shù)的方法:查找姓名的方法
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("請輸入結(jié)束查找的位置:");int e=input.nextInt();System.out.println("請輸入查找的姓名:");String name=input.next();System.out.println("**********查找結(jié)果*******");if(as.seachName(s, e, name)){System.out.println("找到了");}else{System.out.println("沒找到");}} }
數(shù)組作為參數(shù)的方法
package bank.com;public class StudentBiz {/*** 求平均分* 參賽成績數(shù)組*/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("請輸入?yún)①愓叩某煽?#xff1a;");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);} }?
對象作為參數(shù)的方法
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();//顯示學生信息 } }?
轉(zhuǎn)載于:https://www.cnblogs.com/cuixiaomeng/p/6744488.html
總結(jié)
- 上一篇: C#之Directory类、Direct
- 下一篇: hadoop 2.x HA(QJM)安装