201871010107-公海瑜《面向对象程序设计(java)》第七周学习总结
项目 | 内容 |
这个作业属于哪个课程 | http://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 | https://www.cnblogs.com/nwnu-daizh/p/11654436.html |
作业的学习目标 |
|
实验内容和步骤
实验1:在“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法。
代码如下:
package test;
class Parent {
private String p1 = "这是Parent的私有属性";
public String p2 = "这是Parent的公有属性";
protected String p3 = "这是Parent受保护的属性";
String p4 = "这是Parent的默认属性";
private void pMethod1() {
System.out.println("我是Parent用private修饰符修饰的方法");
}
public void pMethod2() {
System.out.println("我是Parent用public修饰符修饰的方法");
}
protected void pMethod3() {
System.out.println("我是Parent用protected修饰符修饰的方法");
}
void pMethod4() {
System.out.println("我是Parent无修饰符修饰的方法");
}
}
class Son extends Parent{
private String s1 = "这是Son的私有属性";
public String s2 = "这是Son的公有属性";
protected String s3 = "这是Son受保护的属性";
String s4 = "这是Son的默认属性";
public void sMethod1() {
System.out.println(p2);//分别尝试显示Parent类的p1、p2、p3、p4值
System.out.println("我是Son用public修饰符修饰的方法");
}
private void sMethod2() {
System.out.println("我是Son用private修饰符修饰的方法");
}
protected void sMethod() {
System.out.println("我是Son用protected修饰符修饰的方法");
}
void sMethod4() {
System.out.println("我是Son无修饰符修饰的方法");
}
}
public class Test {
public static void main(String[] args) {
Parent parent=new Parent();
Son son=new Son();
System.out.println(parent.p2); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法
System.out.println(parent.p3);
System.out.println(parent.p4);
System.out.println(son.p2);
System.out.println(son.p3);
System.out.println(son.p4);
System.out.println(son.s2);
System.out.println(son.s3);
System.out.println(son.s4);
}
}
运行结果如图:
将代码放入以自己名字缩写命名的包中
Parent类代码:
package ghy; public class parent { private String p1 = "这是Parent的私有属性"; public String p2 = "这是Parent的公有属性"; protected String p3 = "这是Parent受保护的属性"; String p4 = "这是Parent的默认属性"; private void pMethod1() { System.out.println("我是Parent用private修饰符修饰的方法"); } public void pMethod2() { System.out.println("我是Parent用public修饰符修饰的方法"); } protected void pMethod3() { System.out.println("我是Parent用protected修饰符修饰的方法"); } void pMethod4() { System.out.println("我是Parent无修饰符修饰的方法"); } }
运行结果如图:
这个实验主要是让我们了解关于四种访问权限的修饰符:
位置 | private |
默认 |
protected |
public |
同一个类 |
是 | 是 | 是 | 是 |
同一个包里的类 |
否 | 是 | 是 | 是 |
不同包内的子类 |
否 | 否 | 是 | 是 |
不同包并且不是子类 |
否 | 否 | 否 | 是 |
实验2:导入第5章以下示例程序,测试并进行代码注释。
测试程序1:
l 运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);
删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。
EqualsTest类:
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) { var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15); var alice2 = alice1; var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15); var 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); var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15); var 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()); } }
Employee类:
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; // 现在我们知道另一个对象是非空雇员 var 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 + "]"; } }
Manager类:
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; var other = (Manager) otherObject; //检查这个和其他属于同一个类
return bonus == other.bonus;
}
public int hashCode()
{
return java.util.Objects.hash(super.hashCode(), bonus);
}
public String toString()
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
运行结果如图:
equals方法:用于检测一个对象是否等于另外一个对象,在子类中调用equals方法时,首先调用超类的equals,如果检测失败,对象就不可能相等,如果超类中的域都相等,就需要比较子类中的实例域。
hashCode方法:散列码(hash code)是由对象导出的一个整型值,且没有规律,由于hashCode方法定义在object类中,因此每个对象都有一个默认的散列码,其值是对象的存储地址该方法返回一个整型数值。
toString方法:用于返回表示对象值的字符串,只要对象与一个字符串通过操作符“+”连接起来,Java编译就会自动地调用toString方法。
测试程序2:
l 在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;
l 掌握ArrayList类的定义及用法;
l 在程序中相关代码处添加新知识的注释;
l 设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法。
ArrayListTst 类代码如下:
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数组列表 var staff = new ArrayList<Employee>(); 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()); } }
Employee类代码如下:
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:
l 编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;
l 掌握枚举类的定义及用法;
l 在程序中相关代码处添加新知识的注释;
删除程序中Size枚举类,背录删除代码,在代码录入中掌握枚举类的定义要求。
代码如下:
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) { var 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); //静态values方法返回枚举的所有值的数组 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; } //调用构造函数
运行结果如图:
删除Size枚举类程序代码如下:
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) { var 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); //静态values方法返回枚举的所有值的数组 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; }//调用构造函数
运行结果如图:
测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法
代码如下:
public class TestVarArgus { public static void dealArray(int... intArray){ for (int i : intArray) System.out.print(i +" "); System.out.println(); } public static void main(String args[]){ dealArray(); dealArray(1); dealArray(1, 2, 3); } }
运行结果如图:
实验:3:编程练习:参照输出样例补全程序,使程序输出结果与输出样例一致。
样例代码如下:
public class Demo { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { //补全本类定义 }
要求运行结果如下:
Parent's Constructor with a boolean parameter Son's Constructor without parameter Son's method() Parent's method()
补全代码如下:
package test; public class ghy { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { Son(){ super(false); System.out.println("Son's Constructor without parameter"); System.out.println("Son's method()"); } }
运行结果如图:
二、实验总结
这周继续学习了第五章,较国庆放假之前,加深了对继承类、抽象类以及多态的学习,但基础知识掌握还是不到位。编写代码过程中,总是会出现这样那样的错误。在以后的学习中,我要更加认真的看书,练习编程,提高自己程序的准确性。