201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

时间:2022-09-16 19:35:16

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

正文开头:

项目

内容

这个作业属于哪个课程

https://home.cnblogs.com/u/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(p2);//p1无法打印,因为是私有属性
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 Demo {
public static void main(String[] args) {
Parent parent=new Parent();
Son son=new Son();
parent.pMethod2();
parent.pMethod3();
parent.pMethod4();//分别尝试用parent调用Parent类的方法
son.sMethod();//用son调用Son类的方法
son.sMethod1();
son.sMethod4();//son.sMethod2()是private,只能在son类中使用
}
}

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

注:protected和friendly都只能访问同一个包中的,但protected权限更大些,可访问不同包,但必须是子类继承父类。

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

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

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

1.程序代码

5-8EqualsTest.java

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

5-9Empolyee.java

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 若显式参数为空必须返回false
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 已知otherObject是一个非空雇员
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()//重写hashcode方法,使得相等的两个对象获取的HashCode相等
{
return Objects.hash(name, salary, hireDay);
} public String toString() //其他类型数据转为字符串型的数据
{
return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay="
+ hireDay + "]";
}
}

5-10Manager.java

package equals;

public class Manager extends Employee//子类Manager继承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 使用super.equals检查这个类和其他的是否属于同一个类
return bonus == other.bonus;
} public int hashCode()
{
return java.util.Objects.hash(super.hashCode(), bonus);
} public String toString()// toString()方法,可自动生成
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法

代码:

Employee.java

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) {  //重写equals方法
    // 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() {   //重写hashCode方法
    // TODO Auto-generated method stub
    return Objects.hash(name,salary,hireDay);
} @Override
public String toString() {  //重写toString方法
    // TODO Auto-generated method stub
    return getClass().getName()+"[name="+name+",salary="+salary+",hireday="+hireDay+"]";
}
}

Manager.java

package equals;

public class Manager extends Employee  //Manager类(子类)继承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 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+"]";
} }

EqualsTest.java

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

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

测试程序2:

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

      掌握ArrayList类的定义及用法;

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

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

5-11代码:

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用三个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)); // raise everyone's salary by 5%每个人的薪水提高5%
for (Employee e : staff)
e.raiseSalary(5); // print out information about all Employee objects
for (Employee e : staff)//for each循环,遍历数组列表
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
+ e.getHireDay());
}
}

Employee.java

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

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

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

设计适当的代码,测试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
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(2, new Employee("DXY", 1000, 1999, 7, 21));
System.out.println(staff.size());
staff.remove(0); // raise everyone's salary by 5%
for (Employee e : staff)
e.raiseSalary(5); // print out information about all Employee objects
for (Employee e : staff)//for each循环,遍历数组列表
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
+ e.getHireDay());
}
}

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

注:get()方法获得下标的储存位置

      set()方法修改ArrayList<>并添加new Employee("DXY", 1000, 1999, 7, 21)

    get()方法获得元素输出

    remove()方法删除数组元素

    size()方法得到长度。

   测试程序3:

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

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

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

      删除程序中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//枚举类型,是enum的子类
{
SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");//需传入参数 private Size(String abbreviation) { this.abbreviation = abbreviation; }
public String getAbbreviation() { return abbreviation; } private String abbreviation;
}

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

删除程序中Size枚举类,背录删除代码

代码:


package equals;

public class Manager extends Employee  //Manager类(子类)继承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 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+"]";
} }

 

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

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

注:int... intArray,...是可变参数,是int型,也可以改为其他型的   eg:String...StringArray

     实验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 Demo1 {
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();
}
}

结果:

201871010106-丁宣元 《面向对象程序设计(java)》第七周学习总结

3. 实验总结:

  通过本次实验,我学习到了:1.四种访问权限修饰符的使用特点   2.Object类   3.ArrayList类的方法    4.枚举类定义方法及用途

通过本次实验,加深了我对java继承特点的理解,并在助教讲解后对四种访问权限修饰符有了更深层面的认识,掌握了ArrayList类,枚举类,对java的学习有了更深的了解。通过线上线下学习,及实验课的练习,我对继承这一章的知识有了明确的把握,但在知识掌握上还是不足。在实验中遇到了许多问题,归根结底是知识掌握的不熟,在同学的帮助下基本解决了问题。在课余时间还是要加强对知识的理解,多练习。