• 201871010117石欣钰《面向对象程序设计(java)》第七周学习总结


    项目

    内容

    这个作业属于哪个课程

     https://www.cnblogs.com/nwnu-daizh/

    这个作业的要求在哪里

       https://www.cnblogs.com/nwnu-daizh/p/11435127.html

    作业学习目标

    1. 掌握四种访问权限修饰符的使用特点;
    2. 掌握Object类的用途及常用API;
    3. 掌握ArrayList类的定义方法及用途;
    4. 掌握枚举类定义方法及用途;
    5. 结合本章实验内容,理解继承与多态性两个面向对象程序设计特征,并体会其优点。

    实验内容和步骤

    实验1:在“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法。(20分)

    代码如下:

    package project;
    
    class Parent {
        private String p1 = "这是Parent的私有属性";
        public String p2 = "这是Parent的公有属性";
        protected String p3 = "这是Parent受保护的属性";
        String p4 = "这是Parent的默认属性";
        private void pMethod1() {
            System.out.println("我是Parent用private修饰符修饰的方法");
        }
        public void pMethod2() {
            System.out.println("我是Parent用public修饰符修饰的方法");
        }
        protected void pMethod3() {
            System.out.println("我是Parent用protected修饰符修饰的方法");
        }
        void pMethod4() {
            System.out.println("我是Parent无修饰符修饰的方法");
        }
    }
    class Son extends Parent{
        private String s1 = "这是Son的私有属性";
        public String s2 = "这是Son的公有属性";
        protected String s3 = "这是Son受保护的属性";
        String s4 = "这是Son的默认属性";
        public void sMethod1() {
            System.out.println(p2);//分别尝试显示Parent类的p1、p2、p3、p4值
            System.out.println("我是Son用public修饰符修饰的方法");
        }
        private void sMethod2() {
            System.out.println("我是Son用private修饰符修饰的方法");
        }
        protected void sMethod() {
            System.out.println("我是Son用protected修饰符修饰的方法");
        }
        void sMethod4() {
            System.out.println("我是Son无修饰符修饰的方法");
        }    
    }
    public class Demo {
        public static void main(String[] args) {
            Parent parent=new Parent();
            Son son=new Son();
            System.out.println(parent.p2);    
            System.out.println(parent.p3);
            System.out.println(parent.p4);//分别尝试用parent调用Paren类的方法、用son调用Son类的方法  
       System.out.println(son.p2);
       System.out.println(son.p3);
       System.out.println(son.p4);
       System.out.println(son.s2);
       System.out.println(son.s3);
      System.out.println(son.s4);
       parent.pMethod2();
       son.pMethod2();
       son.sMethod1(); } }

    该代码是将子类父类以及主函数放在同一个包的同一个文件中,其除了parent类的private不可调用外,其他三个类都可以。子类除了不可以调用父类的私有域外,同样可以调用父类的其他域和方法


    运行结果如下:

    调用p2以及方法2得到的结果:

    调用方法3得到的结果:

    调用方法4得到的结果:

     四种权限修饰符的访问范围的情况:

    访问范围 private friendly(默认) protected public
    同一个类 可以访问 可以访问 可以访问 可以访问
    同一个包内的类 不可以访问 可以访问 可以访问 可以访问
    不同包内的类 不可以访问 不可以访问 可以访问

    可以访问

    不同包并且不是子类 不可以访问 不可以访问 不可以访问 可以访问

     protected介于public和private之间,只能被类本身的方法和子类访问,即使子类在不同的包中也可以访问。

    默认访问的这种修饰符,只能被在同一个包中的类访问和引用,而不能被其他包中的类使用,即使其他包中有该类的子类。

    实验2:导入第5章以下示例程序,测试并进行代码注释。

    测试程序1:

    a)运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);

    b) 删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。

    Employee类代码如下:

    package equals;
    
    import java.time.*;
    import java.util.Objects;
    
    public class Employee
    {
       private String name;    //实例域定义
       private double salary;
       private LocalDate hireDay;
    
       public Employee(String name, double salary, int year, int month, int day)//构造器定义
       {
          this.name = name;
          this.salary = salary;
          hireDay = LocalDate.of(year, month, day);
       }
    
       public String getName()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public LocalDate getHireDay()
       {
          return hireDay;
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    
       public boolean equals(Object otherObject)
       {
          // 快速检查对象是否相同
          //这里获得一个对象参数,第一个if语句判断两个引用是否是同一个,如果是那么这两个对象肯定相等
          if (this == otherObject) return true;
    
          // 如果显式参数为空,则必须返回false
          if (otherObject == null) return false;
    
          //  getClass()方法是得到对象的类,如果两个对象的类不一样,那么就不相等
          if (getClass() != otherObject.getClass()) return false;
    
          //现在我们知道另一个对象是非空雇员
          //在以上判断完成,再将得到的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,然后再判断对象的属性是否相同
    
          var other = (Employee) otherObject;
    
          //测试字段是否具有相同的值
          return Objects.equals(name, other.name) 
             && salary == other.salary && Objects.equals(hireDay, other.hireDay);
       }
    
       public int hashCode()
    // 哈希散列
       {
          return Objects.hash(name, salary, hireDay); 
       }
    // toString()方法,可自动生成
       public String toString()
       
       {
          return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" 
             + hireDay + "]";
       }
    }


    EqualsTest类代码如下:



    package equals;
    
    /**
     * This program demonstrates the equals method.
     * @version 1.12 2012-01-26
     * @author Cay Horstmann
     */
    public class EqualsTest
    {
       public static void main(String[] args)
       {
          var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);  //创建对象,并初始化
          var alice2 = alice1;
          var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
          var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
    
          System.out.println("alice1 == alice2: " + (alice1 == alice2));
    
          System.out.println("alice1 == alice3: " + (alice1 == alice3));
    
          System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
    
          System.out.println("alice1.equals(bob): " + alice1.equals(bob));
    
          System.out.println("bob.toString(): " + bob);
    
          var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
          var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
          boss.setBonus(5000);
          System.out.println("boss.toString(): " + boss);
          System.out.println("carl.equals(boss): " + carl.equals(boss));
          System.out.println("alice1.hashCode(): " + alice1.hashCode());
          System.out.println("alice3.hashCode(): " + alice3.hashCode());
          System.out.println("bob.hashCode(): " + bob.hashCode());
          System.out.println("carl.hashCode(): " + carl.hashCode());
       }
    }

    Manager类代码如下:

    package equals;
    
    public class Manager extends Employee
    {
       private double bonus;
    
       public Manager(String name, double salary, int year, int month, int day)
       {
          super(name, salary, year, month, day);
          bonus = 0;
       }
    
       public double getSalary()
       {
          double baseSalary = super.getSalary();
          return baseSalary + bonus;
       }
    
       public void setBonus(double bonus)
       {
          this.bonus = bonus;
       }
    
       public boolean equals(Object otherObject)
       {
          if (!super.equals(otherObject)) return false;
          var other = (Manager) otherObject;
          //检查这个和其他属于同一个类
          return bonus == other.bonus;
       }
    
       public int hashCode()
       {
          return java.util.Objects.hash(super.hashCode(), bonus);
       }
    
       public String toString()
       {
          return super.toString() + "[bonus=" + bonus + "]";
       }
    }

    其运行结果如下:

    1、equals方法:

      Object类中的equals方法用于检测某个对象是否同另一个对象相等。它在Object类中的实现是判断两个对象是否相等的引用。如果两个对象具有相同的引用,它们一定是相等的;

      如果需要检测两个对象状态的相等性,就需要在新类的定义中需要覆盖equals方法;

      定义子类的equals方法时,可调用超累的equals方法;

        super. equals (otherObjecct)

    2、hashCode方法:

      Object类中的哈hashCode方法导出某个对象的散列码。散列码时任意整数,表示dui过的存储地址;

      两个相等对象的散列码相等。

    3、toString方法:

      Object类中的toString方法返回一个代表该类对象域值的字符串;

      定义子类的toString方法时,可先调用超类的toString方法;

      super.toString()

      toString方法是非常重要的调试工具。标准类库中,多数类定义了toString方法,以便用户获得对象状态的必要信息。




    测试程序2:

    a) elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;

    b) 掌握ArrayList类的定义及用法;

    c) 在程序中相关代码处添加新知识的注释;

    e)设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法。

    程序代码如下:

    package arrayList;

     
    import java.util.*;
     
    /**
     * This program demonstrates the ArrayList class.
     * @version 1.11 2012-01-26
     * @author Cay Horstmann
     */
    public class ArrayListTest
    {
       public static void main(String[] args)
       {
          // fill the staff array list with three Employee objects
          ArrayList<Employee> staff = new ArrayList<Employee>();   //用三个Employee对象填充数组
     
          staff.add(new Employee("Carl Cracker"7500019871215));
          staff.add(new Employee("Harry Hacker"500001989101));
          staff.add(new Employee("Tony Tester"400001990315));
     
          // raise everyone's salary by 5%
          for (Employee e : staff)    //把每个人的薪资提高%5
             e.raiseSalary(5);
     
          // print out information about all Employee objects
          for (Employee e : staff)   //输出所有雇员对象的信息
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                + e.getHireDay());    //利用getName(),getSalary() 和getHireDay()方法输出所有雇员对象的信息
       }
    }
     
    Employee类代码如下:
     
    package arrayList;
    
    import java.time.*;
    
    public class Employee
    {
       private String name;
       private double salary;
       private LocalDate hireDay;
    
       public Employee(String name, double salary, int year, int month, int day)
       {
          this.name = name;
          this.salary = salary;
          hireDay = LocalDate.of(year, month, day);
       }
    
       public String getName()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public LocalDate getHireDay()
       {
          return hireDay;
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    }

    运行结果如下:

     

      ArrayList 即动态数组,它可以动态的增加和减少元素,灵活设置数组的大小。
    ArrayList提供了三个构造器: 
    public ArrayList(); 默认的构造器
    public ArrayList(ICollection); 
    public ArrayList(int); 用指定的大小来初始化内部的数组

    测试程序3:

    a) 编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;

    b)掌握枚举类的定义及用法;

    c)在程序中相关代码处添加新知识的注释;

    d)删除程序中Size枚举类,背录删除代码,在代码录入中掌握枚举类的定义要求。

    程序代码如下:

    import java.util.*;
      
      /**
       * This program demonstrates enumerated types.
       * @version 1.0 2004-05-24
       * @author Cay Horstmann
       */
      public class EnumTest//主类
      {  
        public static void main(String[] args)
        {  
           Scanner in = new Scanner(System.in);
           System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
           String input = in.next().toUpperCase();//字符串转换为大写
           Size size = Enum.valueOf(Size.class, input);
           System.out.println("size=" + size);
           System.out.println("abbreviation=" + size.getAbbreviation());
           if (size == Size.EXTRA_LARGE)
              System.out.println("Good job--you paid attention to the _.");      
        }
     }
     
     enum Size//枚举类型(都是enum的子类)
     {
        SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");//传入参数
     
       private Size(String abbreviation) { this.abbreviation = abbreviation; }
        public String getAbbreviation() { return abbreviation; }
     
        private String abbreviation;
     }


    测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法
     public class TestVarArgus {  
          public static void dealArray(int... intArray){  
              for (int i : intArray)  
                  System.out.print(i +" ");  
                
              System.out.println();  
         }        
         public static void main(String args[]){  
              dealArray();  
             dealArray(1);  
             dealArray(1, 2, 3);  
         }  
     }
    程序运行结果如下:

    实验:3:编程练习:参照输出样例补全程序,使程序输出结果与输出样例一致。

    public class Demo {

    public static void main(String[] args) {

    Son son = new Son();

    son.method();

    }

    }

    class Parent {

    Parent() {

    System.out.println("Parent's Constructor without parameter");

    }

    Parent(boolean b) {

    System.out.println("Parent's Constructor with a boolean parameter");

    }

    public void method() {

    System.out.println("Parent's method()");

    }

    }

    class Son extends Parent {

    //补全本类定义

    }

    要求输出样式如下:

    Parent's Constructor with a boolean parameter
    Son's Constructor without parameter
    Son's method()
    Parent's method()

    实验代码:

    public class Demo {
          public static void main(String[] args) {
              Son son = new Son();
              son.method();
          }
     }
      class Parent {
          Parent() {
              System.out.println("Parent's Constructor without parameter");
         }
         Parent(boolean b) {
             System.out.println("Parent's Constructor with a boolean parameter");
         }
         public void method() {
            System.out.println("Parent's method()");
         }
     }
     class Son extends Parent {
     //补全本类定义
         Son(){
             super(false);
             System.out.println("Son's Constructor without parameter");
         }
         public void method() {
             System.out.println("Son's method()");
             super.method();
         }
     }

    程序运行结果如下:



    二、实验总结

    通过本周的实验,我掌握理解了成员访问权限的四个修饰符,Object类和ArrayList类的常用方法,API以及枚举使用方法。在写程序的过程中,我发现了我的不足,编程能力还远远不行。在继承学习中,仍有一些父类和子类的关系没搞懂;继承程序构造技术还不太熟练,需要继续学习巩固。

     
  • 相关阅读:
    异常:Batch update returned unexpected row count from update [0]; actual row count: 0;
    python UI/API 环境配置
    selenium xpath定位
    python webdriver 的安装与下载
    python面试题
    fiddler工具使用及手机抓包
    jmeter 常用函数
    Jmeter常用插件——Stepping Thread Group ,JMETER以及关于数据库性能分析
    Jmeter Monitor监控
    python 常用快捷键
  • 原文地址:https://www.cnblogs.com/sxy19991214/p/11674249.html
Copyright © 2020-2023  润新知