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

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

    项目 内容
    这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/
    这个作业要求在哪里 https://www.cnblogs.com/lily-2018/p/11441372.html
    作业学习目标

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

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

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

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

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

    第一部分:总结理论知识

    1、(1)访问限制修饰符有:private、protected和public

    (2)使用关键字private修饰的成员变量或者方法称为私有变量和私有方法,该成员变量和方法只能在同一个类中被访问,不能够使用类名来操作这个私有变量和私有方法

    (3)使用关键字protected修饰的成员变量或者方法称为受保护的成员变量和受保护的方法,在与该类同包的其他类中可以访问该成员变量与方法,可以通过类名来操作这个受保护的变量和受保护的方法,而且子类可以继承父类的受保护的成员变量与受保护的方法,子类可以访问在与子类同一个包中的从父类或者祖先中的继承过来的受保护的成员变量与受保护的方法
    (4)使用关键字public修饰的成员变量或者方法称为共有变量和共有方法

    (5)不能够使用private和protected来修饰一个类

    2、Object:所有类的超类

    (1)Object类是Java中所有类的祖先——每一个类都由它扩 展而来。在不给出超类的情况下,Java会自动把Object 作为要定义类的超类。

    (2)equals方法、hashCode方法、toString方法

    3、a.ArrayList定义

    b.添加新元素

    c.统计个数

    d.调整大小

    e.访问

    f.增加与删除

    4、枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。

    (2)声明枚举类

    (3)为枚举类增加构造函数

    (4)Enum类的API

    5、继承设计的技巧

    ① 将公共操作和域放在超类。 ② 不要使用受保护的域。 ③ 使用继承实现“is-a”关系。 ④ 除非所有继承的方法都有意义,否则就不要 使用继承。 ⑤ 在覆盖方法时,不要改变预期的行为。 ⑥ 使用多态,而非类型信息。 ⑦ 不要过多地使用反射。

    第二部分:实验部分

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

    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(...);//分别尝试显示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调用Paren类的方法、用son调用Son类的方法	
    	}
    

     Parent的代码如下:

    package com.nwnu.demo1;
    public 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无修饰符修饰的方法");
        }
    }
    

      Son的代码如下:

    package com.nwnu.demo2;
    import com.nwnu.demo1.Parent;
    public 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无修饰符修饰的方法");
        }  
    }

    运行结果如下:

     

     

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

    可以访问

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

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

    测试程序1:

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

    代码如下:

    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 (this == otherObject) return true;
          // 如果显式参数为空,则必须返回false
          if (otherObject == null) return false;
          // 如果类不匹配,它们就不能相等
          if (getClass() != otherObject.getClass()) return false;
          // 现在我们知道otherObject是一个非空雇员
          Employee other = (Employee) otherObject;
          // 测试字段是否具有相同d的值
          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
                + "]";
       }
    }
    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());
       }
    }
    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;
          Manager 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 + "]";
       }
    }
    

    运行结果如下:

    2) 删除程序中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;
       }
      
    @Override
    public int hashCode() {   //重写hashCode方法,使相等的两个对象获取的HashCode也相等
        // TODO Auto-generated method stub
        return Objects.hash(name, salary, hireDay);
    }
      
    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if (this == obj) return true;     
        if (getClass() !=obj.getClass()) return false;   
        Employee other = (Employee) obj;
        //测试字段是否具有相同的值
        return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
     }
      
      
    @Override
    public String toString() {  //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)
        // TODO Auto-generated method stub
        return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
    }
      
    }
    

     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;
       }
     
    @Override
    public String toString() {
        return super.toString()+"[bonus=" + bonus + "]";
    }
     
    @Override
    public int hashCode() {
        return java.util.Objects.hash(super.hashCode(),bonus);
    }
     
    @Override
    public boolean equals(Object obj) {
        if (!super.equals(obj))
            return false;
        Manager other = (Manager) obj;
            return bonus == other.bonus;
    }
     
        
    }

    测试程序2:

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

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

    在程序中相关代码处添加新知识的注释;设计适当的代码,测试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));
    
          // 把每个人的薪水提高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());
       }
    }
    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));
    
          // raise everyone's salary by 5%
          for (Employee e : staff)
             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());
       }
    }
    

    运行结果如下:

     设置代码如下:

    package arrayList;
      
    import java.util.*;
      
    /**
     * This program demonstrates the ArrayList class.
     * @version 1.11 2012-01-26
     * @author Cay Horstmann
     */
    public class f
    {
       private static final Employee element = null;
       private static final int index = 0;
      
       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", 75000, 1987, 12, 15));
          staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
          staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
          ArrayList<Employee> list = new ArrayList<Employee>();
            
        //size()的用法
          int size=staff.size();
          System.out.println("arrayList中的元素个数是:"+size);
          for(int i=0;i<staff.size();i++)
          {
              //get()的用法
              Employee e=staff.get(i);
              System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                        + e.getHireDay());
          }
          //set()的用法
          staff.set(0, new Employee("llx", 20000, 1999, 11, 06));
          Employee e=staff.get(0);
          System.out.println("修改后的数据为:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                    + e.getHireDay());
            
          //remove()的用法
          staff.remove(2);
          System.out.println("将第一个数据删除后:");
          int size1=staff.size();
          System.out.println("arrayList中的元素个数是:"+size1);
          for(int i=0;i<staff.size();i++)
          {
              Employee p=staff.get(i);
              System.out.println("name=" + p.getName() + ",salary=" + p.getSalary() + ",hireDay="
                        + p.getHireDay());
          }
           
          // raise everyone's salary by 5%
          for (Employee e1 : staff)    //把每个人的薪资提高%5
             e1.raiseSalary(5);
      
          // print out information about all Employee objects
          for (Employee e1 : staff)   //输出所有雇员对象的信息
             System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay="
                + e1.getHireDay());    //利用getName(),getSalary() 和getHireDay()方法输出所有雇员对象的信息
       }
    }
    

      运行结果如下:

    测试程序3:

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

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

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

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

    代码如下:

    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)
       {  
          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
    {
       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()
    

    补充代码如下:

    public class h {
        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();
        }
    }
    

      运行结果如下:

     

    实验总结:

     

         在本周的学习过程中,实验课上听过了学长的讲解后对于四个修饰符有了更加透彻的理解,最初使用的时候只是大概将其敲上去,当时并不理解各个修饰符在功能上的不同点。1.仅对本类可见-private2.对所有类可见-public3.对本包和所有子类可见-protected4.对本包可见-默认,不需要修饰符。在学长的指导之下进一步理解4个成员访问权限修饰符的用途;在老师的讲解以及实验测试下掌握了Object类的常用API用法、ArrayList类用法与常用API、枚举类使用方法;大概理解了继承与多态性两个面向对象程序设计特征,掌握了Java语言中基于类、继承技术构造程序的语法知识。实验方面是在上次实验的基础上进一步了解它的内容和深意,通过注释能更一步了解代码含义,在编程方面感觉还要更加努力,才能进步。

     

  • 相关阅读:
    git 多个commit合并一个
    前端本地proxy跨域代理配置
    .net core指定环境类型
    使用流程引擎整体解决方案
    通用流程相关方法说明及调用事例
    流程引擎及流程设计器的嵌入方式
    可视化流程设计——流程设计器演示(基于Silverlight)
    通用流程设计
    动态网站后台解决方案
    timestamp时间格式
  • 原文地址:https://www.cnblogs.com/chanyeol1127/p/11669178.html
Copyright © 2011-2022 走看看