201771010134杨其菊《面向对象程序设计java》第七周学习总结

                                                                                                                         第七周学习总结

第一部分:理论知识

1.继承是面向对象程序设计(Object Oriented Programming-OOP)中软件重用的关键技术。继承机制使用已经定义的类作为基础建立新的类定义,新的类是原有类的数据及操作与新类所增加的数据及操作组合。新的类把原有的类作为基类引用,而不需要修改原有类的定义。新定义的类作为派生类引用。这种方式减少代码数量,让代码简洁,易懂。

•  继承可以传递,但是继承时单项的。

•  多继承的容易出现二义性,慎用。

•  私有成员不被继承,不能被派生类使用。

2.访问修饰符可访问性:

 

3.0bject:

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

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

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
 

第二部分:实验部分:

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类的定义及用法;

 1 package equals;
 2 
 3 import java.time.*;
 4 import java.util.Objects;
 5 
 6 public class Employee
 7 {
 8    private String name;
 9    private double salary;
10    private LocalDate hireDay;
11 
12    public Employee(String name, double salary, int year, int month, int day)
13    {
14       this.name = name;
15       this.salary = salary;
16       hireDay = LocalDate.of(year, month, day);
17    }
18 
19    public String getName()
20    {
21       return name;
22    }
23 
24    public double getSalary()
25    {
26       return salary;
27    }
28 
29    public LocalDate getHireDay()
30    {
31       return hireDay;
32    }
33 
34    public void raiseSalary(double byPercent)
35    {
36       double raise = salary * byPercent / 100;
37       salary += raise;
38    }
39 
40    public boolean equals(Object otherObject)
41    {
42       // a quick test to see if the objects are identical
43       if (this == otherObject) return true;
44 
45       // must return false if the explicit parameter is null
46       if (otherObject == null) return false;
47 
48       // if the classes don't match, they can't be equal
49       if (getClass() != otherObject.getClass()) return false;
50 
51       // now we know otherObject is a non-null Employee
52       Employee other = (Employee) otherObject;
53 
54       // test whether the fields have identical values
55       return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
56    }
57 
58    public int hashCode()
59    {
60       return Objects.hash(name, salary, hireDay); 
61    }
62 
63    public String toString()
64    {
65       return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay
66             + "]";
67    }
68 }
 1 package equals;
 2 
 3 /**
 4  * This program demonstrates the equals method.
 5  * @version 1.12 2012-01-26
 6  * @author Cay Horstmann
 7  */
 8 public class EqualsTest
 9 {
10    public static void main(String[] args)
11    {
12       Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
13       Employee alice2 = alice1;
14       Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
15       Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
16 
17       System.out.println("alice1 == alice2: " + (alice1 == alice2));
18 
19       System.out.println("alice1 == alice3: " + (alice1 == alice3));
20 
21       System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
22 
23       System.out.println("alice1.equals(bob): " + alice1.equals(bob));
24 
25       System.out.println("bob.toString(): " + bob);
26 
27       Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
28       Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
29       boss.setBonus(5000);
30       System.out.println("boss.toString(): " + boss);
31       System.out.println("carl.equals(boss): " + carl.equals(boss));
32       System.out.println("alice1.hashCode(): " + alice1.hashCode());
33       System.out.println("alice3.hashCode(): " + alice3.hashCode());
34       System.out.println("bob.hashCode(): " + bob.hashCode());
35       System.out.println("carl.hashCode(): " + carl.hashCode());
36    }
37 }
 1 package equals;
 2 
 3 public class Manager extends Employee
 4 {
 5    private double bonus;
 6 
 7    public Manager(String name, double salary, int year, int month, int day)
 8    {
 9       super(name, salary, year, month, day);
10       bonus = 0;
11    }
12 
13    public double getSalary()
14    {
15       double baseSalary = super.getSalary();
16       return baseSalary + bonus;
17    }
18 
19    public void setBonus(double bonus)
20    {
21       this.bonus = bonus;
22    }
23 
24    public boolean equals(Object otherObject)
25    {
26       if (!super.equals(otherObject)) return false;
27       Manager other = (Manager) otherObject;
28       // super.equals checked that this and other belong to the same class
29       return bonus == other.bonus;
30    }
31 
32    public int hashCode()
33    {
34       return java.util.Objects.hash(super.hashCode(), bonus);
35    }
36 
37    public String toString()
38    {
39       return super.toString() + "[bonus=" + bonus + "]";
40    }
41 }

测试程序2:

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

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

 1 package arrayList;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * This program demonstrates the ArrayList class.
 7  * @version 1.11 2012-01-26
 8  * @author Cay Horstmann
 9  */
10 public class ArrayListTest
11 {
12    public static void main(String[] args)
13    {
14       // fill the staff array list with three Employee objects
15       ArrayList<Employee> staff = new ArrayList<>();
16 
17       staff.add(new Employee("Carl Cracker", 75000, 1987, 12, 15));
18       staff.add(new Employee("Harry Hacker", 50000, 1989, 10, 1));
19       staff.add(new Employee("Tony Tester", 40000, 1990, 3, 15));
20 
21       // raise everyone's salary by 5%
22       for (Employee e : staff)
23          e.raiseSalary(5);
24 
25       // print out information about all Employee objects
26       for (Employee e : staff)
27          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
28                + e.getHireDay());
29    }
30 }
 1 package arrayList;
 2 
 3 import java.time.*;
 4 
 5 public class Employee
 6 {
 7    private String name;
 8    private double salary;
 9    private LocalDate hireDay;
10 
11    public Employee(String name, double salary, int year, int month, int day)
12    {
13       this.name = name;
14       this.salary = salary;
15       hireDay = LocalDate.of(year, month, day);
16    }
17 
18    public String getName()
19    {
20       return name;
21    }
22 
23    public double getSalary()
24    {
25       return salary;
26    }
27 
28    public LocalDate getHireDay()
29    {
30       return hireDay;
31    }
32 
33    public void raiseSalary(double byPercent)
34    {
35       double raise = salary * byPercent / 100;
36       salary += raise;
37    }
38 }

 

测试程序3:

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

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

 1 package enums;
 2 
 3 import java.util.*;
 4 
 5 /**
 6  * This program demonstrates enumerated types.
 7  * @version 1.0 2004-05-24
 8  * @author Cay Horstmann
 9  */
10 public class EnumTest
11 {  
12    public static void main(String[] args)
13    {  
14       Scanner in = new Scanner(System.in);
15       System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
16       String input = in.next().toUpperCase();
17       Size size = Enum.valueOf(Size.class, input);
18       System.out.println("size=" + size);
19       System.out.println("abbreviation=" + size.getAbbreviation());
20       if (size == Size.EXTRA_LARGE)
21          System.out.println("Good job--you paid attention to the _.");      
22    }
23 }
24 
25 enum Size
26 {
27    SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");
28 
29    private Size(String abbreviation) { this.abbreviation = abbreviation; }
30    public String getAbbreviation() { return abbreviation; }
31 
32    private String abbreviation;
33 }

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

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

 第三部分:总结

    1.继承最大的特点就是代码重用,让代码变得简洁。继承的学习让我又积累了优化代码的方法。

     2.进一步加深了对4个成员访问权限修饰符的用途的理解。

      3.:晴天不多走,雨天不偷懒。最大的问题不是学习的问题,而是学习方法的问题。每天至少敲100行代码。



猜你喜欢

转载自www.cnblogs.com/yqj-yf-111/p/9785674.html