zoukankan      html  css  js  c++  java
  • 201871010101陈来弟《面向对象程序设计(java)》第七周学习总结 201871010101

                                                   201871010101-陈来弟《面向对象程序设计(java)》第七周学习总结

    项目

    内容

    《面向对象程序设计(java)》

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

    这个作业的要求在哪里

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

    作业学习目标

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

     

    实验内容和步骤

    实验1:(20分)

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

    程序代码如下:

     1 class Parent {
     2     private String p1 = "这是Parent的私有属性";
     3     public String p2 = "这是Parent的公有属性";
     4     protected String p3 = "这是Parent受保护的属性";
     5     String p4 = "这是Parent的默认属性";
     6     private void pMethod1() {
     7         System.out.println("我是Parent用private修饰符修饰的方法");
     8     }
     9     public void pMethod2() {
    10         System.out.println("我是Parent用public修饰符修饰的方法");
    11     }
    12     protected void pMethod3() {
    13         System.out.println("我是Parent用protected修饰符修饰的方法");
    14     }
    15     void pMethod4() {
    16         System.out.println("我是Parent无修饰符修饰的方法");
    17     }
    18 }
    19 class Son extends Parent{
    20     private String s1 = "这是Son的私有属性";
    21     public String s2 = "这是Son的公有属性";
    22     protected String s3 = "这是Son受保护的属性";
    23     String s4 = "这是Son的默认属性";
    24     public void sMethod1() {
    25         System.out.println("...");//分别尝试显示Parent类的p1、p2、p3、p4值
    26         System.out.println("我是Son用public修饰符修饰的方法");
    27     }
    28     private void sMethod2() {
    29         System.out.println("我是Son用private修饰符修饰的方法");
    30     }
    31     protected void sMethod() {
    32         System.out.println("我是Son用protected修饰符修饰的方法");
    33     }
    34     void sMethod4() {
    35         System.out.println("我是Son无修饰符修饰的方法");
    36     }    
    37 }
    38 public class Demo {
    39     public static void main(String[] args) {
    40         Parent parent = new Parent();
    41         Son son = new Son();
    42         Parent   pMethod3= new Son ();
    43 
    44         ((Son) pMethod3).sMethod1();
    45 
    46         ((Son) pMethod3).pMethod3();
    47 
    48         ((Son) pMethod3).sMethod4();
    49 
    50         pMethod3.pMethod2();
    51 
    52         pMethod3.pMethod3();
    53 
    54         pMethod3.pMethod4();
    55 
    56      
    57 
    58         System.out.println(pMethod3.p2);
    59 
    60         System.out.println(pMethod3.p3);
    61 
    62         System.out.println(pMethod3.p4); 
    63     }
    64 }

    运行结果:

     

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

    测试程序1:

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

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

     

     5-8程序代码如下:

    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)
       {
           Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
           Employee alice2 = alice1;
           Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
           Employee 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);
    
          Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
          Manager 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());
       }
    }

     5-9程序代码如下:

    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)
       {
          // a quick test to see if the objects are identical
          if (this == otherObject) return true;
    
          // must return false if the explicit parameter is null
          if (otherObject == null) return false;
    
          // if the classes don't match, they can't be equal
          if (getClass() != otherObject.getClass()) return false;
    
          // now we know otherObject is a non-null Employee
          var other = (Employee) otherObject;
    
          // test whether the fields have identical values
          return Objects.equals(name, other.name) 
             && salary == other.salary && Objects.equals(hireDay, other.hireDay);
       }
    
       public int hashCode()
       {
          return Objects.hash(name, salary, hireDay); 
       }
    
       public String toString()
       {
          return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" 
             + hireDay + "]";
       }
    }


    5-10程序代码如下:

    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;
          // super.equals checked that this and other belong to the same class
          return bonus == other.bonus;
       }
    
       public int hashCode()
       {
          return java.util.Objects.hash(super.hashCode(), bonus);
       }
    
       public String toString()
       {
          return super.toString() + "[bonus=" + bonus + "]";
       }
    }

     三个程序运行结果:

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

    Manager:

     1 public class Manager extends Employee
     2 
     3 {
     4 
     5     private double bonus;
     6 
     7     public Manager(String name, double salary, int year, int month, int day) {
     8 
     9         super(name, salary, year, month, day);
    10 
    11         // TODO Auto-generated constructor stub
    12 
    13          bonus = 0;
    14 
    15     }
    16 
    17     public void setBonus(double bonus) {
    18 
    19         this.bonus = bonus;
    20 
    21     }
    22 
    23     @Override
    24 
    25     public double getSalary() {
    26 
    27         // TODO Auto-generated method stub
    28 
    29         double baseSalary= super.getSalary();
    30 
    31         return baseSalary+bonus;
    32 
    33     }
    34 
    35     @Override
    36 
    37     public boolean equals(Object otherObject) {
    38 
    39         // TODO Auto-generated method stub
    40 
    41         if(!super.equals(otherObject)) return false;
    42 
    43         Manager other=(Manager)otherObject;
    44 
    45         return bonus==other.bonus;
    46 
    47     }
    48 
    49     @Override
    50 
    51     public int hashCode() {
    52 
    53         // TODO Auto-generated method stub
    54 
    55         return super.hashCode()+17*new Double(bonus).hashCode();
    56 
    57     }
    58 
    59     @Override
    60 
    61     public String toString() {
    62 
    63         // TODO Auto-generated method stub
    64 
    65         return super.toString()+"[bonus="+bonus+"]";
    66 
    67     }
    68 
    69   
    70 
    71 }

    Employee:

    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;
    
    }
    
     
    
     
    
    @Override
    
    public boolean equals(Object otherObject) {
    
        // TODO Auto-generated method stub
    
        if(this==otherObject) return true;
    
        if(this==null) return false;
    
        if(getClass() != otherObject.getClass()) return false;
    
        Employee other=(Employee)otherObject;
    
        return Objects.equals(name,other.name)&&salary == other.salary&&Objects.equals(hireDay,other.hireDay);
    
    }
    
    @Override
    
    public int hashCode() {
    
        // TODO Auto-generated method stub
    
        return Objects.hash(name,salary,hireDay);
    
    }
    
     
    
    @Override
    
    public String toString() {
    
        // TODO Auto-generated method stub
    
        return getClass().getName()+"[name="+name+",salary="+salary+",hireday="+hireDay+"]";
    
    }
    }

    运行结果:

     Object类存储在java.lang包中,是所有java类(Object类除外)的终极父类。当然,数组也继承了Object类。然而,接口是不继承Object类的,“Object类不作为接口的父类”。java的任何类都继承了这些函数,并且可以覆盖不被final修饰的函数。例如,没有final修饰的toString()函数可以被覆盖,但是final wait()函数就不行。Java也没有强制声明“继承Object类”。如果这样的话,就不能继承除Object类之外别的类了,因为java不支持多继承。然而,即使不声明出来,也会默认继承了Object类,

    测试程序2:

     

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

     

    l 掌握ArrayList类的定义及用法;

     

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

    5-11代码:

     1 package arrayList;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates the ArrayList class.
     7  * @version 1.11 2012-01-26
     8  * @author Cay Horstmann
     9  */
    10 public class ArrayListTest
    11 {
    12    public static void main(String[] args)
    13    {
    14       // fill the staff array list with three Employee objects
    15       ArrayList<Employee> staff = new ArrayList<Employee>();
    16 
    17       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
    18       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    19       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
    20 
    21       // raise everyone's salary by 5%
    22       for (Employee e : staff)
    23          e.raiseSalary(5);
    24 
    25       // print out information about all Employee objects
    26       for (Employee e : staff)
    27          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
    28             + e.getHireDay());
    29    }
    30 }
     1 package arrayList;
     2 
     3 import java.time.*;
     4 
     5 public class Employee
     6 {
     7    private String name;
     8    private double salary;
     9    private LocalDate hireDay;
    10 
    11    public Employee(String name, double salary, int year, int month, int day)
    12    {
    13       this.name = name;
    14       this.salary = salary;
    15       hireDay = LocalDate.of(year, month, day);
    16    }
    17 
    18    public String getName()
    19    {
    20       return name;
    21    }
    22 
    23    public double getSalary()
    24    {
    25       return salary;
    26    }
    27 
    28    public LocalDate getHireDay()
    29    {
    30       return hireDay;
    31    }
    32 
    33    public void raiseSalary(double byPercent)
    34    {
    35       double raise = salary * byPercent / 100;
    36       salary += raise;
    37    }
    38 }

    运行结果:

     

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

     1 package arrayList;
     2 
     3 import java.util.*;
     4 
     5 /**
     6 
     7  * This program demonstrates the ArrayList class.
     8 
     9  * @version 1.11 2012-01-26
    10 
    11  * @author Cay Horstmann
    12 
    13  */
    14 
    15 public class ArrayListTest
    16 
    17 {
    18 
    19    public static void main(String[] args)
    20 
    21    {
    22 
    23       // fill the staff array list with three Employee objects
    24 
    25       ArrayList<Employee> staff = new ArrayList<Employee>();  //声明和构造一个保存Employee对象的数组列表
    26 
    27       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));//使用add方法将元素添加到数组列表中
    28 
    29       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    30 
    31       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
    32 
    33       staff.remove(1);  //从数组列表中删除元素
    34 
    35       int n = staff.size();
    36 
    37       System.out.println(n);
    38 
    39      System.out.println(staff.get(0)!=staff.get(1));
    40 
    41       // raise everyone's salary by 5%
    42 
    43       for (Employee e : staff)
    44 
    45          e.raiseSalary(5);
    46 
    47       // print out information about all Employee objects
    48 
    49       for (Employee e : staff)
    50 
    51          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
    52 
    53             + e.getHireDay());
    54 
    55    }
    56 
    57 }

    运行结果:

    ArrayList是集合的一种实现,实现了接口List,List接口继承了Collection接口。Collection是所有集合类的父类。ArrayList使用非常广泛,不论是数据库表查询,excel导入解析,还是网站数据爬取都需要使用到,了解ArrayList原理及使用方法显得非常重要。

     

     

    测试程序3:

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

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

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

    5-12代码:

     1 package enums;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates enumerated types.
     7  * @version 1.0 2004-05-24
     8  * @author Cay Horstmann
     9  */
    10 public class EnumTest
    11 {  
    12    public static void main(String[] args)
    13    {  
    14       Scanner in = new Scanner(System.in);//字符串转换为大写
    15       System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
    16       String input = in.next().toUpperCase();
    17       Size size = Enum.valueOf(Size.class, input);//静态values方法返回枚举的所有值的数组
    18       System.out.println("size=" + size);
    19       System.out.println("abbreviation=" + size.getAbbreviation());
    20       if (size == Size.EXTRA_LARGE)//判断语句
    21          System.out.println("Good job--you paid attention to the _.");      
    22    }
    23 }
    24 
    25 enum Size
    26 {
    27    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
    28 
    29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
    30    public String getAbbreviation() { return abbreviation; }
    31 
    32    private String abbreviation;
    33 }

    运行结果:

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

     1 package enums;
     2 
     3 import java.util.*;
     4 
     5 /**
     6  * This program demonstrates enumerated types.
     7  * @version 1.0 2004-05-24
     8  * @author Cay Horstmann
     9  */
    10 public class EnumTest
    11 {  
    12    public static void main(String[] args)
    13    {  
    14       var in = new Scanner(System.in);
    15       System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
    16       String input = in.next().toUpperCase();
    17       Size size = Enum.valueOf(Size.class, input);
    18       System.out.println("size=" + size);
    19       System.out.println("abbreviation=" + size.getAbbreviation());
    20       if (size == Size.EXTRA_LARGE)
    21          System.out.println("Good job--you paid attention to the _.");      
    22    }
    23 }
    24 
    25 enum Size
    26 {
    27    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
    28 
    29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
    30    public String getAbbreviation() { return abbreviation; }
    31 
    32    private String abbreviation;
    33 }

    运行结果:

     java.util.EnumSetjava.util.EnumMap是两个枚举集合。EnumSet保证集合中的元素不重复;EnumMap中的key是enum类型,而value则可以是任意类型。

    测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法

    代码如下:

     1 package CLD;
     2 
     3 public class TestVarArgus {  
     4 
     5     public static void dealArray(int... intArray){  
     6 
     7         for (int i : intArray)  
     8 
     9             System.out.print(i +" ");  
    10 
    11            
    12 
    13         System.out.println();  
    14 
    15     }        
    16 
    17     public static void main(String args[]){  
    18 
    19         dealArray();  
    20 
    21         dealArray(1);  
    22 
    23         dealArray(1, 2, 3);  
    24 
    25     }  
    26 
    27 }

    运行结果:

     

     

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

    代码:

     1 public class Demo {
     2 
     3     public static void main(String[] args) {
     4 
     5         Son son = new Son();
     6 
     7         son.method();
     8 
     9     }
    10 
    11 }
    12 
    13 class Parent {
    14 
    15     Parent() {
    16 
    17         System.out.println("Parent's Constructor without parameter");
    18 
    19     }
    20 
    21     Parent(boolean b) {
    22 
    23         System.out.println("Parent's Constructor with a boolean parameter");
    24 
    25     }
    26 
    27     public void method() {
    28 
    29         System.out.println("Parent's method()");
    30 
    31     }
    32 
    33 }
    34 
    35 class Son extends Parent {
    36 
    37     Son(){
    38 
    39          super(false);
    40 
    41         System.out.println("Son's Constructor without parameter");
    42 
    43         System.out.println("Son's method()");        super.method();
    44 
    45         }
    46 
    47     }

    运行结果:

     三,实验总结:

             这周的 实验大多都是重复上周的实验所以也没有大的难度,让我在前一次实验的基础上对继承有了进一步的了解,但通过测试发现了很多的问题,希望通过后期的学习可以尝试着独自解决这些问题。

     

     

     

     

  • 相关阅读:
    超有爱的并查集
    写给想当程序员的朋友
    POJ 1961 字符串 KMP (i-next[i])
    POJ 2406 KMP算法next数组理解
    POJ 2387 Bellman双重边
    POJ 1917 字符串替换
    POJ 1062 坑爹的聘礼(枚举等级差选择性找边)
    Linux下libxml2的使用
    浙大pat 1003
    判定一棵二叉树是否是二叉搜索树
  • 原文地址:https://www.cnblogs.com/wing2438/p/11673058.html
Copyright © 2011-2022 走看看