zoukankan      html  css  js  c++  java
  • 201871010133赵永军《面向对象程序设计(java)》第七周学习总结

    201871010133-赵永军《面向对象程序设计(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: “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(p2);//分别尝试显示Parent类的p1、p2、p3、p4值
    26         System.out.println(p3);
    27         System.out.println(p4);
    28         System.out.println("我是Son用public修饰符修饰的方法");
    29     }
    30     private void sMethod2() {
    31         System.out.println("我是Son用private修饰符修饰的方法");
    32     }
    33     protected void sMethod3() {
    34         System.out.println("我是Son用protected修饰符修饰的方法");
    35     }
    36     void sMethod4() {
    37         System.out.println("我是Son无修饰符修饰的方法");
    38     }    
    39 }
    40 public class Demo {
    41     public static void main(String[] args) {
    42         Parent parent=new Parent();
    43         Son son=new Son();
    44         parent.pMethod2(); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法
    45         parent.pMethod3();
    46         parent.pMethod4();
    47         son.pMethod2();
    48         son.pMethod3();
    49         son.pMethod4();
    50         son.sMethod1();
    51         son.sMethod3();
    52         son.sMethod4();
    53     }
    54 }

    实验运行结果如图所示:

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

    测试程序1:

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

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

    测试程序2:

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

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

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

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

    测试程序3:

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

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

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

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

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

        }  

    }

     

    测试程序1

    实验代码:

    EqualsTest类:

     1 public class EqualsTest
     2 {
     3    public static void main(String[] args)
     4    {
     5       var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
     6       var alice2 = alice1;
     7       var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
     8       var bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
     9 
    10       System.out.println("alice1 == alice2: " + (alice1 == alice2));
    11 
    12       System.out.println("alice1 == alice3: " + (alice1 == alice3));
    13 
    14       System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
    15 
    16       System.out.println("alice1.equals(bob): " + alice1.equals(bob));
    17 
    18       System.out.println("bob.toString(): " + bob);
    19 
    20       var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    21       var boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
    22       boss.setBonus(5000);
    23       System.out.println("boss.toString(): " + boss);
    24       System.out.println("carl.equals(boss): " + carl.equals(boss));
    25       System.out.println("alice1.hashCode(): " + alice1.hashCode());
    26       System.out.println("alice3.hashCode(): " + alice3.hashCode());
    27       System.out.println("bob.hashCode(): " + bob.hashCode());
    28       System.out.println("carl.hashCode(): " + carl.hashCode());
    29    }
    30 }

    Employee类:

     1 package zyj;
     2 
     3 import java.time.*;
     4 import java.util.Objects;
     5 
     6 public class Employee
     7 {
     8    private String name;
     9    private double salary;
    10    private LocalDate hireDay;
    11 
    12    public Employee(String name, double salary, int year, int month, int day)
    13    {
    14       this.name = name;
    15       this.salary = salary;
    16       hireDay = LocalDate.of(year, month, day);
    17    }
    18 
    19    public String getName()
    20    {
    21       return name;
    22    }
    23 
    24    public double getSalary()
    25    {
    26       return salary;
    27    }
    28 
    29    public LocalDate getHireDay()
    30    {
    31       return hireDay;
    32    }
    33 
    34    public void raiseSalary(double byPercent)
    35    {
    36       double raise = salary * byPercent / 100;
    37       salary += raise;
    38    }
    39 
    40    public boolean equals(Object otherObject)
    41    {
    42       // a quick test to see if the objects are identical
    43       if (this == otherObject) return true;
    44 
    45       // must return false if the explicit parameter is null
    46       if (otherObject == null) return false;
    47 
    48       // if the classes don't match, they can't be equal
    49       if (getClass() != otherObject.getClass()) return false;
    50 
    51       // now we know otherObject is a non-null Employee
    52       var other = (Employee) otherObject;
    53     return false;
    54    }
    55 
    56    public int hashCode()
    57    {
    58       return Objects.hash(name, salary, hireDay); 
    59    }
    60 
    61    public String toString()
    62    {
    63       return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" 
    64          + hireDay + "]";
    65    }
    66 }

    Manage类:

     1 package zyj;
     2 
     3 public class Manager extends Employee
     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       bonus = 0;
    11    }
    12 
    13    public double getSalary()
    14    {
    15       double baseSalary = super.getSalary();
    16       return baseSalary + bonus;
    17    }
    18 
    19    public void setBonus(double bonus)
    20    {
    21       this.bonus = bonus;
    22    }
    23 
    24 }

    程序运行结果如图所示:

    1)Object类是所有Java类的祖先。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法在不明确给出超类的情况下,java会自动把object作为要定义类的超类。

    2)可以使用类型为object的变量指向任意类型的对象。

    3)object类有一个默认构造方法public object(),在构造子类时,都会先调用这个默认构造方法。

    测试程序2:

    实验代码:

    ArrayListTest类:

     1 import java.util.*;
     2 
     3 /**
     4  * This program demonstrates the ArrayList class.
     5  * @version 1.11 2012-01-26
     6  * @author Cay Horstmann
     7  */
     8 public class ArrayListTest//主类
     9 {
    10    public static void main(String[] args)
    11    {
    12       //用三个Employee类填充staff数组列表
    13       ArrayList<Employee> staff = new ArrayList<>();//动态数组,可以灵活设置数组的大小
    14 
    15       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
    16       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
    17       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
    18 
    19       //将每个人的薪水提高5%
    20       for (Employee e : staff)
    21          e.raiseSalary(5);
    22 
    23       //打印出所有Employee类的信息
    24       for (Employee e : staff)
    25          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
    26                + e.getHireDay());
    27    }
    28 }

    Employee类:

     1 import java.time.*;
     2 
     3 public class Employee
     4 {
     5    private String name;
     6    private double salary;
     7    private LocalDate hireDay;
     8    //创建三个私有属性
     9    public Employee(String name, double salary, int year, int month, int day)//构造器
    10    {
    11       this.name = name;
    12       this.salary = salary;
    13       hireDay = LocalDate.of(year, month, day);
    14    }
    15 
    16    public String getName()
    17    {
    18       return name;
    19    }
    20 
    21    public double getSalary()
    22    {
    23       return salary;
    24    }
    25 
    26    public LocalDate getHireDay()
    27    {
    28       return hireDay;
    29    }
    30 //访问器
    31    public void raiseSalary(double byPercent)
    32    {
    33       double raise = salary * byPercent / 100;
    34       salary += raise;
    35    }//定义两个局部变量
    36 }

    程序运行结果如图所示:

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

    测试程序3:

    实验代码:

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

    程序运行结果如下:

    创建枚举类型要使用 Enum 关键字,隐含了所创建的类型都是 java.lang.Enum 类的子类。

    枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰。

    枚举类的使用private final 修饰的属性应该在构造器中为其赋值。
    若枚举类显式的定义了带参数的构造器,则在列出枚举值时也必须对应的传入参数。
    测试程序4:
    实验代码
     1 public class TestVarArgus {  
     2     public static void dealArray(int... intArray){  
     3         for (int i : intArray)  
     4             System.out.print(i +" ");  
     5           
     6         System.out.println();  
     7     }        
     8     public static void main(String args[]){  
     9         dealArray();  
    10         dealArray(1);  
    11         dealArray(1, 2, 3);  
    12     }  
    13 }

    程序运行结果如下:

    输出字符串

    实验代码:

     1 public class TestVarArgus { 
     2     public static void dealArray(String... StringArray){     //定义为字符串,在下面就只能输入字符串,不能输入其他的,例如整型等等
     3         for (String string : StringArray) 
     4             System.out.print(string +" "); 
     5            
     6         System.out.println(); 
     7     }       
     8     public static void main(String args[]){ 
     9         dealArray("计算机与工程学院"); 
    10         dealArray("计算机科学与技术一班");
    11         dealArray("赵永军");
    12     } 
    13 }

    实验测试程序如下:

    实验: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()

    实验代码:

     1 public class Demo {
     2     public static void main(String[] args) {
     3         Son son = new Son();
     4         son.method();
     5     }
     6 }
     7 class Parent {
     8     Parent() {
     9         System.out.println("Parent's Constructor without parameter");
    10     }
    11     Parent(boolean b) {
    12         System.out.println("Parent's Constructor with a boolean parameter");
    13     }
    14     public void method() {
    15         System.out.println("Parent's method()");
    16     }
    17 }
    18 class Son extends Parent {
    19 //补全本类定义
    20     Son(){
    21         super(false);
    22         System.out.println("Son's Constructor without parameter");
    23     }
    24     public void method() {
    25         System.out.println("Son's method()");
    26         super.method();
    27     }
    28 }

    程序运行结果如下:

    二、实验总结

    在前几周的学习基础上,这周又学习了第五章,相比之前,加深了对继承类、抽象类以及多态的学习。但对于后面学习的枚举类等新知识,在学习理论知识时,觉得已经基本掌握了。但在实验过程中发现自己还是不会运用。在做测试题的时候,很多基础知识都不会,还是自己平时没有多看书,对基础知识掌握不到位。对于编程题,一方面是pta平台上只要出一点格式错误程序就会显示错误,另外是自己的知识还不够,因此几乎不能完整的做出实验。在以后的学习中,我会多多pta平台上练习编程,来保证自己程序的严谨和准确性。

  • 相关阅读:
    MicroPython实例之TPYBoard v102炫彩跑马灯WS2812B
    MicroPython实例之TPYBoardv102自动浇花系统
    Micropython实例之DIY超声波避障小车
    MicroPython支持图形化编辑了:Python Editor带你轻松玩转MicroPython
    潍坊首个小学“教育创客空间”落户呼家庄小学 萝卜(创客)教育走进小学课堂
    Micropython TPYBoard I2C的用法
    JDK5.0新特性-反射
    JDK5.0新特性-枚举
    JDK5.0新特性-泛型
    JDK5.0新特性-静态导入
  • 原文地址:https://www.cnblogs.com/zhaoyongjun0911/p/11669089.html
Copyright © 2011-2022 走看看