枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁,安全性以及便捷性。创建枚举类型要使用enum关键字,隐含了所创建的类型都是java.lang.Enum类的子类(java.lang.Enum是一个抽象类)。枚举类型符合通用模式Class Enum<E extends Enum<E>>,而E表示枚举类型的名称。枚举类型的每一个值都映射到protected Enum(String name,int ordinal)构造函数中,在这里,每个值的名称都转换成一个字符串,并且序数设置表示了此设置被创建的顺序。

 

类、超类和子类;

         类继承的格式: class 新类名extends已有类名。继承的特点:具有结构层次;子类继承了父类的域和方法。已有类称为:超类(superclass)、基类(base class) 或父类(parent  class)新类称作:子类(subclass)、派生类(derived  class)或孩子类(child class)super是一个指示编译器调用超类方法的特有关键字,它不是一个对象的引用,不能将super赋给另一个对象变量从一个超类扩展而来的类集合称为继承层次。在继承层次中,某个类到其祖先的路径被称为该类的继承链。注:Java不支持多继承。

多态性:多态性泛指在程序中同一个符号在不同的情况 下具有不同解释的现象。不允许继承的类称为final类,在类的定义中用final修饰符加以说明

抽象类:抽象方法充当着占位的角色,它们的具体实现在子类中。抽象类不能被实例化,即不能创建对象,只能产生子类。

Object:所有类的超类;

       Object类是Java中所有类的祖先——每一个类都由它扩展而来。在不给出超类的情况下,Java会自动把Object 作为要定义类的超类。可以使用类型为Object的变量指向任意类型的对象。但要对它们进行专门的操作都要进行类型转换。Object类中的equals方法用于测试某个对象是否同另一个对象相等。它在Object类中的实现是判断两个对象是否具有相同的引用。如果两个对象具有相同的引用,它们一定是相等的。Object类中的hashCode方法导出某个对象的散列码。散列码是任意整数,表示对象的存储地址。

两个相等对象的散列码相等。

泛型数组列表;

        ArrayList类,可允许程序在运行时确定数组的大小。.ArryList是一个采用类型参数的泛型类。为指定数组列表保存元素的对象类型,需要用一对尖括号将数组元素对象类名括起来加在后面。ArryList<Employee> staff=new ArrayList<Employee>();

对象包装器和自动打包;

       所有基本数据类型都有着与之对应的预定义类,它们被称为对象包装器。

       对象包装器类是不可变的,即一旦构造了包装器,就不允更改包装在其中的值。且对象包装器类还是final,因此不能定义它们的子类。在JavaSE5.0中,可以自动的将基本数据类型转换为包装器类的对象,将这种变换称为自动打包

参数数量可变的方法;

      在Java SE 5.0以前的版本中,每个Java方法都有固定数量的参数。然而,现在的版本提供了可以用可变的参数数量调用的方法(称为“可变参 ”方法)。

用户自己可以定义可变参数的方法,并将参数指定为任意类型,甚至是基本类型。

枚举类;

       声明枚举类publicenumGrade{A,B,C,D,E};它包括一个关键字enum,一个新枚举类型的名字 Grade以及为Grade定义的一组值,这里的值既非整型,亦非字符型枚举类是一个类,它的隐含超类是java.lang.Enum。枚举值并不是整数或其它类型,是被声明的枚举类的自身实例

继承设计的技巧。

继承的特点:具有结构层次;子类继承了父类的域和方法。

       将公共操作和域放在超类。不要使用受保护的域。使用继承实现“is-a”关系。除非所有继承的方法都有意义,否则就不要使用继承。在覆盖方法时,不要改变预期的行为。使用多态,而非类型信息。过多地使用反射。

第二部分:实验部分

 

实验六继承定义与使用

1、实验目的与要求

 

(1) 理解继承的定义;

 

(2) 掌握子类的定义要求

 

(3) 掌握多态性的概念及用法;

 

(4) 掌握抽象类的定义及用途;

 

