import关键字、项目0.5

java面向对象学习day09

不经一番彻骨寒,怎得梅花扑鼻香。

一、import关键字

1、说明

  1. 在源文件中显式的使用import结构导入指定的包下的类、接口

  2. 声明在包的声明和类的声明之间

  3. 如果需要导入多个结构,则并列写出即可

  4. 可以使用“xxx.*”的方式,表示可以导入xxx包下的所有结构

  5. 如果使用的类或接口是java.lang(比较常用)包下定义的,则可以省略import结构

  6. 如果使用的类或接口是本包下定义的,则可以省略import结构

  7. 如果在源文件中,使用了不同包下的同名的类,则必须至少有一个类需要以全类名的方式显示。如下:

    Date date = new Date();

    java.sql.Date date1 = new java.sql.Date(2121221221122L)

  8. 使用“xxx.*”方式表明可以调用xxx包下的所有结构,但是如果使用的是xxx子包下的结构,则仍需要显式导入。如:

    import java.lang.reflect.Field;

  9. import static : 导入指定类或接口中的静态结构(属性或方法)。(不常用)例如:

     import static java.lang.System.*;
     import static java.lang.Math.*;
     ​
     out.println("hello");
     long num = round(123.456);

二、复习

1、封装性的体现

  • 体现一:将类的属性xxx私有化(private),同时,提供公共的(public)方法来获取(getxxx)和设置(setxxx)此属性的值

  • 体现二:不对外暴露的私有的方法

  • 体现三:单例模式(将构造器私有化)

  • 体现四:如果不希望类在包外被调用,可以将类设置为缺省的

2、Java规定的四种权限修饰符的权限大小

private < 缺省 < protected < public

3、构造器

1、说明:

  • 只要造对象,就得用构造器。

  • 构造器作用:创建对象+初始化对象的信息。

  • 默认的构造器的权限修饰符,与所属的类的权限修饰符一致。

  • 一个类中至少有一个构造器

2、属性赋值的顺序:

  1. 默认初始化

  2. 显式初始化

  3. 构造器中初始化

  4. 通过 “对象.方法” 或 “对象.属性” 赋值

注:可简记为,就近原则

三、项目0.5

CMUtillity类:

 package yangtingp2util;
 ​
 import java.util.*;
 ​
 public class CMUtility {
     private static Scanner scanner = new Scanner(System.in);
     //界面菜单的选择,读取用户在键盘输入的字符
     public static char readMenuSelection(){
         char c;
         for(;;){
             String str = readKeyBoard(1,false);
             c = str.charAt(0);
             if(c != '1' && c != '2' && c != '3' && c != '4' && c != '5'){
                 System.out.println("选择错误,请重新输入:");
             } else break;
         }
         return c;
     }
 ​
     //从键盘读取一个字符,并将其作为方法的返回值
     public static char readChar(){
         String str = readKeyBoard(1,false);
         return str.charAt(0);
     }
 ​
     //从键盘读取一个字符,并将其作为方法的返回值
     //如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
     public static char readChar(char defaultValue){
         String str = readKeyBoard(1,true);
         return (str.length() == 0) ? defaultValue : str.charAt(0);
     }
 ​
     //从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值
     public static int readInt(){
         int n;
         for(;;){
             String str = readKeyBoard(2,false);
             try{
                 n = Integer.parseInt(str);
                 break;
             } catch (NumberFormatException e){
                 System.out.println("数字输入错误,请重新输入:");
             }
         }
         return n;
     }
 ​
     //从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值
     //如果用户不输入字符而直接回车,方法将以defaultValue最为返回值
     public static int readInt(int defaultValue){
         int n;
         for(;;){
             String str = readKeyBoard(2,true);
             if(str.equals("")){
                 return defaultValue;
             }
             try{
                 n = Integer.parseInt(str);
                 break;
             }catch(NumberFormatException e){
                 System.out.println("数字输入错误,请重新输入:");
             }
         }
         return n;
     }
 ​
     //从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
     public static String readString(int limit){
         return readKeyBoard(limit,false);
     }
 ​
     //从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值
     //如果用户不输入字符而直接回车,方法将以defauValue作为返回值。
     public static String readString(int limit,String defaultValue){
         String str = readKeyBoard(limit,true);
         return str.equals("") ? defaultValue :str;
     }
 ​
     //用于确认选择的输入。该方法从键盘读取'Y'或'N’,并将其作为方法的返回值。
     public static char readConfirmSelection(){
         char c;
         for(;;){
             String str = readKeyBoard(1,false).toUpperCase();
             c = str.charAt(0);
             if(c == 'Y' || c== 'N'){
                 break;
             } else {
                 System.out.println("选择错误,请重新输入:");
             }
         }
         return c;
     }
 ​
     private static String readKeyBoard(int limit , boolean blankReturn){
         String line = "";
         while(scanner.hasNextLine()){
             line = scanner.nextLine();
             if(line.length() == 0){
                 if(blankReturn) return line;
                 else continue;
             }
         }
         return null;
     }
 }

