曹玉中-201871010105《面向对象程序设计(java)》第6-7周学习总结
项目 | 内容 |
这个作业属于哪个课程 | <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 | <作业链接地址>https://www.cnblogs.com/nwnu-daizh/p/11605051.html |
作业学习目标 |
|
一:理论部分:
第五章:继承类。
1.继承:已有类来构建新类的一种机制。档定义了一个新类继承另一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新的情况。
继承的特点:具有层次结构;子类继承了超类的方法和域。
2.类继承的格式:class 新类名 extends 已有类名
已有类称为:超类(superclass)、基类(base class) 或父类(parent class)
新类称作:子类(subclass)、派生类(derived class)或孩子类(child class)
1)继承类中,子类的构造器不能直接访问超类的私有域,必须调用超类构造器,且必须是第一条语句。
2)子类不能直接访问超类的私有域,必须和其他方法一样使用公有接口。
3)子类中可以增加域、增加方法或覆盖(override)超类的方法,但是绝不能删除超类的任何域和方法。
注:java不支持多继承。
3.多态性:泛指在程序中同一个符号在不同情况下具有不同解释。
超类方法在子类中可以重写。
java中,对象变量是多态的。
继承层次结构中,每个子类对象也可视作超类对象,因此,也可以将子类对象赋给超类变量。如:boss.setBouns(5000);
4:final类:不允许继承的类,在类的定义中用final修饰符加以说明。
5:强制转换类型:如果要把一个超类对象赋给一个子类对象变量,就必须进行强制类型转换,格式如下:Manager boss=(Manager)staff【0】;
6.抽象类:abstract class Person
1)abstract方法不能被实例化,即只能声明,不能实现!
2)包含一个或多个抽象方法的类本身必须被声明为抽象类。
7:Object类:所有类的超类。
8:equals方法:用于测试某个对象是否和另一个对象相等。如果两个对象有相同的引用,则他们一定相等。
9:hasCode方法:导出某个对象的散列码(任意整数)。
两个相等对象的散列码相等。
10:toString方法:返回一个代表该对象域值的字符串。
格式:类名【域值】
11:枚举类:它包括一个关键字enum,一个新枚举类型的名字 Grade以及为Grade定义的一组值,这里的值既非整型,亦非字符型。
1)枚举类是一个类,它的隐含超类是java.lang.Enum。
2)枚举值并不是整数或其它类型,是被声明的枚举类的自身实例
二:实验部分。
1、实验目的与要求
(1) 理解继承的定义;
(2) 掌握子类的定义要求
(3) 掌握多态性的概念及用法;
(4) 掌握抽象类的定义及用途;
(5) 掌握类中4个成员访问权限修饰符的用途;
(6) 掌握抽象类的定义方法及用途;
(7)掌握Object类的用途及常用API;
(8) 掌握ArrayList类的定义方法及用法;
(9) 掌握枚举类定义方法及用途。
2、实验内容和步骤
实验1: 导入第5章示例程序,测试并进行代码注释。
测试程序1:
在elipse IDE中编辑、调试、运行程序5-1 (教材152页-153页) ;
掌握子类的定义及用法;
结合程序运行结果,理解并总结OO风格程序构造特点,理解Employee和Manager类的关系子类的用途,并在代码中添加注释。
5-1程序如下:
ackage inheritance;
/**
* This program demonstrates inheritance.
* @version 1.21 2004-02-21
* @author Cay Horstmann
*/
public class ManagerTest//主类:创建一个Manager类
{
public static void main(String[] args)
{
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);//bouns在Manager类中赋初值为空,此处用更改器更改为5000
Employee[] staff = new Employee[3];//用三个Employee类填充Staff数组
staff[0] = boss;//已定义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类,所以需新建Employee类,5-2程序如下:
ackage 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;//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;
}//定义两个局部变量
}
知识兔在Employee类中,有Manager类,而Manager类中部分属性在Employee中已定义,故使用继承来新建Manager类,5-3程序如下:
ackage inheritance;
public class Manager extends Employee//子类:Manager类继承Employee类
{
private double bonus;//创建一个私有属性bouns
/**
* @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)//构造器
{
super(name, salary, year, month, day);//子类不用再重新定义,直接调用超类中已定义的属性
bonus = 0;//bouns的初值赋为空
}
public double getSalary()//访问器
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}//定义两个局部变量
public void setBonus(double b)//更改器
{
bonus = b;
}
}
知识兔程序运行结果如图所示:
测试程序2:
编辑、编译、调试运行教材PersonTest程序(教材163页-165页);
掌握超类的定义及其使用要求;
掌握利用超类扩展子类的要求;
在程序中相关代码处添加新知识的注释;
删除程序中Person类、PersonTest类,背录删除类的程序代码,在代码录入中理解抽象类与子类的关系和使用特点。
5-4代码如下:
ackage 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[] people = new Person[2];
//用Employee类和Student类填充people数组
people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");
//打印出所有person类的名字和其他描述
for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
}
}
知识兔因主类中有Person类,而Person类是一个抽象类,故需新建一个抽象类Person类,5-5程序如下:
ackage abstractClasses;
public abstract class Person//抽象类:Person
{
public abstract String getDescription();
private String name;//传建一个私有属性
public Person(String name)//构造器
{
this.name = name;
}
public String getName()//访问器
{
return name;
}
}
知识兔在主类中,Employee类和student类都是person类的子类,所以新建两个子类,5-6程序如下:
Student类:
ackage abstractClasses;
public class Student extends Person//子类:Student类继承Person类
{
private String major;//创建一个私有属性major
/**
* @param nama the student's name
* @param major the student's major
*/
public Student(String name, String major)//构造器
{
//将n传递给超类构造函数
super(name);//子类直接调用超类中的name属性
this.major = major;
}
public String getDescription()//访问器
{
return "a student majoring in " + major;
}
}
知识兔Employee类,5-7代码如下:
ackage abstractClasses;
import java.time.*;
public class Employee extends Person//子类:Employee类继承Person类
{
private double salary;
private LocalDate hireDay;
//构建两个私有属性
public Employee(String name, double salary, int year, int month, int day)//构造器
{
super(name);//子类中不再重新定义,直接调用超类中的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类的定义及用法;
在程序中相关代码处添加新知识的注释。
5-8程序如下:
ackage 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);//在子类中赋初值为空,主类中用更改器更改为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,5-9代码如下:
ackage 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;
//其他对象是非空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,5-10代码如下:
ackage 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()//把其他类型的数据转为字符串类型的数据
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
知识兔运行结果如下:
实验2:编程练习
定义抽象类Shape:
属性:不可变常量double PI,值为3.14;
方法:public double getPerimeter();public double getArea())。
让Rectangle与Circle继承自Shape类。
编写double sumAllArea方法输出形状数组中的面积和和double sumAllPerimeter方法输出形状数组中的周长和。
main方法中
1)输入整型值n,然后建立n个不同的形状。如果输入rect,则再输入长和宽。如果输入cir,则再输入半径。
2) 然后输出所有的形状的周长之和,面积之和。并将所有的形状信息以样例的格式输出。
3) 最后输出每个形状的类型与父类型,使用类似shape.getClass()(获得类型),shape.getClass().getSuperclass()(获得父类型);
思考sumAllArea和sumAllPerimeter方法放在哪个类中更合适?
输入样例:
输出样例:
程序代码如下:
主类Work:
mport java.util.Scanner;
public class Work
{
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[] count = new shape[n];
for(int i=0;i<n;i++)
{
System.out.println("请输入图形形状:");
String input = in.next();
if(input.equals(rect))
{
double length = in.nextDouble();
double width = in.nextDouble();
System.out.println("长方形:"+"长:"+length+" 宽:"+width);
count[i] = new Rect(length,width);
}
if(input.equals(cir))
{
double radius = in.nextDouble();
System.out.println("圆:"+"半径:"+radius);
count[i] = new Cir(radius);
}
}
Work c = new Work();
System.out.println(c.sumAllPerimeter(count));
System.out.println(c.sumAllArea(count));
for(shape s:count)
{
System.out.println(s.getClass()+", "+s.getClass().getSuperclass());
}
}
public double sumAllArea(shape count[])
{
double sum = 0;
for(int i = 0;i<count.length;i++)
sum+= count[i].getArea();
return sum;
}
public double sumAllPerimeter(shape count[])
{
double sum = 0;
for(int i = 0;i<count.length;i++)
sum+= count[i].getPerimeter();
return sum;
}
}
知识兔抽象类Shape:
ublic abstract class shape
{
double PI = 3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
知识兔子类Rect:
ublic class Rect extends shape
{
private double width;
private double length;
public Rect(double w,double l)
{
this.width = w;
this.length = l;
}
public double getPerimeter()
{
double Perimeter = 2*(length+width);
return Perimeter;
}
public double getArea()
{
double Area = length*width;
return Area;
}
}
知识兔子类Cir:
ublic class Cir extends shape
{
private double radius;
public Cir(double radius2) {
// TODO Auto-generated constructor stub
}
public double getPerimeter()
{
double Perimeter=2*PI*radius;
return Perimeter;
}
public double getArea()
{
double Area=PI*radius*radius;
return Area;
}
}
知识兔程序运行结果如下:
实验总结:
通过前一周的学习,我初步掌握了有关继承类的知识。在编写程序时,使用继承类更能简化程序。通过课堂学习和课后运行程序,了解了子类和父类的关系。另外,学习了抽象类的用法,在运行程序的过程中,加深了对它的理解。在完成最后两个实验时,自己通过查书查资料,并仿照老师给的源程序,写出了大概的程序,尤其是第二个程序,和之前的studentfile有相似之处。但在细节上,程序总是会有错误,最后通过请教同学完成了本次实验。通过这次实验,我深深认识到,很多知识一定要看书,并在课后多敲代码练习。对于很多自己还不理解的知识,自己定会加强学习,希望之后的实验中,问题能够减少。