|
项目 |
内容 |
|
《面向对象程序设计(java)》 |
https://www.cnblogs.com/nwnu-daizh/ |
|
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11654436.html |
|
作业学习目标 |
|
实验内容和步骤
实验1:(20分)
程序代码如下:
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( );//分别尝试显示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 Demo {
public static void main(String[] args) {
Parent parent=new Parent();
Son son=new Son();
System.out.println( ); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法
}
}
因为private属性仅对本类可见,public属性对所有类可见,protected属性对本包和所有类可见,默认属性对本包可见。
而Parent类的p1是Parent的私有属性,在子类中不能被调用,所以程序出错,无法显示;p2是Parent的公有属性,p3是Parent受保护的属性,p4是Parent的默认属性,这些属性能在子类中被调用,所以可以显示p2、p3、p4的值.
运行过程如图:

同上。parent类和son类的私有属性也不能被调用,所以调用p1与s1程序运行错误。
用parent调用Paren类的方法运行结果如下:




用son调用Son类的方法运行结果如下:




实验2:测试程序1(15分)
5-8程序代码如下:
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());
}
}
5-9程序代码如下:
package equals;
import java.time.*;
import java.util.Objects;
public class Employee
{
private String name;//private定义了一个只能在该类中访问的字符串变量
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)
{
// a quick test to see if the objects are identical 快速测试这些对象是否相同
if (this == otherObject) return true;
// must return false if the explicit parameter is null 如果显示参数为空,必须返回false
if (otherObject == null) return false;
// if the classes don't match, they can't be equal 如果第几个类不匹配,则他们不相同
if (getClass() != otherObject.getClass()) return false;
// now we know otherObject is a non-null Employee //其他对象为非空Employee类
var other = (Employee) otherObject;
// test whether the fields have identical values //测试是不是有相同值
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 + "]";
}
}
5-10程序代码如下:
package equals;
public class Manager extends Employee //扩展了一个子类Manager
{
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;
// super.equals checked that this and other belong to the same class 用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 + "]";
}
}
5-8、5-9、5-10程序运行结果如下:

删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。
equals方法:用于检测一个对象是否等于另外一个对象,在子类中调用equals方法时,首先调用超类的equals,如果检测失败,对象就不可能相等,如果超类中的域都相等,就需要比较子类中的实例域。
hashCode方法:散列码(hash code)是由对象导出的一个整型值,且没有规律,由于hashCode方法定义在object类中,因此每个对象都有一个默认的散列码,其值是对象的存储地址该方法返回一个整型数值。
toString方法:用于返回表示对象值的字符串,只要对象与一个字符串通过操作符“+”连接起来,Java编译就会自动地调用toString方法。
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;
}
@Override
public int hashCode() { //重写hashCode方法,使相等的两个对象获取的HashCode也相等
// TODO Auto-generated method stub
return Objects.hash(name, salary, hireDay);
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj) return true; //快速测试几个类的根是否相同,即是否是同一个超类。这个if语句判断两个引用是否是同一个,如果是同一个,那么这两个对象肯定相等。
if (obj == null) return false; //如果显示参数为空,则返回false
if (getClass() !=obj.getClass()) return false; //用getClass()方法得到对象的类。如果几个类不匹配,则它们不相等
//其他对象是非空Employee类
//在以上判断完成,再将得到的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,然后再判断对象的属性是否相同
Employee other = (Employee) obj;
//测试字段是否具有相同的值
return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
}
@Override
public String toString() { //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)
// TODO Auto-generated method stub
return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
}
}
Manager类重写之后代码如下:
package equals;
public class Manager extends Employee //子类:Manager类继承Employee类
{
private double bonus; //创建私有属性bouns
public Manager(String name, double salary, int year, int month, int day)
{
super(name, salary, year, month, day); //子类直接调用超类中已创建的属性
bonus = 0; //给bouns赋初值为空
}
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() //重写hashCode方法,使相等的两个对象获取的HashCode也相等
{
return java.util.Objects.hash(super.hashCode(), bonus);
}
public String toString() //把其他类型的数据转为字符串类型的数据(toString方法可以自动生成)
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
EmployeeTest类代码如下:
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)
{
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:测试程序2(15分)
5-11程序代码如下:
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)
{
// fill the staff array list with three Employee objects 用三个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));
// raise everyone's salary by 5% 将每个人的薪水提高5%
for (Employee e : staff)
e.raiseSalary(5);
// print out information about all Employee objects 打印出所有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;
}//定义两个局部变量
}
程序运行结果如下:

设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法。
package arrayList;
import java.util.*;
/**
* This program demonstrates the ArrayList class.
* @version 1.11 2012-01-26
* @author Cay Horstmann
*/
public class ArrayListTest
{
private static final Employee element = null;
private static final int index = 0;
public static void main(String[] args)
{
// fill the staff array list with three Employee objects
ArrayList<Employee> staff = new ArrayList<Employee>(); //用三个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));
ArrayList<Employee> list = new ArrayList<Employee>();
//size()的用法
int size=staff.size();
System.out.println("arrayList中的元素个数是:"+size);
for(int i=0;i<staff.size();i++)
{
//get()的用法
Employee e=staff.get(i);
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
+ e.getHireDay());
}
//set()的用法
staff.set(0, new Employee("llx", 20000, 1999, 11, 06));
Employee e=staff.get(0);
System.out.println("修改后的数据为:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
+ e.getHireDay());
//remove()的用法
staff.remove(2);
System.out.println("将第一个数据删除后:");
int size1=staff.size();
System.out.println("arrayList中的元素个数是:"+size1);
for(int i=0;i<staff.size();i++)
{
Employee p=staff.get(i);
System.out.println("name=" + p.getName() + ",salary=" + p.getSalary() + ",hireDay="
+ p.getHireDay());
}
// raise everyone's salary by 5%
for (Employee e1 : staff) //把每个人的薪资提高%5
e1.raiseSalary(5);
// print out information about all Employee objects
for (Employee e1 : staff) //输出所有雇员对象的信息
System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay="
+ e1.getHireDay()); //利用getName(),getSalary() 和getHireDay()方法输出所有雇员对象的信息
}
}
程序运行结果如图:

实验2:测试程序3(15分)
程序代码如下:
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枚举类,背录删除代码,在代码录入中掌握枚举类的定义要求。
所有的枚举类型都是Enum类地子类,他们继承了这个类的许多方法,其中最有用的是toString,这个方法能够返回枚举常量名。每个枚举类型都有一个静态的values方法,它将返回一个包含全部枚举值的数组。ordinal方法返回enum声明中枚举常量的位置,位置从0开始计数。
删除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;
}//调用构造函数
程序运行结果如下:

实验2:测试程序4(5分)
程序代码如下:
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:编程练习(10分)
程序代码如下:
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 {
//补全本类定义
Son() {
super(true); //调用父类有参构造
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
super.method();
}
}
程序运行结果如下:

3. 实验总结:(15分)
通过学习继承这一章的内容以及实验,掌握了继承建立类与类间的is-a关系,意味着父类对象变量可以引用子类对象,在Java中,一个父类可以有多个子类,但一个子类只能有一个父类,子类通过extends关键字来继承父类,父类和子类的三种继承方式:1.public继承,(public成员所有的类都可以访问);2.private继承,(private成员只有本类能访问);3.protect继承,(protect成员只有本类和子类能访问)。子类继承父类是对父类属性和方法的全面继承,同时子类在继承了父类的方法后可对父类的方法进行重写。父类用作对象的声明类型,构造器用子类的构造器。object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来的,如果没有明确的指出超类,object就被认为是这个类的超类,