201871010121 王方 《面向对象程序设计(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提示,以验证四种权限修饰符的用法。

已知代码为

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类的方法    
    }
}

这个程序主要用来分析和帮助我们理解四个修饰符的作用与区别,最重要的是我们要理解在继承关系中,那些修饰符修饰的父类方法子类可以继承。实验输出结果如下:

 

 

特别的,我们要注意由private修饰的父类方法子类不能继承。

父类(parent)程序代码为:

public class parent {
    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 project;

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类的方法    
        System.out.println(parent.p2);
        System.out.println(son.p4);
        System.out.println(son.s3);
        parent.pMethod2();
        parent.pMethod4();
        son.pMethod3();
    }
}

 

 

 

当(parent类)在一名字命名的新建包下面时的代码为:

 

package wangfang;

public class parent {
    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无修饰符修饰的方法");
        }
    }
}

 

输出结果截图为:

 

 主要研究子类与父类见的继承关系以及特点:如下图所示

 

 

 

 此图为四个修饰符的访问特点。

 

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

测试程序1

运行教材程序5-85-95-10,结合程序运行结果理解程序(教材174-177页);

删除程序中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)
   {
      // 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 + "]";
   }
}

 

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;
      // 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父类方法后的代码为:

override后的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);
        // TODO Auto-generated constructor stub
         bonus = 0;
    }
    public void setBonus(double bonus) {
        this.bonus = bonus;
    }
    @Override
    public double getSalary() {
        // TODO Auto-generated method stub
        double baseSalary= super.getSalary();
        return baseSalary+bonus;
    }
    @Override
    public boolean equals(Object otherObject) {
        // TODO Auto-generated method stub
        if(!super.equals(otherObject)) return false;
        Manager other=(Manager)otherObject;
        return bonus==other.bonus;
    }
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode()+17*new Double(bonus).hashCode();
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString()+"[bonus="+bonus+"]";
    }

}

override后的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 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+"]";
}
  

}

程序代码输出结果如图:

 

 

 

测试程序2

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

掌握ArrayList类的定义及用法;

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

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

ArrayListTest的程序代码为:

 

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
      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));

      // 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());
   }
}

 

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类中加入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", 75000, 1987, 12, 15));//使用add方法将元素添加到数组列表中
      staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
      staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
      staff.remove(1);  //从数组列表中删除元素
      int n = staff.size();
      System.out.println(n);
     System.out.println(staff.get(0)!=staff.get(1));
      // 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());
   }
}

 

程序输出结果截图为:

 

 

 

测试程序3

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

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

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

删除程序中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)
   {  
      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;
}

 

程序输出结果截图为:

 

 

删除程序中Size枚举类,override后:

程序代码为:

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);  //静态方法valueOf
      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; }
   @Override
public String toString() {
    // TODO Auto-generated method stub
    return super.toString();
}


   public String getAbbreviation() {
    return abbreviation;
}
public void setAbbreviation(String abbreviation) {
    this.abbreviation = abbreviation;
}


private String abbreviation;
}

程序输出结果截图为:

 

 

 

 

 

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

程序代码为:

 

package test;
  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");
        System.out.println("Son's method()");
        }
    }

实验输出结果如图所示:

 

 

 

 

实验总结:

(一)在本周我们主要复习了继承的知识点,在复习过程中尤其注重学习了四个修饰符(private,toString,protected,默认)的特点。通过实验更加直观的让我们了解子类与父类在继承过程中哪些方法可继承而那些不可继承。

(二)另外,我们再次通过实验学习了Object类,ArrayList类以及枚举类的特点,另外在实验测试过程中,我发现自己动手操作过程的能力不行,以后我会尽量多实验,多动手操作。而且在实验过程中的每一小步都应该仔细,谨慎,避免小错误。

posted @ 2019-10-14 21:31  计师-王方  阅读(108)  评论(1编辑  收藏  举报