Customer类:

 
package yangtingp2bean;
 //Customer为实体对象,用来封装客户信息
 public class Customer {
     private String name;
     private char gender;
     private int age;
     private String phone;
     private String email;
 ​
     public Customer(){
 ​
     }
     public Customer(String name,char gender,int age,String phone,String email){
         this.name = name;
         this.gender = gender;
         this.age = age;
         this.phone = phone;
         this.email = email;
     }
 ​
     public String getName(){
         return name;
     }
     public void setName(String name){
         this.name = name;
     }
 ​
     public char getGender(){
         return gender;
     }
     public void setGender(char gender){
         this.gender = gender;
     }
 ​
     public int getAge(){
         return age;
     }
     public void setAge(int age){
         this.age = age;
     }
 ​
     public String getPhone(){
         return phone;
     }
     public void setPhone(){
         this.phone = phone;
     }
 ​
     public String getEmail(){
         return email;
     }
     public void setEmail(String email){
         this.email = email;
     }
 }

CustomerList类:

 
package yangtingp2service;
 ​
 import yangtingp2bean.Customer;
 ​
 /*CustomerList为Customer对象的管理模块,
   内部用数组管理一组Customer对象,并提供
   相应的添加、修改、删除和遍历方法,供CustomerView调用
 */
 public class CustomerList {
     private Customer[] customers;
     private int total = 0;
 ​
     public CustomerList(){
 ​
     }
     //初始化customers数组的构造器
     public CustomerList(int totalCustomer){
         customers = new Customer[totalCustomer];
     }
 ​
     //添加用户
     public boolean addCustomer(Customer customer){
         if(total >= customers.length){
             return false;
         }
         customers[total++] = customer;
         return true;
     }
 ​
     //修改指定索引位置的客户信息
     public boolean replaceCustomer(int index,Customer cust){
         if(index < 0 || index >= total){
             return false;
         }
         customers[index] = cust;
         return true;
     }
 ​
     //删除用户
     public boolean deleteCustomer(int index){
         if(index < 0 && index >= total){
             return false;
         }
         for(int i = index ; i< total - 1 ; i++){
             customers[i] = customers[i+1];
         }
 ​
         //最后有数据的元素需要置空
 //        customers[total - 1] = null;
 //        total --;
         customers[--total] = null;
         return true;
     }
 ​
     //获取所有用户
     public Customer[] getAllCustomers(){
 //        return customers;
         Customer[] custs = new Customer[total];
         for(int i = 0;i < total ; i++){
             custs[i] = customers[i];
         }
         return custs;
     }
 ​
     //获取指定索引的用户
     public Customer getCustomer(int index){
         if(index < 0 || index >= total){
             return null;
         }
         return customers[index];
     }
 ​
     //获取总用户数
     public int getTotal(){
         return total;
     }
 ​
 }

思:

今天课比较少,学了大概八小时,感觉也是比较满意吧。而且今天心情也比较好,一身清朗的感觉。希望明天继续加油吧。

最近在看柴静的《看见》,挺棒的一本书。曾经以为这个世界是美好的,每个人也都是善良的。觉得所谓的“恶”,只不过是极少数的存在。而当我看了这本书之后也是挺震撼的。发现这个世界或许并没有我想象中的那么美好。也从作者的笔触间,窥见世间不那么美好的一角。而这份不美好,或许也在提醒着我们更该珍惜眼前的美好......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值