张云飞 201771010143《面向对象程序设计(java)》第七周学习总结
实验七继承附加实验
实验时间2018-10-11
1、实验目的与要求
(1)进一步理解4个成员访问权限修饰符的用途;
1.仅对本类可见-private
2.对所有类可见-public
3.对本包和所有子类可见-protected
4.对本包可见-默认,不需要修饰符
掌握Object类的常用API用法;
toString方法:它用于返回对象值的字符串
equals方法:检测一个对象是否等于另一个对象(不同于==)
hasCode方法:获得散列码(其值为对象的存储地址)
1.子类继承父类的成员变量
当子类继承了某个类之后,便可以使用父类中的成员变量,但是并不是完全继承父类的所有成员变量。具体的原则如下:
1)能够继承父类的public和protected成员变量;不能够继承父类的private成员变量;
2)对于父类的包访问权限成员变量,如果子类和父类在同一个包下,则子类能够继承;否则,子类不能够继承;
3)对于子类可以继承的父类成员变量,如果在子类中出现了同名称的成员变量,则会发生隐藏现象,即子类的成员变量会屏蔽掉父类的同名成员变量。如果要在子类中访问父类中同名成员变量,需要使用super关键字来进行引用。super.属性名
2.子类继承父类的方法
子类并不是完全继承父类的所有方法。
1)能够继承父类的public和protected成员方法;不能够继承父类的private成员方法;
2)对于父类的包访问权限成员方法,如果子类和父类在同一个包下,则子类能够继承;否则,子类不能够继承;
3)对于子类可以继承的父类成员方法,如果在子类中出现了同名称的成员方法,则称为覆盖,即子类的成员方法会覆盖掉父类的同名成员方法。如果要在子类中访问父类中同名成员方法,需要使用super关键字来进行引用。super.方法名
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属性,结合程序运行结果理解继承和权限修饰符的用法与区别*/ } } |
public class Main {
public static void main(String[] args) {
TEST2 test2 = new TEST2();
test2.demo1();
test2.demo3();
test2.demo4();
test2.test2();
test2.test3();
test2.test4();
String e2=test2.e2;
String e3=test2.e3;
String e4=test2.e4;
System.out.println(e2);
System.out.println(e3);
System.out.println(e4);
System.out.println(test2.t2);
System.out.println(test2.t3);
System.out.println(test2.t4);
}
}
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无修饰符修饰的方法");
}
}
TEST1
public class TEST2 extends TEST1{
private String e1 = "这是TEST2的私有属性";
public String e2 = "这是TEST2的公有属性";
protected String e3 = "这是TEST2受保护的属性";
String e4 = "这是TEST1的默认属性";
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无修饰符修饰的方法");
}
}
TEST2
Main
实验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)
{
// 快速测试,看看这些对象是否相同
if (this == otherObject) return true;
// 如果显式参数为空,则必须返回false
if (otherObject == null) return false;
// 如果类不匹配,它们就不能相等
if (getClass() != otherObject.getClass()) return false;
// 现在我们知道otherObject是一个非空雇员
Employee other = (Employee) otherObject;
// 测试字段是否具有相同的值
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;
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;
}
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 + "]";
}
}
/**
* 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;
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;
// 如果显式参数为空,则必须返回false
if (otherObject == null) return false;
// 如果类不匹配,它们就不能相等
if (getClass() != otherObject.getClass()) return false;
// 现在我们知道otherObject是一个非空雇员
Employee other = (Employee) otherObject;
// 测试字段是否具有相同的值
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;
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;
}
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 + "]";
}
}
/**
* 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());
}
}
测试程序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)
{
// 用三个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());
}
}
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未完成的测试内容。
本周学习总结:在本周的学习过程中,实验课上听过了学长的讲解后对于四个修饰符有了更加透彻的理解,最初使用的时候只是大概将其敲上去,当时并不理解各个修饰符在功能上的不同点。1.仅对本类可见-private2.对所有类可见-public3.对本包和所有子类可见-protected4.对本包可见-默认,不需要修饰符。
第二:在pintia平台上做编程题目时,总是得到结果错误的反馈,在及时和学长沟通之后,得到了两个收获,一个是抽象类也可以和主类放在一个class中运行。另外就是时刻注意程序的输入输出的格式是否和题目要求中的一致。