《面向对象程序设计(java)》第五周学习总结
第一部分:课程准备部分
第二部分:理论知识学习部分
第四章:对象与类
4.1:类与对象的概念。
类:是构造对象的模板或蓝图。由类构造对象的过程称为创建类的实例。
对象:想要使用oop,一定要清楚对象的三个特性:
1)对象的行为:对象的行为使用可调用的方法定义的。
·2)对象的状态:每个对象都保存着描述当前特征的信息。
3)对象标识:如何辨明具有相同行为的相似性。
4.2:类之间的关系。
常见关系有:依赖、聚合、继承。
4.3:使用预定义类。
已学过的预定义类如math,Math,String,Scanner等。
1)对象与对象变量。
在Java语言中,使用构造器构造新实例。构造器是类中一个特殊的方法,生成并初始化对象,它的方法名与类名相同。
想要构造一个Data对象(定义在java.util中),需要在构造器前加上new操作符:new Data()
Data deadline;该语句错误。
可将一个对象变量设置为null,表示该对象变量未引用任何变量,如deadlin=null。
4.4:更改器与访问器。
更改器:修改实例域。前缀set,更改当前类中的属性。
访问器:更改实例域。前缀get。
第三部分:实验部分
1.实验名称:实验五 类与对象附加实验
2. 实验目的:
(1) 掌握用户自定义类的定义,理解类内构造函数、访问器、更改器等方法用途;
(2) 掌握对象的声明与创建;
(3) 掌握对象使用方法;学会使用初始化对象;
(4) 掌握package和import语句的用途;
(5)能够利用自定义类设计包含两个类的OO应用程序;
(6)综合本章知识,理解面向对象程序设计的封装特征,并体会其优点。
3. 实验步骤与内容:
实验1 示例程序代码添加注释示范。
import java.time.*; public class CalendarTestNote { public static void main(String[] args) {
LocalDate date = LocalDate.now();//获得当前日期 int month = date.getMonthValue();//获得当期日期的月份 int today = date.getDayOfMonth();//获得当期日期的天 System.out.println("today="+today); date = date.minusDays(today - 1); // 更改当前日期为月初日期 DayOfWeek weekday = date.getDayOfWeek();//获得月初日期的起点星期 int value = weekday.getValue(); // 1 = Monday, ... 7 = Sunday,获得起点星期的整数编号 System.out.println("Mon Tue Wed Thu Fri Sat Sun"); for (int i = 1; i < value; i++) System.out.print(" "); //输出空格占用本月初日期所在周中上月天数的位置 while (date.getMonthValue() == month)//本月天数的计算控制条件 { System.out.printf("%3d", date.getDayOfMonth()); //输出日期对象date当前天 if (date.getDayOfMonth() == today)//如本月天数编号是当日,输出“*”标记 System.out.print("*"); else System.out.print(" ");//输出天分割标记 date = date.plusDays(1); //日期对象date月天数编号加1 if (date.getDayOfWeek().getValue() == 1) System.out.println();//一周的天数已输出,则换行,准备输出本月下一周的天数 } if (date.getDayOfWeek().getValue() != 1) System.out.println();// 本月月历输出完毕,则换行 } } |
实验2 第四章测试程序反思,对象与类知识总结。
测试程序1:
编辑、编译、调试运行程序4-2(教材104页);
结合程序运行结果,掌握类的定义与类对象的用法;(4.3节)
尝试在项目中编辑两个类文件(Employee.java、 EmployeeTest.java ),编译并运行程序。
(1)
import java.time.*; /** * This program tests the Employee class. * @version 1.12 2015-05-08 * @author Cay Horstmann */ public class EmployeeTest { public static void main(String[] args) { // fill the staff array with three Employee objects Employee[] staff = new Employee[3];// 用三个Employee对象填充staff数组 staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);//创建用户自定义类 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); // raise everyone's salary by 5% for (Employee e : staff) e.raiseSalary(5);//涨工资5% // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } } class Employee { private String name;//name值是私有的,只能在类内使用 private double salary; private LocalDate hireDay; public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; 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; } } |
运行结果如下:
(2)
测试程序2:
编辑、编译、调试运行程序4-3(教材116);
结合程序运行结果,理解程序代码,掌握静态域(netxtId)与静态方法(getNextId)的用法;(4.4节)
理解Java单元(类)测试的技巧。
/**
* This program demonstrates static methods. * @version 1.01 2004-02-19 * @author Cay Horstmann */ public class StaticTest { public static void main(String[] args) { // fill the staff array with three Employee objects Employee[] staff = new Employee[3]; staff[0] = new Employee("Tom", 40000);
staff[1] = new Employee("Dick", 60000); staff[2] = new Employee("Harry", 65000); // print out information about all Employee objects
for (Employee e : staff) { e.setId(); System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary()); } int n = Employee.getNextId(); // calls static method
System.out.println("Next available id=" + n); } } class Employee
{ private static int nextId = 1; private String name;
private double salary; private int id; public Employee(String n, double s)
{ name = n; salary = s; id = 0; } public String getName()
{ return name; } public double getSalary()
{ return salary; } public int getId()
{ return id; } public void setId()
{ id = nextId; // set id to next available id nextId++; } public static int getNextId()
{ return nextId; // returns static field } public static void main(String[] args) // unit test
{ Employee e = new Employee("Harry", 50000); System.out.println(e.getName() + " " + e.getSalary()); } } |
运行结果如下:
测试程序3:
编辑、编译、调试运行程序4-4(教材121);
结合程序运行结果,理解程序代码,掌握Java方法参数的用法;(4.5节)
/**
* This program demonstrates parameter passing in Java. * @version 1.00 2000-01-27 * @author Cay Horstmann */ public class ParamTest { public static void main(String[] args) { /* * Test 1: Methods can't modify numeric parameters */ System.out.println("Testing tripleValue:"); double percent = 10; System.out.println("Before: percent=" + percent); tripleValue(percent); System.out.println("After: percent=" + percent); /*
* Test 2: Methods can change the state of object parameters */ System.out.println(" Testing tripleSalary:"); Employee harry = new Employee("Harry", 50000); System.out.println("Before: salary=" + harry.getSalary()); tripleSalary(harry); System.out.println("After: salary=" + harry.getSalary()); /*
* Test 3: Methods can't attach new objects to object parameters */ System.out.println(" Testing swap:"); Employee a = new Employee("Alice", 70000); Employee b = new Employee("Bob", 60000); System.out.println("Before: a=" + a.getName()); System.out.println("Before: b=" + b.getName()); swap(a, b); System.out.println("After: a=" + a.getName()); System.out.println("After: b=" + b.getName()); } public static void tripleValue(double x) // doesn't work
{ x = 3 * x; System.out.println("End of method: x=" + x); } public static void tripleSalary(Employee x) // works
{ x.raiseSalary(200); System.out.println("End of method: salary=" + x.getSalary()); } public static void swap(Employee x, Employee y)
{ Employee temp = x; x = y; y = temp; System.out.println("End of method: x=" + x.getName()); System.out.println("End of method: y=" + y.getName()); } } class Employee // simplified Employee class
{ private String name; private double salary; public Employee(String n, double s)
{ name = n; salary = s; } public String getName()
{ return name; } public double getSalary()
{ return salary; } public void raiseSalary(double byPercent)
{ double raise = salary * byPercent / 100; salary += raise; } } |
运行结果如下:
测试程序4:
编辑、编译、调试运行程序4-5(教材129);
结合程序运行结果,理解程序代码,掌握Java用户自定义类的用法,掌握对象构造方法及对象使用方法。(4.6节)
import java.util.*;
/**
* This program demonstrates object construction. * @version 1.01 2004-02-19 * @author Cay Horstmann */ public class ConstructorTest { public static void main(String[] args) { // fill the staff array with three Employee objects Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000); staff[2] = new Employee(); // print out information about all Employee objects
for (Employee e : staff) System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary()); } } class Employee
{ private static int nextId; private int id;
private String name = ""; // instance field initialization private double salary; // static initialization block static { Random generator = new Random(); // set nextId to a random number between 0 and 9999 nextId = generator.nextInt(10000); } // object initialization block
{ id = nextId; nextId++; } // three overloaded constructors
public Employee(String n, double s) { name = n; salary = s; } public Employee(double s)
{ // calls the Employee(String, double) constructor this("Employee #" + nextId, s); } // the default constructor
public Employee() { // name initialized to ""--see above // salary not explicitly set--initialized to 0 // id initialized in initialization block } public String getName()
{ return name; } public double getSalary()
{ return salary; } public int getId()
{ return id; } } |
测试程序5:
编辑、编译、调试运行程序4-6、4-7(教材135);
结合程序运行结果,理解程序代码,掌握Java包的定义及用法;(4.7节)
import com.horstmann.corejava.*;
// the Employee class is defined in that package import static java.lang.System.*;
/**
* This program demonstrates the use of packages. * @version 1.11 2004-02-19 * @author Cay Horstmann */ public class PackageTest { public static void main(String[] args) { // because of the import statement, we don't have to use // com.horstmann.corejava.Employee here Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); harry.raiseSalary(5);
// because of the static import statement, we don't have to use System.out here
out.println("name=" + harry.getName() + ",salary=" + harry.getSalary()); } } |
运行结果如下:
实验3 编程示范
编写长方形类Rectangle与圆形类Circle,其中Rectangle类设置私有属性:width,length;Circle类设置私有属性radius。编写Rectangle类的带参构造函数Rectangle(int width,int length), Circle类的带参构造函数Circle(int radius),编写两个类的toString方法(Eclipse可自动生成)。上述2个类均定义以下方法:
求周长的方法public int getPerimeter()
求面积的方法public int getArea()
在main方法中完成以下任务:
(1)输入1行长与宽,创建一个Rectangle对象;
(2)输入1行半径,创建一个Circle对象;
(3)将两个对象的周长加总输出,将两个对象的面积加总输出。
package Areas.java;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in); System.out.println("请输入矩形的长:"); int length=scan.nextInt(); System.out.println("请输入矩形的宽:"); int width=scan.nextInt(); Rectangle r=new Rectangle(width,length); System.out.println("请输入圆的半径:"); int radius=scan.nextInt(); Circle c=new Circle(radius); System.out.println("周长和:"+(r.getPerimeter(width, length)+c.getPerimeter(radius))); System.out.println("面积和:"+(r.getArea(width, length)+c.getArea(radius))); } }
|
运行结果如下:
4. 实验总结:
通过老师和助教学长的指导教学,在上次实验的基础上,我将两次实验进行了对比学习和反思,掌握了更多的技能和知识。