(5) 掌握类中4个成员访问权限修饰符的用途;

 

(6) 掌握抽象类的定义方法及用途;

 

(7)掌握Object类的用途及常用API;

 

(8) 掌握ArrayList类的定义方法及用法;

 

(9) 掌握枚举类定义方法及用途。

 

2、实验内容和步骤

实验1: 导入第5章示例程序,测试并进行代码注释。

测试程序1:

1)在elipse IDE中编辑、调试、运行程序5-1 (教材152页-153页) ;

2)掌握子类的定义及用法;

3)结合程序运行结果,理解并总结OO风格程序构造特点,理解Employee和Manager类的关系子类的用途,并在代码中添加注释。

package inheritance;

/**
* This program demonstrates inheritance.
* @version 1.21 2004-02-21
* @author Cay Horstmann
*/
public class ManagerTest
{
public static void main(String[] args)
{
// 构建管理者对象
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);

Employee[] staff = new Employee[3];

// 用管理者和雇员对象填充工作人员数组

staff[0] = boss;
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

// 打印所有员工对象的信息
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}

employee类
package inheritance;

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

Manager类
package inheritance;
//关键字extends表示继承。表明正在构造一个新类派生于一个已经存在的类。已经存在的类称为超类/基类/或者父类;新类称为子类/派生类/或者孩子类。
public class Manager extends Employee
{
private double bonus;

/**
* @param name the employee's name
* @param salary the salary
* @param year the hire year
* @param month the hire month
* @param day the hire day
*/
public Manager(String name, double salary, int year, int month, int day)
{//下面这句的意思是调用超类中含有n,s,year,month,day参数的构造器。
super(name, salary, year, month, day);
bonus = 0;
}

public double getSalary()
{ //子类要想访问要想访问超类中的方法需要使用特定的关键字super,
//super调用构造器的语句必须是子类构造器的第一条语句。
double baseSalary = super.getSalary();
return baseSalary + bonus;
}

public void setBonus(double b)
{
bonus = b;
}
}

测试程序2:

1)编辑、编译、调试运行教材PersonTest程序(教材163页-165页);

2)掌握超类的定义及其使用要求;

3)掌握利用超类扩展子类的要求;

4)在程序中相关代码处添加新知识的注释。

 程序5-4如下:

package abstractClasses;

/**
* This program demonstrates abstract classes.
* @version 1.01 2004-02-21
* @author Cay Horstmann
*/
public class PersonTest
{
public static void main(String[] args)
{
// 抽象类的声明,但不能将抽象类实例化 ,实例化的是Person类的子类
// 父类引用可以引用,子类对象,因为父类引用的方法不会超过子类对象的方法,但反之不行
Person[] people = new Person[2];

// 用学生和雇员填充人物数组
people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");

// 打印所有人对象的名称和描述
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
}
}

student类
package abstractClasses;

public class Student extends Person
{
private String major;

/**
* @param nama the student's name
* @param major the student's major
*/
public Student(String name, String major)
{
// 通过n to 总纲构造函数
super(name);
this.major = major;
}

public String getDescription()
{
return "a student majoring in " + major;
}
}

person类
package abstractClasses;

public abstract class Person
{
//包含一个或多个抽象方法的类被称为抽象类,由abstract关键字修饰
//通用的作用域和方法也放到了这里,抽象类不能被实例化,但可以被声明
public abstract String getDescription();
private String name;

public Person(String name)
{
this.name = name;
}

public String getName()
{
return name;
}
}

employee类
package abstractClasses;

import java.time.*;

public class Employee extends Person
{
private double salary;
private LocalDate hireDay;

public Employee(String name, double salary, int year, int month, int day)
{
super(name);
this.salary = salary;
hireDay = LocalDate.of(year, month, day);
}

public double getSalary()
{
return salary;
}

public LocalDate getHireDay()
{
return hireDay;
}

//重写父类方法,返回一个格式化的字符串
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}

public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}

程序运行结果如下:

 

 

测试程序3:

编辑、编译、调试运行教材程序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);//在子类中赋初值为空,主类中用更改器更改为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:

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;

//其他对象是非空Employee类
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
+ "]";
}
}

子类Manager:

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()//把其他类型的数据转为字符串类型的数据
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
运行结果如图所示:

测试程序4:

 在elipse IDE中调试运行程序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());
}
}

Employee类:

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;
}//定义两个局部变量
}

程序运行结果如下:

测试程序5:

1)编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;

2)掌握枚举类的定义及用法;

3)在程序中相关代码处添加新知识的注释。

程序5-12如下:

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

程序运行结果如下:

实验2:编程练习1

1)定义抽象类Shape:

属性:不可变常量double PI,值为3.14;

方法:public double getPerimeter();public double getArea())。

2)让Rectangle与Circle继承自Shape类。

3)编写double sumAllArea方法输出形状数组中的面积和和double sumAllPerimeter方法输出形状数组中的周长和。

main方法中

1)输入整型值n,然后建立n个不同的形状。如果输入rect,则再输入长和宽。如果输入cir,则再输入半径。
2) 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。
3) 最后输出每个形状的类型与父类型,使用类似shape.getClass()(获得类型),shape.getClass().getSuperclass()(获得父类型);

思考sumAllArea和sumAllPerimeter方法放在哪个类中更合适?

输入
rect
rect
cir
输出
18.28
8.14
[Rectangle [width=1, length=1], Rectangle [width=2, length=2], Circle [radius=1]]
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape

package shape;

import java.math.*;
import java.util.*;
import shape.shape;
import shape.Rectangle;
import shape.Circle;

public class shapecount
{

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String rect = "rect";
String cir = "cir";
System.out.print("请输入形状个数:");
int n = in.nextInt();
shape[] score = new shape[n];
for(int i=0;i<n;i++)
{
System.out.println("请输入形状类型 (rect or cir):");
String input = in.next();
if(input.equals(rect))
{
double length = in.nextDouble();
double width = in.nextDouble();
System.out.println("Rectangle["+"length:"+length+" width:"+width+"]");
score[i] = new Rectangle(width,length);
}
if(input.equals(cir))
{
double radius = in.nextDouble();
System.out.println("Circle["+"radius:"+radius+"]");
score[i] = new Circle(radius);
}
}
shapecount c = new shapecount();
System.out.println(c.sumAllPerimeter(score));
System.out.println(c.sumAllArea(score));
for(shape s:score)
{

System.out.println(s.getClass()+", "+s.getClass().getSuperclass());
}
}

public double sumAllArea(shape score[])
{
double sum = 0;
for(int i = 0;i<score.length;i++)
sum+= score[i].getArea();
return sum;
}

public double sumAllPerimeter(shape score[])
{
double sum = 0;
for(int i = 0;i<score.length;i++)
sum+= score[i].getPerimeter();
return sum;
}

}

package shape;

public abstract class shape
{
double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}


package shape;

public class Rectangle extends shape
{
private double width;
private double length;
public Rectangle(double w,double l)
{
this.width = w;
this.length = l;
}
public double getPerimeter()
{
double Perimeter = (width+length)*2;
return Perimeter;
}
public double getArea()
{
double Area = width*length;
return Area;
}

public String toString()
{
return getClass().getName() + "[ width=" + width + "]"+ "[length=" + length + "]";
}
}

 

package shape;

public class Circle extends shape
{

private double radius;
public Circle(double r)
{
radius = r;
}
public double getPerimeter()
{
double Perimeter = 2*PI*radius;
return Perimeter;
}
public double getArea()
{
double Area = PI*radius*radius;
return Area;
}
public String toString()
{
return getClass().getName() + "[radius=" + radius + "]";
}
}

实验3: 编程练习2

编制一个程序,将身份证号.txt 中的信息读入到内存中,输入一个身份证号或姓名,查询显示查询对象的姓名、身份证号、年龄、性别和出生地。

程序代码如下:

