zoukankan      html  css  js  c++  java
  • 201871010114李岩松《面向对象程序设计(java)》第七周学习总结

    项目

    内容

    这个作业属于哪个课程

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

    这个作业的要求在哪里

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

    作业学习目标

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

    实验六 Object类、ArrayList类与枚举类

    实验时间 2019-10-11

    1、实验目的与要求

    (1) 掌握四种访问权限修饰符的使用特点;

    (2) 掌握Object类的用途及常用API;

    (3) 掌握ArrayList类的定义方法及用法;

    (4)掌握枚举类定义方法及用途;

    (5)结合本章实验内容,理解继承与多态性两个面向对象程序设计特征,并体会其优点。

    2、实验内容和步骤

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

    package hello;
    
    
    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);
            System.out.println(p3);
            System.out.println(p4);
            //分别尝试显示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 multiplication {
        public static void main(String[] args) {
            Parent parent=new Parent();
            Son son=new Son();
            //System.out.println();
            parent.pMethod2();//用parent调用parent类的方法
            parent.pMethod3();//
            parent.pMethod4();//
            son.sMethod1();//用son调用son类的方法
            son.sMethod4();
            son.sMethod();
            son.pMethod2();//用son类调用parent类的方法
            son.pMethod4();
            son.pMethod3();
        }
    }

    输出结果:

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

    测试程序1:

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

    l  删除程序中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 Employee 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;
    
    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
          Employee 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
                + "]";
       }
    }

    运行结果

     总结:Object是所有类的父类,它有很多类对象会用到的方法,例如比较常用的toString 、equals,当你新建xx类时,你可以重写Object已经定义的方法,也可以直接调用Object中的方法,如果你写一个封装的方法,不确定传进来的是什么类型的值,就可以使用Object作为一个笼统类。

    测试程序2:

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

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

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

    l  设计适当的代码,测试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<>();
    
          staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
          staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
          staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
    
          // 把每个员工的薪资提高5%
          for (Employee e : staff)
             e.raiseSalary(5);
    
          //打印所有雇员对象的信息
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                   + e.getHireDay());
       }
    }

    运行结果:

    设计适当的代码,测试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)
       {
          // 用三个Employee对象填充staff数组列表
          var staff = new ArrayList<Employee>();
    
          staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
          staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
          staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
          
          System.out.println(staff.get(1));
          staff.set(1, new Employee("liyansong", 20000, 2018, 10, 10));
          System.out.println(staff.size());
          staff.remove(2);
    
          // 把每个人的薪水提高5%
          for (Employee e : staff)
             e.raiseSalary(5);
    
          // 打印所有Employee对象的信息
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 
                + e.getHireDay());
       }
    }

    测试结果:

     

     

     总结:ArrayList就是传说中的动态数组,动态的增加和减少元素 ,ArrayList内部封装了一个Object类型的数组,从一般的意义来说,它和数组没有本质的差别,

    并且可以灵活的设置数组的大小。

    测试程序3

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

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

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

     

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

    5-12

    package enums;
    
    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)
       {  
          var 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
    {
       SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
    
       private Size(String abbreviation) { this.abbreviation = abbreviation; }
       public String getAbbreviation() { return abbreviation; }
    
       private String abbreviation;
    }


    运行结果:

     

     总结:枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁,安全性以及便捷性。创建枚举类型要使用enum关键字,隐含了所创建的类型都是java.lang.Enum类的子类(java.lang.Enum是一个抽象类)。

    测试程序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()

    补全代码:

    package enums;
    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(true);
        }
        public void method() {
            System.out.println("Son's Constructor without parameter");  
            System.out.println("Son's method()");
            super.method();
    //补全本类定义
    }
    }

    运行结果:

    第三部分:总结

     

    通过这周的实验,使我更进一步的理解了继承的相关知识,尤其是对四种权限修饰符的理解,还有Object类的常用API用法;ArrayList类用法与常用API,枚举类使用方法;在本章知识中,继承与多态性两个面向对象程序设计特征,利用已掌握Java语言程序设计知识,通过一次一次的练习,我觉得阅读已有的代码,理解其中的方法,并将其转化为自己的知识,是一种很重要的能力,编程没有捷径,只有不断地训练。

     

     

  • 相关阅读:
    迅为-IMX6Q开发板Android应用测试-AndroidStudio-录音机测试
    迅为3399开发板Linux固件编译
    迅为-i.MX6ULL开发板-Busybox移植DHCP(三)
    迅为-i.MX6ULL开发板-Busybox移植DHCP(二)
    itop4412开发板Qt串口编程-实现串口功能
    迅为iMX6D/Q/PLUS设备树 Android7.1.2 系统编译
    迅为3399开发板使用ADB命令传递文件到android设备
    Jlink如何校验Hex
    编码器芯片MLX90363的使用
    编程小技巧
  • 原文地址:https://www.cnblogs.com/liyansong0198/p/11669189.html
Copyright © 2011-2022 走看看