王之泰201771010131《面向对象程序设计(java)》第四周学习总结
王之泰201771010131《面向对象程序设计(java)》第四周学习总结
第一部分:理论知识学习部分
第四章
1.类与对象的基础概念。
a.类(class)是构造对象的模板或蓝图。由类构造对象的过程称为创建类的实例;
b.对象:即数据,对象有三个特性——1.行为 2.状态 3.标识。
2.类与对象的关系
a.类是对象,事物的描述和抽象,是具有相同属性和行为的对象集合。对象则是该类事物的实例。
b.类是一个静态的概念,类本身不携带任何数据。当没有为类创建任何对象时,类本身不存在于内存空间中。对象是一个动态的概念。每一个对象都存在着有别于其它对象的属于自己的独特的属性和行为。对象的属性可以随着它自己的行为而发生改变。
3.对象与对象变量的关系
a.java中想使用对象就必须先构造对象,并指定其初始状态。
4.通过实验掌握了预定义类的基本使用方法,熟悉math类、string类、math类、scanner类、localdate类的常用api。
5.掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求
a.实例域:可将实例域定义为final,构建对象时必须初始化这样的域。
b.静态域:绝大多数面向对象程序设计语言中,静态域被称为类域。如果将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域却都有自己的一份拷贝。
c.静态方法:静态方法是一种不能向对象实时操作的方法。可以使用对象调用静态方法。
d.构造器方法:构造器与类同名。构造器总是伴随着new操作符的执行被调用,而不能对一个已经存在的对象调用构造器来达到重新设置实例域的目的。
e.更改器方法:调用更改器方法后对象的状态会改变。
f.访问器方法:只访问对象而不修改对象的方法。
g.main方法:main方法不对任何对象进行操作。静态的main方法将执行并创建程序所需要的对象。
6.重载
多个方法有相同的名字、不同的参数、便产生了重载。java允许重载任何方法,而不只是构造器方法。
7.包
java允许使用包将类组织起来。借助包可以方便地组织自己的代码,并将自己的代码与别人提供的代码库分开管理。而且使用包可以确保类名的唯一性。
8.文档注释技术
a.类注释
b.方法注释
c.域注释
d.通用注释
e.包与概述注释
第二部分:实验部分
1、实验目的与要求
(1) 理解用户自定义类的定义;
(2) 掌握对象的声明;
(3) 学会使用构造函数初始化对象;
(4) 使用类属性与方法的使用掌握使用;
(5) 掌握package和import语句的用途。
2、实验内容和步骤
实验1 测试以下程序,掌握文件输入输出程序设计技术(文件输入输出,教材61-62)
示例代码:
1 import java.io.*; 2 import java.util.*; 3 public class filewritereadtest { 4 public static void main(string[] args) throws ioexception{ 5 //写入文件演示 6 printwriter out = new printwriter("myfile.txt"); 7 out.println("姓名 高数 java 数据结构 平均成绩 总成绩"); 8 out.println("张三 20 30 40 0 0"); 9 out.println("李四 50 60 70 0 0"); 10 out.close();//输出完毕,需要close 11 //读入文件演示 12 scanner in = new scanner(new file("myfile.txt"));//为myfile.txt这个file创建一个扫描器in 13 int number = 1;//行号 14 system.out.println(in.nextline()); 15 while(in.hasnextline()){//判断扫描器是否还有下一行未读取,该循环把文件的每一行都读出 16 string line = in.nextline();//读出myfile.txt的下一行 17 system.out.print("第"+(++number)+"行的内容: "); 18 scanner linescanner = new scanner(line);//行内容建立扫描器 19 linescanner.usedelimiter(" ");//使用空格作为分隔符 20 string name = linescanner.next(); 21 string math = linescanner.next(); 22 string java = linescanner.next(); 23 string ds = linescanner.next(); 24 string avg = linescanner.next(); 25 string total = linescanner.next(); 26 system.out.println("name="+name+" math="+math+" java="+java+" ds="+ds+" avg"+avg+" total="+total); 27 } 28 in.close();//读入完毕,最后需要对其进行close。 29 } 30 }
文件输出结果如下:
实验2 导入第4章示例程序并测试。
测试程序1:
编辑、编译、调试运行程序4-2(教材104页);
结合程序运行结果,掌握类的定义与类对象的用法,并在程序代码中添加类与对象知识应用的注释;
尝试在项目中编辑两个类文件(employee.java、 employeetest.java ),编译并运行程序。
程序4-2如下:
1 import java.time.*; 2 3 /** 4 * this program tests the employee class. 5 * @version 1.12 2015-05-08 6 * @author cay horstmann 7 */ 8 public class employeetest 9 { 10 public static void main(string[] args) 11 { 12 // fill the staff array with three employee objects 13 employee[] staff = new employee[3]; 14 15 staff[0] = new employee("carl cracker", 75000, 1987, 12, 15); 16 staff[1] = new employee("harry hacker", 50000, 1989, 10, 1); 17 staff[2] = new employee("tony tester", 40000, 1990, 3, 15); 18 19 // raise everyone's salary by 5% 20 for (employee e : staff) 21 e.raisesalary(5); 22 23 // print out information about all employee objects 24 for (employee e : staff) 25 system.out.println("name=" + e.getname() + ",salary=" + e.getsalary() + ",hireday=" 26 + e.gethireday()); 27 } 28 } 29 30 class employee 31 { 32 private string name; 33 private double salary; 34 private localdate hireday; 35 36 public employee(string n, double s, int year, int month, int day) 37 { 38 name = n; 39 salary = s; 40 hireday = localdate.of(year, month, day); 41 } 42 43 public string getname() 44 { 45 return name; 46 } 47 48 public double getsalary() 49 { 50 return salary; 51 } 52 53 public localdate gethireday() 54 { 55 return hireday; 56 } 57 58 public void raisesalary(double bypercent) 59 { 60 double raise = salary * bypercent / 100; 61 salary += raise; 62 } 63 }
程序运行结果如下:
employeetest.java
1 package test; 2 3 public class employeetest { 4 public static void main(string[] args) 5 { 6 // fill the staff array with three employee objects 7 employee[] staff = new employee[3]; 8 9 staff[0] = new employee("carl cracker", 75000, 1987, 12, 15); 10 staff[1] = new employee("harry hacker", 50000, 1989, 10, 1); 11 staff[2] = new employee("tony tester", 40000, 1990, 3, 15); 12 13 // raise everyone's salary by 5% 14 for (employee e : staff) 15 e.raisesalary(5); 16 17 // print out information about all employee objects 18 for (employee e : staff) 19 system.out.println("name=" + e.getname() + ",salary=" + e.getsalary() + ",hireday=" 20 + e.gethireday()); 21 } 22 }
employee.java
package test; import java.time.localdate; class employee { private string 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; } }
参考教材104页employeetest.java,设计studenttest.java,定义student类,包含name(姓名)、sex(性别)、javascore(java成绩)三个字段,编写程序,从键盘输入学生人数,输入学生信息。
按以下表头输出学生信息表:
姓名 性别 java成绩
程序代码如下:
1 import java.util.scanner; 2 3 public class tudent { 4 string name; 5 string sex; 6 double javascore; 7 public static void main(string[] args) { 8 system.out.println("请输入学生人数"); 9 scanner sc = new scanner(system.in); 10 int totalstudent = sc.nextint(); 11 tudent[] stus = new tudent[totalstudent]; 12 for(int i=0;i<totalstudent;i++){ 13 tudent s = new tudent(); 14 stus[i]=s; 15 system.out.println("请输入第"+(i+1)+"个学生的姓名"); 16 s.name = sc.next(); 17 system.out.println("请输入第"+(i+1)+"个学生的性别"); 18 s.sex = sc.next(); 19 system.out.println("请输入第"+(i+1)+"个学生的java成绩"); 20 s.javascore = sc.nextdouble(); 21 } 22 printtudents(stus); 23 sc.close(); 24 } 25 26 public static void printtudents(tudent[] s){ 27 system.out.println("姓名\t性别\tjava成绩"); 28 for(int i=0;i<s.length;i++){ 29 system.out.println(s[i].name+"\t"+s[i].sex+"\t"+s[i].javascore); 30 } 31 } 32 }
程序运行结果如下:
测试程序2:
编辑、编译、调试运行程序4-3(教材116);
结合程序运行结果,理解程序代码,掌握静态域(netxtid)与静态方法(getnextid)的用法,在相关代码后添加注释;
理解java单元(类)测试的技巧。
程序4-3如下
1 /** 2 * this program demonstrates static methods. 3 * @version 1.01 2004-02-19 4 * @author cay horstmann 5 */ 6 public class statictest 7 { 8 public static void main(string[] args) 9 { 10 // fill the staff array with three employee objects 11 employee[] staff = new employee[3]; 12 13 staff[0] = new employee("tom", 40000); 14 staff[1] = new employee("dick", 60000); 15 staff[2] = new employee("harry", 65000); 16 17 // print out information about all employee objects 18 for (employee e : staff) 19 { 20 e.setid(); 21 system.out.println("name=" + e.getname() + ",id=" + e.getid() + ",salary=" 22 + e.getsalary()); 23 } 24 25 int n = employee.getnextid(); // calls static method 26 system.out.println("next available id=" + n); 27 } 28 } 29 30 class employee 31 { 32 private static int nextid = 1; 33 34 private string name; 35 private double salary; 36 private int id; 37 38 public employee(string n, double s) 39 { 40 name = n; 41 salary = s; 42 id = 0; 43 } 44 45 public string getname() 46 { 47 return name; 48 } 49 50 public double getsalary() 51 { 52 return salary; 53 } 54 55 public int getid() 56 { 57 return id; 58 } 59 60 public void setid() 61 { 62 id = nextid; // set id to next available id 63 nextid++; 64 } 65 66 public static int getnextid() 67 { 68 return nextid; // returns static field 69 } 70 71 public static void main(string[] args) // unit test 72 { 73 employee e = new employee("harry", 50000); 74 system.out.println(e.getname() + " " + e.getsalary()); 75 } 76 }
程序运行结果如下:
测试程序3:
编辑、编译、调试运行程序4-4(教材121);
结合程序运行结果,理解程序代码,掌握掌握java方法参数的用法,在相关代码后添加注释;
程序4-4如下
1 /** 2 * this program demonstrates parameter passing in java. 3 * @version 1.00 2000-01-27 4 * @author cay horstmann 5 */ 6 public class paramtest 7 { 8 public static void main(string[] args) 9 { 10 /* 11 * test 1: methods can't modify numeric parameters 12 */ 13 system.out.println("testing triplevalue:"); 14 double percent = 10; 15 system.out.println("before: percent=" + percent); 16 triplevalue(percent); 17 system.out.println("after: percent=" + percent); 18 19 /* 20 * test 2: methods can change the state of object parameters 21 */ 22 system.out.println("\ntesting triplesalary:"); 23 employee harry = new employee("harry", 50000); 24 system.out.println("before: salary=" + harry.getsalary()); 25 triplesalary(harry); 26 system.out.println("after: salary=" + harry.getsalary()); 27 28 /* 29 * test 3: methods can't attach new objects to object parameters 30 */ 31 system.out.println("\ntesting swap:"); 32 employee a = new employee("alice", 70000); 33 employee b = new employee("bob", 60000); 34 system.out.println("before: a=" + a.getname()); 35 system.out.println("before: b=" + b.getname()); 36 swap(a, b); 37 system.out.println("after: a=" + a.getname()); 38 system.out.println("after: b=" + b.getname()); 39 } 40 41 public static void triplevalue(double x) // doesn't work 42 { 43 x = 3 * x; 44 system.out.println("end of method: x=" + x); 45 } 46 47 public static void triplesalary(employee x) // works 48 { 49 x.raisesalary(200); 50 system.out.println("end of method: salary=" + x.getsalary()); 51 } 52 53 public static void swap(employee x, employee y) 54 { 55 employee temp = x; 56 x = y; 57 y = temp; 58 system.out.println("end of method: x=" + x.getname()); 59 system.out.println("end of method: y=" + y.getname()); 60 } 61 } 62 63 class employee // simplified employee class 64 { 65 private string name; 66 private double salary; 67 68 public employee(string n, double s) 69 { 70 name = n; 71 salary = s; 72 } 73 74 public string getname() 75 { 76 return name; 77 } 78 79 public double getsalary() 80 { 81 return salary; 82 } 83 84 public void raisesalary(double bypercent) 85 { 86 double raise = salary * bypercent / 100; 87 salary += raise; 88 } 89 }
程序运行结果如下:
测试程序4:
编辑、编译、调试运行程序4-5(教材129);
结合程序运行结果,理解程序代码,掌握java用户自定义类的用法,掌握对象构造方法及对象使用方法,在相关代码后添加注释。
程序4-5如下:
1 import java.util.*; 2 3 /** 4 * this program demonstrates object construction. 5 * @version 1.01 2004-02-19 6 * @author cay horstmann 7 */ 8 public class constructortest 9 { 10 public static void main(string[] args) 11 { 12 // fill the staff array with three employee objects 13 employee[] staff = new employee[3]; 14 15 staff[0] = new employee("harry", 40000); 16 staff[1] = new employee(60000); 17 staff[2] = new employee(); 18 19 // print out information about all employee objects 20 for (employee e : staff) 21 system.out.println("name=" + e.getname() + ",id=" + e.getid() + ",salary=" 22 + e.getsalary()); 23 } 24 } 25 26 class employee 27 { 28 private static int nextid; 29 30 private int id; 31 private string name = ""; // instance field initialization 32 private double salary; 33 34 // static initialization block 35 static 36 { 37 random generator = new random(); 38 // set nextid to a random number between 0 and 9999 39 nextid = generator.nextint(10000); 40 } 41 42 // object initialization block 43 { 44 id = nextid; 45 nextid++; 46 } 47 48 // three overloaded constructors 49 public employee(string n, double s) 50 { 51 name = n; 52 salary = s; 53 } 54 55 public employee(double s) 56 { 57 // calls the employee(string, double) constructor 58 this("employee #" + nextid, s); 59 } 60 61 // the default constructor 62 public employee() 63 { 64 // name initialized to ""--see above 65 // salary not explicitly set--initialized to 0 66 // id initialized in initialization block 67 } 68 69 public string getname() 70 { 71 return name; 72 } 73 74 public double getsalary() 75 { 76 return salary; 77 } 78 79 public int getid() 80 { 81 return id; 82 } 83 }
程序运行结果如下:
测试程序5:
编辑、编译、调试运行程序4-6、4-7(教材135);
结合程序运行结果,理解程序代码,掌握java包的定义及用法,在相关代码后添加注释;
程序4-6如下:
1 import com.horstmann.corejava.*; 2 // the employee class is defined in that package 3 4 import static java.lang.system.*; 5 6 /** 7 * this program demonstrates the use of packages. 8 * @version 1.11 2004-02-19 9 * @author cay horstmann 10 */ 11 public class packagetest 12 { 13 public static void main(string[] args) 14 { 15 // because of the import statement, we don't have to use 16 // com.horstmann.corejava.employee here 17 employee harry = new employee("harry hacker", 50000, 1989, 10, 1); 18 19 harry.raisesalary(5); 20 21 // because of the static import statement, we don't have to use system.out here 22 out.println("name=" + harry.getname() + ",salary=" + harry.getsalary()); 23 } 24 }
程序运行结果如下:
程序4-7如下:
1 package com.horstmann.corejava; 2 3 // the classes in this file are part of this package 4 5 import java.time.*; 6 7 // import statements come after the package statement 8 9 /** 10 * @version 1.11 2015-05-08 11 * @author cay horstmann 12 */ 13 public class employee 14 { 15 private string name; 16 private double salary; 17 private localdate hireday; 18 19 public employee(string name, double salary, int year, int month, int day) 20 { 21 this.name = name; 22 this.salary = salary; 23 hireday = localdate.of(year, month, day); 24 } 25 26 public string getname() 27 { 28 return name; 29 } 30 31 public double getsalary() 32 { 33 return salary; 34 } 35 36 public localdate gethireday() 37 { 38 return hireday; 39 } 40 41 public void raisesalary(double bypercent) 42 { 43 double raise = salary * bypercent / 100; 44 salary += raise; 45 } 46 }
实验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) 将两个对象的周长加总输出,将两个对象的面积加总输出。
程序如下:
1 import java.util.*; 2 3 public class shapecount { 4 5 public static void main(string[] args) { 6 scanner in = new scanner(system.in); 7 system.out.println("输入长:"); 8 double length = in.nextdouble(); 9 system.out.println("输入宽:"); 10 double width = in.nextdouble(); 11 system.out.println("输入半径:"); 12 double radius = in.nextdouble(); 13 rectangle a=new rectangle(length,width); 14 circle b=new circle(radius); 15 system.out.println("矩形周长:"+a.getperimeter()+"矩形面积:"+a.getarea()); 16 system.out.println("圆周长"+b.getperimeter()+"圆面积:"+b.getarea()); 17 double c = a.getperimeter()+b.getperimeter(); 18 double d = a.getarea()+b.getarea(); 19 system.out.println("周长和:"+c+"面积和:"+d); 20 } 21 22 } 23 24 25 class rectangle { 26 private double width; 27 private double length; 28 public rectangle(double w,double l) 29 { 30 width=w; 31 length=l; 32 } 33 public double getperimeter() 34 { 35 double perimeter = (width+length)*2; 36 return perimeter; 37 } 38 public double getarea() 39 { 40 double area = width*length; 41 return area; 42 } 43 } 44 45 class circle { 46 47 private double radius; 48 double pi = 3.14; 49 public circle(double r) 50 { 51 radius=r; 52 } 53 public double getperimeter() 54 { 55 double perimeter = 2*pi*radius; 56 return perimeter; 57 } 58 public double getarea() 59 { 60 double area = pi*radius*radius; 61 return area; 62 } 63 }
程序结果如下:
第二部分:总结
本周自学了第四章,通过这周的学习,我掌握了预定义类的基本使用方法,如math类、string类、math类、scanner类、localdate类等常用api;大致掌握了用户自定义类的语法规则,如实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求等。
但还是有些不足,应该是概念理解不够深刻。上课实验时老师带着我们完成了文件的读写,更重要的是教会了我们正确的读别人代码的方法,作为一个优秀的猿和农,应该具备能写出高质量的代码和读懂别人代码的能力。在课后自学时间里,完成了剩余的实验任务。在运行示例代码的过程中不仅学会了相关知识,还更加规范了自己的编码风格。学习的一些漏洞和疑惑,在看翁恺老师的视频时解决了部分,但还是感觉自己有着很多的欠缺,在以后的学习中会更加努力。总体来讲这个中秋过得很充实!!!