方法的返回值
如果方法具有返回值,方法中必须使用关键字return返回该值
如果方法没有返回值,返回值的类型为void
返回值的作用:跳出方法,返回结果
返回值只能有一个
方法调用
方法之间允许相互调用,不需要知道方法的具体实现,实现重用,提高效率
有两种情况,以student类中的方法为例
①、student类的方法a(),调用student类的方法b,可以直接调用
public class Student {
//定义方法a
public void a(){
System.out.println("我是Student类中的方法a");
}
public void b(){
System.out.println("我是Student类中的方法b");
a();
}
}
②、 student类的方法a()调用Teacher类的方法b(),先创建类对象,然后使用“.”调用
public class Teacher {
//定义方法c
public void c() {
System.out.println("我是Teacher类里的方法c");
//调用Student类里的方法a,不能直接调用,需要使用Student对象调用
//a();
Student stu = new Student();
stu.a();
}
}
区别成员变量和局部变量
如果局部变量的名字与成员变量的名字相同,则成员变量被隐藏,即该成员变量在这个方法内暂时失效,如:判断用户的登录名和密码,正确的话可以修改密码,错误输出密码错误
源代码:
//user类
public class User {
String userName;
int password;
//定义方法
public void changePwd(String name,int pwd){
if(name.equals(userName)&&pwd==password){
System.out.println("请输入新密码:");
Scanner sc = new Scanner(System.in);
password = sc.nextInt();
System.out.println("修改密码成功,新密码为:"+password);
}else{
System.out.println("用户名或密码不正确,不能修改密码");
}
}
}
//测试代码
public class UserTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
User user = new User();
user.userName = "admin";
user.password = 123456;
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入密码:");
int pwd = sc.nextInt();
user.changePwd(name, pwd);
System.out.println(user.password);
}
}
这里面形参的名字和属性名是不一样的,这样是可以正常输出的,如果把名字改成一样的
public class User {
String userName;
int password;
// 定义方法
public void changePwd(String userName, int password) {
if (userName.equals(userName) && password == password) {
System.out.println("请输入新密码:");
Scanner sc = new Scanner(System.in);
password = sc.nextInt();
System.out.println("修改密码成功,新密码为:" + password);
} else {
System.out.println("用户名或密码不正确,不能修改密码");
}
}
}
运行结果就不一样了
这是因为:如果局部变量的名字与成员变量的名字相同,则成员变量被隐藏,即该成员变量在这个方法内暂时失效,如果想在改方法中使用被隐藏的成员变量,必须使用关键字this(后面会讲)
也就是改成这样滴
public class User {
String userName;
int password;
// 定义方法
public void changePwd(String userName, int password) {
if (userName.equals(this.userName) && password == this.password) {
System.out.println("请输入新密码:");
Scanner sc = new Scanner(System.in);
this.password = sc.nextInt();
System.out.println("修改密码成功,新密码为:" + this.password);
} else {
System.out.println("用户名或密码不正确,不能修改密码");
}
}
}
带参方法
为什么要使用带参方法:不同对象对同一方法产生不同的行为。比如用榨汁机榨汁汁,不同的水果汁是不一样的
如何使用带参数的方法:
定义参数的方法
<访问修饰符> 返回类型 <方法名>(<形式参数列表>) {
//方法的主体
}
调用参数的方法
对象名.方法名(参数1,参数2,……,参数n);
常见的错误
①//方法定义
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);
形参和实参数据类型不一致,数量,顺序都要一致
方法传参
方法中最重要的部分之一就是方法的参数,参数属于局部变量,当对象调用方法时,参数被分配内存空间,并要求调用者向参数传递值,即方法被调用时,参数变量必须有具体的值,局部变量是在调用方法时分配内存
基本数据类型和引用数据类型数据在传参时的区别
1.基本数据类型,操作传递的是变量的值,改变一个变量的值不会影响另一个变量的值。就像老马找我借数据,我觉得老马不靠谱所以就拷贝一份发给他,那份文件老马怎么搞都跟我电脑里的数据无关,我的数据也不会因为老马的操作而改变
2.引用数据类型(类、数组和接口),赋值是把原对象的引用(可理解为内存地址)传递给另一个引用。就像婷婷又找我要数据,我觉得婷婷靠谱,我就直接把电脑借给她操作,她的操作对我的数据是由影响的
public class Test {
public void calc1(int num){
num=num+1;
}
public void calc2(Student student){
student.age=student.age+1;
}
}
//测试类
Test test=new Test();
int n=8;
test.calc1(n);
Student stu=new Student();
stu.age=18;
test.calc2(stu);
System.out.println(n+"---"+stu.age);
输出的结果是 8-----19
对象数组做参数
例:在保存了多个学生姓名的数组中,指定查找区间,查找某个学生姓名并显示是否查找成功
public class Student {
String name;
int age;
public static boolean searchStudebt(Student[] student,String stuName,int startIndex,int endIndex){
int count=0;
for (int i = startIndex; i <endIndex ; i++) {
if(stuName.equals(student[i].name)){
return true;
}else{
count++;
if(count==endIndex-startIndex+1){
return false;
}
}
}
return false;
}
}
//测试类
public class StudentTest {
public static void main(String[] args) {
Student stu1=new Student();
stu1.name="马力";
stu1.age=66;
Student stu2=new Student();
stu2.name="牛力";
stu2.age=27;
Student stu3=new Student();
stu3.name="羊力";
stu3.age=88;
Student stu4=new Student();
stu4.name="鹿力";
stu4.age=29;
Student stu5=new Student();
stu5.name="虎力";
stu5.age=27;
Student[] stu={stu1,stu2,stu3,stu4,stu5};
boolean result=Student.searchStudebt(stu, "马力", 3, 4);
System.out.println("您的搜索结果是:"+result);
}
}
方法实现了一段代码的重复使用