实验七 继承附加实验
实验时间 2018-10-11
1、实验目的与要求
(1)进一步理解4个成员访问权限修饰符的用途;
private--私有域或私有方法:只能在定义它的类中使用
public--公有域或公有方法:在任何其他的类中都可以访问
protected--受保护的域或方法:在所有子类和本包中可以访问
不用修饰符--友好域和友好方法:在同一包中的不同类之间访问
(2)掌握Object类的常用API用法;
Object类是Java中所有类最终的祖先——每一个类都由它扩展而来。也就是说,在不给出超类的情况下,Java会自动把Object作为要定义类的超类。 可以使用类型为Object的变量指向任意类型的对象。但要对他们进行专门的操作,都要进行类型转换。
(3)掌握ArrayList类用法与常用API;
(4)掌握枚举类使用方法;
(5)结合本章知识,理解继承与多态性两个面向对象程序设计特征,并体会其优点;
多态性:发送消息给某个对象,让该对象自行决定响应何种行为。 通过将子类对象引用赋值给超类对象引用变量来实现动态方法调用。 java 的这种机制遵循一个原则:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法。
(6)熟练掌握Java语言中基于类、继承技术构造程序的语法知识(ch1-ch5);
(7)利用已掌握Java语言程序设计知识,学习设计开发含有1个主类、2个以上用户自定义类的应用程序。
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Son son2 = new Son(false); 6 Son son = new Son(); 7 son2.method(3); 8 son.method(); 9 }10 }11 12 class Parent {13 Parent() {14 15 }16 17 Parent(boolean b) {18 System.out.println("Parent's Constructor with a boolean parameter");19 }20 21 public void method() {22 System.out.println("Parent's method()");23 }24 }25 26 class Son extends Parent {27 // 补全本类定义28 Son() {29 System.out.println("Son's Constructor without parameter");30 }31 32 Son(boolean b) {33 super(b);34 }35 36 public void method(int a) {37 System.out.println("Son's method()");38 }39 }
2、实验内容和步骤
实验1 补充以下程序中主类内main方法体,以验证四种权限修饰符的用法。
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无修饰符修饰的方法"); } } 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属性,结合程序运行结果理解继承和权限修饰符的用法与区别*/ } } |
1 public class TEST1 { 2 private String t1 = "这是TEST1的私有属性"; 3 public String t2 = "这是TEST1的公有属性"; 4 protected String t3 = "这是TEST1受保护的属性"; 5 String t4 = "这是TEST1的默认属性"; 6 private void tese1() { 7 System.out.println("我是TEST1用private修饰符修饰的方法"); 8 } 9 public void tese2() {10 System.out.println("我是TEST1用public修饰符修饰的方法");11 }12 protected void tese3() {13 System.out.println("我是TEST1用protected修饰符修饰的方法");14 }15 void tese4() {16 System.out.println("我是TEST1无修饰符修饰的方法");17 }18 }
1 public class TEST2 extends TEST1{ 2 private String e1 = "这是TEST2的私有属性"; 3 public String e2 = "这是TEST2的公有属性"; 4 protected String e3 = "这是TEST2受保护的属性"; 5 String e4 = "这是TEST2的默认属性"; 6 public void demo1() { 7 System.out.println("我是TEST2用public修饰符修饰的方法"); 8 } 9 private void demo2() {10 System.out.println("我是TEST2用private修饰符修饰的方法");11 }12 protected void demo3() {13 System.out.println("我是TEST2用protected修饰符修饰的方法");14 }15 void demo4() {16 System.out.println("我是TEST2无修饰符修饰的方法");17 }18 }
1 public class Main { 2 public static void main(String[] args) { 3 TEST2 test= new TEST2(); 4 /*以下设计代码分别调用 demo1 demo2 demo3 demo4 test1 test2 test3 test4方法和t1 t2 t3 t3 e1 e2 e3 e4属性,结合程序运行结果理解继承和权限修饰符的用法与区别*/ 5 test.demo1(); 6 test.demo3(); 7 test.demo4(); 8 test.tese2(); 9 test.tese3();10 test.tese4();11 System.out.println(test.t2);12 System.out.println(test.t3);13 System.out.println(test.t4);14 System.out.println(test.e2);15 System.out.println(test.e3);16 System.out.println(test.e4);17 }18 }
实验2 第五章测试程序反思,继承知识总结。
测试程序1:
Ÿ 编辑、编译、调试运行教材程序5-8、5-9、5-10(教材174页-177页);
Ÿ 结合程序运行结果,理解程序代码,掌握Object类的定义及用法;
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 ArrayListTest11 {12 public static void main(String[] args)13 {14 // 填充雇员数组信息15 ArrayListstaff = 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 // 用以下方法为雇员涨5%的薪资22 for (Employee e : staff)23 e.raiseSalary(5);24 25 // 输出所有雇员对象的信息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 }
测试程序2:
Ÿ 编辑、编译、调试运行教材程序5-11(教材182页);
Ÿ
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 EnumTest11 { 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:
Ÿ 编辑、编译、调试运行程序5-12(教材189页);
Ÿ 结合运行结果,理解程序代码,掌握枚举类的定义及用法;
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 // 测试对象是否想等43 if (this == otherObject) return true;44 45 // 若不相等则返回错误,或返回空46 if (otherObject == null) return false;47 48 // 如果不是相同类型则不相等,返回错误信息49 if (getClass() != otherObject.getClass()) return false;50 51 // 确定other中的对象是雇员对象52 Employee other = (Employee) otherObject;53 54 // 测试是否在此域中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=" + hireDay66 + "]";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 //添加Manager的信息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//Manager类继承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 // 检查超类是否与此类相等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 }
实验总结:
通过两周的学习,对继承,以及子类如何继承父类的方法有了进一步的了解,理解了继承的多态性;懂得了4个成员访问权限修饰符的用途,以及如何使用。掌握了Object流泪,ArrayList类,以及枚举的定义和使用