目录
1、定义带参数的方法
语法:<访问修饰符> 返回类型 <方法名>(<形式参数列表>) { //方法的主体 }
2、调用带参数的方法
语法:对象名.方法名(参数1, 参数2,……,参数n)
- package cn.bdqn.demo04;
-
- public class Juicer {
- //定义榨汁机的榨汁方法
- public String juicing1(String fruit){
- return fruit+"汁";
- }
-
- //定义榨汁机的第二种榨汁方法
- public String juicing2(String fruit,int num){
- return num+"杯"+fruit+"汁";
- }
-
- public String juicing3(String fruit1,String fruit2,int num){
- return num+"杯"+fruit1+fruit2+"汁";
- }
-
- }
- package cn.bdqn.demo04;
-
- public class Test {
-
- public static void main(String[] args) {
-
- Juicer juicer = new Juicer();
- String result1 =juicer.juicing1("苹果");
- System.out.println(result1);
-
- String result2 =juicer.juicing1("榴莲");
- System.out.println(result2);
-
- String result3 =juicer.juicing2("香蕉",3);
- System.out.println(result3);
-
- String result4=juicer.juicing3("榴莲", "西梅", 5);
- System.out.println(result4);
- }
-
- }
使用带参方法实现学员信息管理
1.增加学员姓名
2.在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
- package cn.bdqn.demo05;
-
- /*
- * 在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
- */
- public class Student {
- public String name;
- public int age;
-
- // 定义一个输出学生信息的方法
- public void printInfo() {
- System.out.println("学生姓名:" + name + ",年龄:" + age);
- }
-
- }
- package cn.bdqn.demo05;
-
- public class StudentUtils {
-
- // 定义一个静态方法,实现在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
- public static boolean searchStudent(Student[] students, int startIndex,
- int endIndex, String name) {
-
- for (int i = startIndex; i <= endIndex; i++) {
- if (students[i].name.equals(name)) {
- return true;
- }
- }
-
- return false;
- }
-
- // 定义遍历输出学生对象数组中的所有学生
- public static void outputAllStudent(Student[] stus) {
- for (int i = 0; i < stus.length; i++) {
- // 直接输出对象,输出的是地址值
- // System.out.println(stus[i]);
- stus[i].printInfo();
- }
- }
-
- //定义方法输出学生对象数组中年龄最大的学生信息
- }
- package cn.bdqn.demo05;
-
- public class Test {
-
- public static void main(String[] args) {
- /*
- * 在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
- */
-
- //创建7个学生对象
- Student stu1 = new Student();
- stu1.name = "张三";
- stu1.age = 19;
-
- Student stu2 = new Student();
- stu2.name = "李四";
- stu2.age = 18;
-
- Student stu3 = new Student();
- stu3.name = "王五";
- stu3.age = 17;
-
- Student stu4 = new Student();
- stu4.name = "赵六";
- stu4.age = 19;
-
- Student stu5 = new Student();
- stu5.name = "孙七";
- stu5.age = 20;
-
- Student stu6 = new Student();
- stu6.name = "吴八";
- stu6.age = 19;
-
- Student stu7 = new Student();
- stu7.name = "王二麻子";
- stu7.age = 21;
-
- //创建学生对象数组
- Student[] stus = {stu1,stu2,stu3,stu4,stu5,stu6,stu7};
-
- boolean result1 =StudentUtils.searchStudent(stus, 1, 5, "李二狗");
- System.out.println("你指定的区间内有李二狗:"+result1);
-
- Student[] stus2 = {stu1,stu2,stu3,stu4};
- boolean result2 =StudentUtils.searchStudent(stus2, 1, 2, "李四");
- System.out.println("你指定的区间内有李四:"+result2);
-
- System.out.println("----------------");
-
- StudentUtils.outputAllStudent(stus2);
- }
-
- }
//方法定义 public void addName(String name){
//方法体
}
//方法调用
对象名.addName(String "张三");
注意:调用方法时不能指定实参类型!
//方法定义 public boolean searchName(int start ,int end ,String name){
//方法体
}
//方法调用
String s="开始";
int e=3;
String name="张三";
boolean flag=对象名. searchName(s ,e ,name);
注意:形参和实参数据类型不一致!数量也要一致!
基本数据类型和引用数据类型数据在传参时区别




基本数据类型,操作传递的是变量的值,改变一个变量的值不会影响另一个变量的值。引用数据类型(类、数组和接口),赋值是把原对象的引用(可理解为内存地址)传递给另一个引用
语法:访问修饰符 构造方法名 ( ) { //初始化代码 }
无返回值类型、方法名与类名相同、可以指定参数
作用:对象初始化
系统提供默认无参构造方法
◆自定义构造方法
◆此时系统不再提供默认无参构造方法
public Student(){}
public Student(String name,int age){
this.name=name; ←——带参构造方法this关键字 是对一个对象的默认
this.age=age; 引用这里用以区分同名成员变量
}
◆this关键字的用法
●调用属性
this.health = 100;
this.name = "大黄";
●调用方法
this.print();
●调用构造方法
this();
this("小黑",100,100,"雄"); ←——如果使用,必须是构造方法 中的第一条语句
●同一个类中
●方法名相同
●参数个数或类型不同
●与返回值、访问修饰符无关
●变量声明的位置决定变量作用域
●变量作用域确定可在程序中按变量名访问该变量的区域
常见错误
public class Test {
int score1 = 88;
int score2 = 98;
public void calcAvg() {
int avg = (score1 + score2)/2;
}
public void showAvg(){
System.out.println("平均分是: " + avg);
}
}
局部变量超出其作用域后不可用!
◆作用域不同
●局部变量的作用域仅限于定义它的方法
●成员变量的作用域在整个类内部都是可见的
◆初始值不同
●Java会给成员变量一个初始值
●Java不会给局部变量赋予初始值
注意:
1、在同一个方法中,不允许有同名局部变量 在不同的方法中,可以有同名局部变量
2、在同一个类中,成员变量和局部变量同名时,局部变量具有更高的优先级