201771010112罗松《面向对象程序设计(java)》第七周学习总结

 第一部分:理论知识

1.访问修饰符:private,protected,public,默认

使用访问修饰符的原因:实现受限信息隐藏。 

信息隐藏目的:  对类中任何实现细节的更改不会影响使用该类的代码,防止用户意外删除数据,易于使用类。

public:该类或非该类均可访问

private:只有该类可以访问

protected:该类及其子类的成员可以访问,同一个包中的类也可访问

默认:相同包中的类可以访问

2.Object:所有类的超类 

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

可以使用类型为Object的变量指向任意类型的对象。但 要对它们进行专门的操作都要进行类型转换。

3.ArrayList 

ArrayList 对象=new ArrayList(); 例:ArrayList staff =new ArrayList(); 

API:ArrayList 的构造器 – ArrayList()构造一个空数组列表 – ArrayList(int initialCapacity)构造一个具有 指定容量的空数组列表

添加新元素 API:boolean add(T obj) 把元素obj追加到数组列表的结尾 例:staff.add(new Employee(…));

统计个数 API:int size() 返回数组列表中当前元素个数 例:staff.size();

调整大小 API:void trimToSize() 把数组列表的存贮空间调整到当前大小

访问 API:void set(int index, T obj) 将obj放入数组列表index位置,将覆盖这 个位置的原有内容。 API:T get(int index) 获得指定位置index的元素值 例:Employee harry = new Employee(…); staff.set(1, harry); Employee e = staff.get(1);

增加与删除 API:boolean add(int index, T obj) 向后移动元素,在第n 个位置插入obj API:T remove(int index); 将第n个位置存放的对象删除,并将后面的 元素向前移动 例:staff.add(i,harry); Employee e = staff.remove(i);

4.equals
        0bject类中的equals方法用于测试某个对象是否同另-一个对象相等。它在0b ject类中的实现是判断两个对象是否具有相同的引用。如果两个对象具有相同的引用,它们一定是相等的。

如果需要检测两个对象状态的相等性,就需要在新类的定义中需要覆盖equals方法。定义子类的equals方法时,可调用超类的equals方法。

  super.equals ( otherObject)
5.hascCode:
     0bject类中的hashCode方法导出某个对象的散列码。散列码是任意整数,表示对象的存储地址。两个相等对象的散列码相等。

6.Java中,利用ArrayList类,可允许程序在运行时确定数组的大小。ArryList是-一个采用类型参数的泛型类。为指定数组列表保存元素的对象类型,需要用一一对尖括号将数组元素对象类名括起来加在后面。没有<>的ArrayList将被认为是一个删去了类型参数的“原始”类型。

7.为了提高程序的清晰度,包含一个或多个抽象方法的类本身必须被声明为抽象类。除了抽象方法之外抽象类还可以包含具体数据和具体方法。由象方法充当着占位的角色,它们的具体实现在子类中。扩展抽象类可以有两种选择:一种是在子类中实现部分抽象方法,这样就必须将子类也标记为由象类;另-种是实现全部抽象方法,这样子类就可以不是抽象类。此外,类即使不含抽象方法,也可以将类声明为抽象类。由象类不能被实例化,即不能创建对象,只能产生子类。可以创建抽象类的对象变量,只是这个变量必须指向它的非抽象子类的对象。
8.继承设计的技巧
①  将公共操作和域放在超类。②不要使用受保护的域。
③  使用继承实现“is-a”关系。
④  除非所有继承的方法都有意义,否则就不要
使用继承。
⑤  在覆盖方法时,不要改变预期的行为。
⑥  使用多态,  而非类型信息。
⑦不要过多地使用反射。

 9. 封装、继承和多态是面向对象的主要特征
继承可提高代码的重用性,使用extends关键 字来实现。除了构造方法之外,父类的所有方法和属性都被子类的对象继承
多态性是不同的实例对象以不同的方式对相同的信息作出不同的表现
访问修饰符用于确定访问类成员的方式Java常用修饰符有static、 final、 abstract

10.枚举类

声明枚举类 public enum Grade { A, B, C, D, E }; 

它包括一个关键字enum,一个新枚举类型的名字 Grade以及为Grade定义的一组值。

11.抽象类

一个类一定被定义成抽象类,他就是一个专职父类。不能实例化,即不能创建对象,只能产生子类。

