Java基础知识总结(47)

(4)可变参数

    本质是数组
    可变参数是指方法参数列表不确定
    public class Varargs {
        public static void main(String[] args) {
            //可变参数可以单独传递
            test(100, "张三","李四","王五");
            String[] strs = {"张无忌","赵敏","张三丰"};
            //可变参数可以传入数组
            test(200, strs);
            //也可以不传递
            test(300);
        }
​
        public static void test(int a,String...names) {
            System.out.println(a);
            if(names!=null&&names.length>0) {
                for (String name : names) {
                    System.out.println(name);
                }
            }
        }
    }
    遍历可变参数时,先判断是否为空并且长度大于0

(5)方法重载(overload) 同一个类中才算方法重载 Java中允许有同名方法的存在,这些方法要求方法名称相同,但是参数不同。 方法重载与访问修饰符、返回值类型、抛出异常无关,与参数有关 注意:方法重载中,对于参数而言,参数类型不同 参数的个数不同 与参数名相同与否无关。

课后练习:

1.编写一个学生类,提供name、age、gender、phone、address、email 成员变量。为学生类提供默认的构造器和带所有成员变量的构造器。为学生类提供方法, 用于描绘吃、喝、玩、睡等行为。

/**

 学生类

 @author Ray

* */

public class Student {

     // 学生姓名 String name;

     // 学生年龄 int age;

     // 学生性别 char gender;

     // 学生电话 String phone;

     // 学生地址 String address;

    // 学生邮箱地址 String email;

public Student() {

}

public Student(String name, int age, char gender, String phone, String address, String email) {        this.name = name;

       this.age = age;

       this.gender = gender;

       this.phone = phone;

       this.address = address;

       this.email = email;

}

public void eat() {

        System.out.println(name+"在吃饭");

}

public void drink() {

        System.out.println(name+"在喝水");

}

public void play() {

        System.out.println(name+"在玩耍");

}

public void sleep() {

        System.out.println(name+"在睡觉");

}

}

2.利用第1 题定义的 Student 类,定义一个 Student[数组保存多个 Student 对象作为通讯录数据。 程序可通过 name、email、address 查询,如果找不到数据,则进行友好提示。 /**

  • 测试类

  • @author Ray * */ public class Test1

{

public static void main(String[] args) {

// TODO Auto-generated method stub

Student[] studentArray = new Student[5];
for(int i = 0;i<studentArray.length;i++) {
    switch(i) {
       case 0:
           studentArray[i] = new Student("张三",18,'女',"13830521813","陕西省西安市未央区", "2010188091@qq.com");
           break;
       case 1:
           studentArray[i] = new Student("李四",19,'男',"13830521813","陕西省西安市新城区", "2110188091@qq.com");
           break;
       case 2:
           studentArray[i] = new Student("王五",20,'女',"13830521813","陕西省西安市碑林区", "2210188091@qq.com");
           break;
       case 3:
           studentArray[i] = new Student("赵六",22,'男',"13830521813","陕西省西安市莲湖区", "2310188091@qq.com");
           break;
       case 4:
           studentArray[i] = new Student("宋七",21,'女',"13830521813","陕西省西安市灞桥区", "2410188091@qq.com"); 
           break;
       case 5:
           studentArray[i] = new Student("周八",18,'男',"13830521813","陕西省西安市雁塔区", "2510188091@qq.com"); 
           break;
    }
}
System.out.println("学生查询属性如下:");
System.out.println("***************");
System.out.println("1.姓名");
System.out.println("2.住址");
System.out.println("3.邮箱地址");
System.out.println("***************");
System.out.println("请输入要查找的内容编号:");
Scanner scanner = new Scanner(System.in);
//用户输入的选择项值
int selectionValue =  scanner.nextInt();
switch(selectionValue) {
    case 1:
        System.out.println("请输入学生的姓名:");
        String studentName = scanner.next();
        findStudentByName(studentArray,studentName);
        break;
    case 2:
        System.out.println("请输入学生的住址:");
        String studentAddress = scanner.next();
        findStudentByAddress(studentArray,studentAddress);
        break;
    case 3:
        System.out.println("请输入学生的邮箱地址:");
        String studentEmail = scanner.next();
        findStudentByEmail(studentArray,studentEmail);
        break;
}
scanner.close();    

}

public static void findStudentByName(Student[] studentArray,String studentName) {

boolean flag = false;

for(int i = 0;i<studentArray.length;i++) {

//引用数据类型比较用

equals if(studentArray[i].name.equals(studentName)) {

        System.out.println("查询结果为:");

        printStudentInfo(studentArray[i]);

        flag = true; break;

}

} if(!flag) {

        System.out.println("没有查询到与输入内容相匹配的学生信息,请重新输入!");

}

}

public static void findStudentByAddress(Student[] studentArray,String studentAddress) {

        boolean flag = false;

        for(int i = 0;i<studentArray.length;i++) {

                //引用数据类型比较用

                equals if(studentArray[i].address.equals(studentAddress)) {                         System.out.println("查询结果为:");

                        printStudentInfo(studentArray[i]);

                        flag = true; break;

        }

} if(!flag) {

        System.out.println("没有查询到与输入内容相匹配的学生信息,请重新输入!");

}

}

public static void findStudentByEmail(Student[] studentArray,String studentEmail) {

        boolean flag = false;

        for(int i = 0;i<studentArray.length;i++) {

                //引用数据类型比较用

                equals if(studentArray[i].email.equals(studentEmail)) {

                        System.out.println("查询结果为:");

                        printStudentInfo(studentArray[i]);

                        flag = true; break;

                }

        } if(!flag) {

                        System.out.println("没有查询到与输入内容相匹配的学生信息,请重新输入!");

        }

}

public static void printStudentInfo(Student student){ System.out.println("-----------------------------"); System.out.println("姓名:"+student.name); System.out.println("年龄:"+student.age); System.out.println("性别:"+student.gender); System.out.println("电话:"+student.phone); System.out.println("住址:"+student.address); System.out.println("邮箱地址:"+student.email); System.out.println("-----------------------------");

}

}

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

好教员好

您的鼓励是我前进动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值