201771010108 -韩腊梅-第七周学习总结

第六周实验总结 

一、知识点总结

1、继承的概述:在多个类中存在相同的属性和行为,把这些相同的部分抽取到一个单独的类中,把这个单独的类叫作父类,也叫基类或者超类,把其他被抽取的类叫作子类,并且父类的所有属性和方法(除private修饰的私有属性和方法外),子类都可以调用。这样的一种行为就叫做继承。(相同的东西在父类,不同的东西在子类)

2、继承的关键字:extends

3、继承的格式:class 子类名  extends  父类名{    }

4、在代码中使用继承提高了代码的复用性和维护性,让类与类直接产生了关系。

5、继承的注意点:

①子类只能继承父类所有的非私有的成员方法和成员变量,private修饰的不能继承。

②子类不能继承父类的构造方法,但可以通过   super   关键字去访问父类的构造方法。(先初始化父类,再执行自己)

③不同包不能继承。

 6、在使用  super  的时候,我们还需要了解关键字 super  和  this  的区别:

         super :到父类中去找方法,没有引用的作用;也可以用于其他方法中;与this调用构造方的重载一样,用于第一行。

         this:是指当前正在初始化的这个对象的引用。

二、实验七继承附加实验

1、实验目的与要求

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

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

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

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

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

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

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

2、实验内容和步骤

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

public class TEST1 {

private String t1 = "这是TEST1的私有属性";

public String t2 = "这是TEST1的公有属性";

protected String t3 = "这是TEST1受保护的属性";

String t4 = "这是TEST1的默认属性";

private void test1() {

System.out.println("我是TEST1用private修饰符修饰的方法");

}

public void test2() {

System.out.println("我是TEST1用public修饰符修饰的方法");

}

protected void test3() {

System.out.println("我是TEST1用protected修饰符修饰的方法");

}

void test4() {

System.out.println("我是TEST1无修饰符修饰的方法");

}

}

public class TEST2 extends TEST1{

private String e1 = "这是TEST2的私有属性";

public String e2 = "这是TEST2的公有属性";

protected String e3 = "这是TEST2受保护的属性";

String e4 = "这是TEST2的默认属性";

public void demo1() {

System.out.println("我是TEST2用public修饰符修饰的方法");

}

private void demo2() {

System.out.println("我是TEST2用private修饰符修饰的方法");

}

protected void demo3() {

System.out.println("我是TEST2用protected修饰符修饰的方法");

}

void demo4() {

System.out.println("我是TEST2无修饰符修饰的方法");

}

}

public class Main {

public static void main(String[] args) {

TEST2 test2 = new TEST2();

/*以下设计代码分别调用 demo1 demo2 demo3 demo4 test1 test2 test3 test4方法和t1 t2 t3 t3 e1 e2 e3 e4属性,结合程序运行结果理解继承和权限修饰符的用法与区别*/

}

}

实验代码:

package dei;

public class Main {
        public static void main(String[] args) {
            TEST2 test2 = new TEST2();
           test2.demo1();
            test2.demo3();
            test2.demo4();
            test2.tese2();
            test2.tese3();
            test2.tese4();
            System.out.println(test2.e2);
            System.out.println(test2.e3);
            System.out.println(test2.e4);
            System.out.println(test2.t2);
            System.out.println(test2.t3);
            System.out.println(test2.t4);
            }
}
package dei;

public class TEST1 {
    private String t1 = "这是TEST1的私有属性";
    public String t2 = "这是TEST1的公有属性";
    protected String t3 = "这是TEST1受保护的属性";
    String t4 = "这是TEST1的默认属性";
    private void tese1() {
        System.out.println("我是TEST1用private修饰符修饰的方法");
    }
    public void tese2() {
        System.out.println("我是TEST1用public修饰符修饰的方法");
    }
    protected void tese3() {
        System.out.println("我是TEST1用protected修饰符修饰的方法");
    }
    void tese4() {
        System.out.println("我是TEST1无修饰符修饰的方法");
    }
}
package dei;
    public class TEST2 extends TEST1{
        private String e1 = "这是TEST2的私有属性";
        public String e2 = "这是TEST2的公有属性";
        protected String e3 = "这是TEST2受保护的属性";
        String e4 = "这是TEST2的默认属性";
        public void demo1() {
            System.out.println("我是TEST2用public修饰符修饰的方法");
        }
        private void demo2() {
            System.out.println("我是TEST2用private修饰符修饰的方法");
        }
        protected void demo3() {
            System.out.println("我是TEST2用protected修饰符修饰的方法");
        }
        void demo4() {
            System.out.println("我是TEST2无修饰符修饰的方法");
        }
    }
    

实验结果:

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

测试程序1:

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

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

测试代码:

/**
 * 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);//在子类中Bouns赋初值为空,主类中用更改器更改为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());
   }
}
EqualsTest
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)
   {
      //快速测试几个类的根是否相同,即是否是同一个超类。
      if (this == otherObject) return true;
      if (otherObject == null) return false;

      //用getclass()方法得到对象的类。如果几个类不匹配,则它们不相等
      if (getClass() != otherObject.getClass()) return false;
      Employee other = (Employee) otherObject;
      //测试字段是否具有相同的值
      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
            + "]";
   }
}
Employee
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检查这个类和其他是否属于同一个类
      return bonus == other.bonus;
   }

   public int hashCode()
   {
      return java.util.Objects.hash(super.hashCode(), bonus);
   }

   public String toString()
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }
}
Manager

测试结果:

测试程序2:

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

Ÿ   结合程序运行结果,理解程序代码,掌握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数组列表
      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));

      //将每个人的薪水提高5%
      for (Employee e : staff)
         e.raiseSalary(5);

      //打印出所有Employee类的信息
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
               + e.getHireDay());
   }
}
ArrayListTest
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;
   }//定义两个局部变量
}
Employee

测试结果:

测试程序3:

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

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

测试代码:

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

测试结果:

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

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

三、实验总结

      通过本次的实验测试和之前的实验,我进一步理解4个成员访问权限修饰符的用途,大致上掌握Object类的常用API用法,ArrayList类用法与常用API,枚举类使用方法;但在学习设计开发含有1个主类、2个以上用户自定义类的应用程序上有很大的问题,感觉知识点就像是一些条理,但在实际应用中会有很多问题,写代码过程中没有自我修改意识,遇到问题就不知道该怎么办,知识点好像就是了解一样,不会使用已学的知识去解决实际问题,编程能力还是很差。

猜你喜欢

转载自www.cnblogs.com/hanlamei/p/9786006.html