主类Test:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Test{
private static ArrayList<Person> Personlist;
public static void main(String[] args) {
Personlist = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
File file = new File("E:\\WPS Office\\身份证号.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String temp = null;
while ((temp = in.readLine()) != null) {

Scanner linescanner = new Scanner(temp);

linescanner.useDelimiter(" ");
String name = linescanner.next();
String ID = linescanner.next();
String sex = linescanner.next();
String age = linescanner.next();
String birthplace =linescanner.nextLine();
Person Person = new Person();
Person.setname(name);
Person.setID(ID);
Person.setsex(sex);
Person.setage(age);
Person.setbirthplace(birthplace);
Personlist.add(Person);

}
} catch (FileNotFoundException e) {
System.out.println("查找不到信息");
e.printStackTrace();
} catch (IOException e) {
System.out.println("信息读取有误");
e.printStackTrace();
}
boolean isTrue = true;
while (isTrue) {

System.out.println("1.按姓名查找");
System.out.println("2.按身份证号查找");
System.out.println("3.退出");
int nextInt = scanner.nextInt();
switch (nextInt) {
case 1:
System.out.println("请输入姓名:");
String Personname = scanner.next();
int nameint = findPersonByname(Personname);
if (nameint != -1) {
System.out.println("查找信息为:姓名:"
+ Personlist.get(nameint).getname() + " 身份证号:"
+ Personlist.get(nameint).getID() +" 年龄:"
+Personlist.get(nameint).getage() +" 性别:"
+Personlist.get(nameint).getsex()+" 出生地:"
+Personlist.get(nameint).getbirthplace()
);
} else {
System.out.println("此人不存在!");
}
break;
case 2:
System.out.println("请输入身份证号");
String PersonID = scanner.next();
int IDint = findPersonByID(PersonID);
if (IDint != -1) {
System.out.println("查找信息为:姓名:"
+ Personlist.get(IDint).getname() + " 身份证号:"
+ Personlist.get(IDint).getID() +" 年龄:"
+Personlist.get(IDint).getage() +" 性别:"
+Personlist.get(IDint).getsex()+" 出生地:"
+Personlist.get(IDint).getbirthplace()
);
} else {
System.out.println("此人不存在!");
}
break;
case 3:
isTrue = false;
System.out.println("退出程序!");
break;
default:
System.out.println("输入有误");
}
}
}

private static int findPersonByID(String personID) {
// TODO Auto-generated method stub
return 0;
}

private static int findPersonByname(String personname) {

return 0;
}

public static int findStudentByname(String name) {
int flag = -1;
int a[];
for (int i = 0; i < Personlist.size(); i++) {
if (Personlist.get(i).getname().equals(name)) {
flag= i;
}
}
return flag;
}

public static int findStudentByID(String ID) {
int flag = -1;

for (int i = 0; i < Personlist.size(); i++) {
if (Personlist.get(i).getID().equals(ID)) {
flag = i;
}
}
return flag;
}
}

由于在此程序中使用了ArrayList<Person> PersonList,所以需要新建一个Person类,程序如下所示

public class Person {
private String name;
private String ID;
private String age;
private String sex;
private String birthplace;

public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID= ID;
}
public String getage() {
return age;
}
public void setage(String age) {
this.age= age;
}
public String getsex() {
return sex;
}
public void setsex(String sex) {
this.sex= sex;
}
public String getbirthplace() {
return birthplace;
}
public void setbirthplace(String birthplace) {
this.birthplace= birthplace;
}

}

程序运行结果如图:

实验总结:

通过这几天放假的学习,学习到了许多新的知识,也掌握了继承方面的的一些知识。另外,学习了抽象类的用法,在运行程序的过程中,加深了对它的理解。在完成最后两个实验时,自己通过查书查资料,并仿照老师给的源程序,写出了大概的程序,尤其是第二个程序,和之前的studentfile有相似之处。自己在学习方面主动性不够,还需要更加努力。作业的延迟感到抱歉。