第二部分:实验部分

1、实验目的与要求

(1)进一步理解4个成员访问权限修饰符的用途; 

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

(3)掌握ArrayList类用法与常用API;

(4)掌握枚举类使用方法;

(5)结合本章知识,理解继承与多态性两个面向对象程序设计特征,并体会其优点;

(6)熟练掌握Java语言中基于类、继承技术构造程序的语法知识(ch1-ch5);

(7)利用已掌握Java语言程序设计知识,学习设计开发含有1个主类、2个以上用户自定义类的应用程序。

2、实验内容和步骤

实验1  补充以下程序中主类内main方法体,以验证四种权限修饰符的用法。

1 public class Test {
 2     private String t1 = "这是 Test 的私有属性";
 3     public String t2 = "这是Test的公有属性";
 4     protected String t3 = "这是Test受保护的属性";
 5     String t4 = "这是Test的默认属性";
 6     private void tese1() {
 7         System.out.println("我是Test用private修饰符修饰的方法");
 8     }
 9     public void tese2() {
10         System.out.println("我是Test用public修饰符修饰的方法");
11     }
12     protected void tese3() {
13         System.out.println("我是Test用protected修饰符修饰的方法");
14     }
15     void tese4() {
16         System.out.println("我是Test无修饰符修饰的方法");
17     }
18 }
19 public class Test2 extends Test{
20     private String e1 = "这是Test2的私有属性";
21     public String e2 = "这是Test2的公有属性";
22     protected String e3 = "这是Test2受保护的属性";
23     String e4 = "这是Test2的默认属性";
24     public void demo1() {
25         System.out.println("我是Test2用public修饰符修饰的方法");
26     }
27     private void demo2() {
28         System.out.println("我是Test2用private修饰符修饰的方法");
29     }
30     protected void demo3() {
31         System.out.println("我是Test2用protected修饰符修饰的方法");
32     }
33     void demo4() {
34         System.out.println("我是Test2无修饰符修饰的方法");
35     }
36 }
37 public class Main {
38     public static void main(String[] args) {
39         Test2 test= new Test2();
40         /*以下设计代码分别调用 demo1 demo2 demo3 demo4 test1 test2 test3 test4方法和t1 t2 t3 t3 e1 e2 e3 e4属性,结合程序运行结果理解继承和权限修饰符的用法与区别*/
41     
42          test.demo1();
43          test.demo3();
44          test.demo4();
45     
46          test.tese2();
47          test.tese3();
48          test.tese4();
49     
50          System.out.println(test.t2);
51          System.out.println(test.t3);
52          System.out.println(test.t4);
53          System.out.println(test.e2);
54          System.out.println(test.e3);
55          System.out.println(test.e4);
56     }
57 }

结果:

 

 

实验2  第五章测试程序反思,继承知识总结。

测试程序1:

Ÿ 编辑、编译、调试运行教材程序5-8、5-9、5-10(教材174页-177页);

Ÿ 结合程序运行结果,理解程序代码,掌握Object类的定义及用法;

代码:

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
      Employee 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
            + "]";
   }
}
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:

Ÿ 编辑、编译、调试运行教材程序5-11(教材182页);

Ÿ 结合程序运行结果,理解程序代码,掌握ArrayList类的定义及用法;

代码:

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

结果:

测试程序3:

Ÿ 编辑、编译、调试运行程序5-12(教材189页);

Ÿ 结合运行结果,理解程序代码,掌握枚举类的定义及用法;

代码:

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

结果:

实验3采用个人账号登录https://pintia.cn/,完成《2018秋季西北师范大学面向对象程序设计(Java)(ch1-ch5)测试题2》,测试时间60分钟;

实验4: 课后完成实验3未完成的测试内容。

实验总结:

通过这一周的学习,我深入理解OO程序设计的特征:继承、多态;并且开始慢慢掌握Java语言中基于类、继承技术构造程序的语法知识。并且在这次试验中我感受到了继承的便利性,继承最大的特点就是代码重用,使代码变得简洁,这次实验还使我学会了枚举类使用方法,总之收获很大。同时我也认识到在之后的学习中要更加努力,才能充分学习和消化掉所学知识。

   

 

posted @ 2018-10-14 13:27  如果优质豺狼它英勇披上婚纱  阅读(151)  评论(0编辑  收藏  举报