201871010108高文利《面向对象程序设计(java)》第七周学习总结 201871010108

项目

内容

这个作业属于哪个课程

<任课教师博客主页链接>

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

这个作业的要求在哪里

<作业链接地址>

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

作业学习目标

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

实验内容和步骤

实验1:(20分)

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

package Parent1;

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

  

实验2:测试程序1(15分)

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

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

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());//导出alice1对象的散列码
      System.out.println("alice3.hashCode(): " + alice3.hashCode());
      System.out.println("bob.hashCode(): " + bob.hashCode());
      System.out.println("carl.hashCode(): " + carl.hashCode());
   }
}

  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 int hashCode() {
	// TODO Auto-generated method stub
	return Objects.hash(name, salary, hireDay); 

}

@Override
public boolean equals(Object otherObject) {
	// TODO Auto-generated method stub
	
    if (this == otherObject) return true;   
    if (otherObject == null) return false;    
    if (getClass() != otherObject.getClass()) return false;
    var other = (Employee) otherObject;
    return Objects.equals(name, other.name) 
       && salary == other.salary && Objects.equals(hireDay, other.hireDay);
}

@Override
public String 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 int hashCode() {
	// TODO Auto-generated method stub
	return java.util.Objects.hash(super.hashCode(), bonus);

}

@Override
public boolean equals(Object otherObject) {
	// TODO Auto-generated method stub

    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;

}

@Override
public String toString() {
	// TODO Auto-generated method stub
	return super.toString() + "[bonus=" + bonus + "]";

}
   
}

  

实验2:测试程序2(15分)

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

掌握ArrayList类的定义及用法;

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

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

ArrayListTest.java

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));
      
      // for each循环
      for (Employee e : staff)
         e.raiseSalary(5);
      // 输出所有Employee对象的信息
      for (Employee e : staff)
         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;
   }
}

  

 

在ArrayListTest.java中添加

 Employee b=staff.remove(1);//移除第一个位置上存放的对象,并将后面的元素向前移

 System.out.println(staff.size());//返回当前数组列表中的元素个数

Employee a=new Employee("Wang",10000,1999,2,15);
staff.set(1, a);//将a放入数组列表的第1个位置,将这个位置原有的内容覆盖

Employee b=staff.get(2);//得到第二个位置的元素值
staff.add(b);//并将得到的元素值追加在数组列表的末尾

实验2:测试程序3(15分)

编辑、编译、调试运行程序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;
	
	
  
}

  

实验2:测试程序4(5分)

public class TestVarArgus {  
    public static void dealArray(int... intArray){  参数数量可变
        for (int i : intArray)  //for each循环
            System.out.print(i +" ");  
          
        System.out.println();  
    }        
    public static void main(String args[]){  
        dealArray();  
        dealArray(1);  
        dealArray(1, 2, 3);  
    }  
}

  

实验3:编程练习(10分)

package Parent1;
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);  
        System.out.println("Son's Constructor without parameter");
    }
    public void method() {
        System.out.println("Son's method()");
        super.method();
    }

}

  

实验总结:在本周的学习下,更深层次的学习了继承,理解并运用。以及object类。通过实验课老师及学长的讲解,对私有属性,公有属性,protected属性,默认属性四种属性的理解以及应用。

object类作为所有类的父类,不能在拓展父类,以及不能在拓展子类的final类。泛型数组列表和枚举类的学习。

原文地址:https://www.cnblogs.com/gwlg/p/11667374.html