杨其菊201771010134《面向对象程序设计(java)》第六周学习总结
Posted yqj-yf-111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杨其菊201771010134《面向对象程序设计(java)》第六周学习总结相关的知识,希望对你有一定的参考价值。
《面向对象程序设计(java)》第六周学习总结
第一部分:理论知识
1)类、超类和子类
2)Object:所有类的超类
4)对象包装器和自动打包
7)继承设计的技巧
第二部分:实验部分
继承定义与使用《代码测试和示例程序的注释》
1、实验目的与要求
(1) 理解继承的定义;
(2) 掌握子类的定义要求
(3) 掌握多态性的概念及用法;
(4) 掌握抽象类的定义及用途;//不能创建自己的对象,特殊类
(5) 掌握类中4个成员访问权限修饰符的用途;//public和private,
(6) 掌握抽象类的定义方法及用途;
(7)掌握Object类的用途及常用API;//最顶层的类,
(8) 掌握ArrayList类的定义方法及用法;//预定一类
(9) 掌握枚举类定义方法及用途。//预定一类
2、实验内容和步骤
实验1: 导入第5章示例程序,测试并进行代码注释。
测试程序1:
? 在elipse IDE中编辑、调试、运行程序5-1 (教材152页-153页) ;
? 掌握子类的定义及用法;
? 结合程序运行结果,理解并总结OO风格程序构造特点,理解Employee和Manager类的关系子类的用途,并在代码中添加注释。
1 package inheritance; 2 3 /** 4 * This program demonstrates inheritance. 5 * @version 1.21 2004-02-21 6 * @author Cay Horstmann 7 */ 8 public class ManagerTest 9 { 10 public static void main(String[] args) 11 { 12 // 创建一个Manager类对象; 13 Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); 14 boss.setBonus(5000); 15 16 Employee[] staff = new Employee[3]; //定义一个雇员数组; 17 18 // 用管理者和雇员对象填充数组; 19 20 staff[0] = boss; 21 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); 22 staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15); 23 24 // 输出所有雇员对象的信息; 25 for (Employee e : staff) 26 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); 27 28 } 29 }
package inheritance; 2 3 import java.time.*; 4 5 public class Employee 6 { 7 //定义Employee属性; 8 private String name; 9 private double salary; 10 private LocalDate hireDay; 11 //构造Employee对象,及初始化其属性, 12 public Employee(String name, double salary, int year, int month, int day) 13 { 14 this.name = name; 15 this.salary = salary; 16 hireDay = LocalDate.of(year, month, day); 17 } 18 //name属性访问器 19 public String getName() 20 { 21 return name; 22 } 23 //Salary属性访问器; 24 public double getSalary() 25 { 26 return salary; 27 } 28 //HireDay属性访问器 29 public LocalDate getHireDay() 30 { 31 return hireDay; 32 } 33 //raiseSalary方法; 34 public void raiseSalary(double byPercent) 35 { 36 double raise = salary 37 salary +=raise; 38 }
1 package inheritance; 2 3 public class Manager extends Employee//定义新类Manager是Employee的子类; 4 { 5 private double bonus;//子类独有属性; 6 7 /** 8 * @param name the employee‘s name 9 * @param salary the salary 10 * @param year the hire year 11 * @param month the hire month 12 * @param day the hire day 13 */ 14 //构造Manager对象并初始化其属性;super直接调用父类参数,name,salary, year, month, day,无返回值; 15 16 public Manager(String name, double salary, int year, int month, int day) 17 { 18 super(name, salary, year, month, day); 19 bonus = 0; //Manager独有属性并初始化; 20 } 21 //Salary属性访问器, 22 public double getSalary() 23 { 24 //父类中的方法语句在子类中被重写成“独有方法”; 25 double baseSalary = super.getSalary(); 26 return baseSalary + bonus; 27 } 28 //Bonus属性更改器; 29 public void setBonus(double b) 30 { 31 bonus = b; 32 } 33 }
实验结果截图:
测试程序2:
? 编辑、编译、调试运行教材PersonTest程序(教材163页-165页);
? 掌握超类的定义及其使用要求;
? 掌握利用超类扩展子类的要求;
? 在程序中相关代码处添加新知识的注释。
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[] people = new Person[2]; // 用学生和雇员对象填充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());
}
}
package abstractClasses; public abstract class Person //定义抽象类型Person;
{
public abstract String getDescription();
private String name;
public Person(String name) {
this.name = name;
}
//Name属性访问器;
public String getName() {
return name;
}
}
package 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); //调用父类方法;
this.salary = salary;//this代替当前对象;
hireDay = LocalDate.of(year, month, day);//LocalDate方法;
}
//Salary属性访问器
public double getSalary()
{
return salary;
}
public LocalDate getHireDay()
{
return hireDay;
}
//Description属性访问器,String类型;
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}
//Employee类独有方法;
public void raiseSalary(double byPercent)
{
//计算语句;
double raise = salary * byPercent / 100; salary += raise;
}
}
package abstractClasses; public class Student extends Person//子类Student继承父类Person; { private String major; /** * @param nama the student‘s name * @param major the student‘s major */ public Student(String name, String major) { // 将name传递给父类构造函数; super(name); this.major = major; } public String getDescription() { return "a student majoring in " + major; } }
运行结果如下:
测试程序3:
? 编辑、编译、调试运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);
? 掌握Object类的定义及用法;
? 在程序中相关代码处添加新知识的注释。
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()); } }
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; } public boolean equals(Object otherObject) { //快速检查对象是否相同 if (this == otherObject) return true; //如果显示参数为null,则必须返回false; if (otherObject == null) return false; // 如果这些 类不匹配,他们不相等。 if (getClass() != otherObject.getClass()) return false; // 现在我们知道另一个对象是非空雇员; Employee other = (Employee) otherObject; //测试字段是否具有相同的值; return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); } public int hashCode() { return Objects.hash(name, salary, hireDay); } //toString方法; public String toString() { return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
package equals; public class Manager extends //Employee子类Manager继承父类Employee {
// 定义属性; private double bonus; public Manager(String name, double salary, int year, int month, int day) { super(name, salary, year, month, day);//调用父类参数,无返回值; bonus = 0; } //Salary属性访问器; 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; // 检查这个是否与其他是否属于同一类; return bonus == other.bonus; } public int hashCode() { return java.util.Objects.hash(super.hashCode(), bonus); } public String toString() { return super.toString() + "[bonus=" + bonus + "]"; } }
测试程序4:
? 在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;
? 掌握ArrayList类的定义及用法;
? 在程序中相关代码处添加新知识的注释。
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) { // 用雇员对象填充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); // 打印所有雇员对象的信息; 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的用法; 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:
? 编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;
? 掌握枚举类的定义及用法;
? 在程序中相关代码处添加新知识的注释。
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) {
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
? 定义抽象类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方法放在哪个类中更合适?
输入样例:
3 rect 1 1 rect 2 2 cir 1
输出样例:
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
1 package Attention; 2 3 import java.math.*; 4 import java.util.*; 5 import Attention.Shape; 6 import Attention.Rectangle; 7 import Attention.Circle; 8 9 public class sum 10 { 11 12 public static void main(String[] args) 13 { 14 Scanner in = new Scanner(System.in); 15 String rect = "rect"; 16 String cir = "cir"; 17 System.out.print("请输入形状个数:"); 18 int n = in.nextInt(); 19 Shape[] score = new Shape[n]; 20 for(int i=0;i<n;i++) 21 { 22 System.out.println("请输入形状类型 (rect or cir):"); 23 String input = in.next(); 24 if(input.equals(rect)) 25 { 26 double length = in.nextDouble(); 27 double width = in.nextDouble(); 28 System.out.println("Rectangle["+"length:"+length+" width:"+width+"]"); 29 score[i] = new Rectangle(width,length); 30 } 31 if(input.equals(cir)) 32 { 33 double radius = in.nextDouble(); 34 System.out.println("Circle["+"radius:"+radius+"]"); 35 score[i] = new Circle(radius); 36 } 37 } 38 Shape c = new Shape(); 39 System.out.println(c.sumAllPerimeter(score)); 40 System.out.println(c.sumAllArea(score)); 41 for(Shape s:score) 42 { 43 44 System.out.println(s.getClass()+", "+s.getClass().getSuperclass()); 45 } 46 } 47 48 public double sumAllArea(Shape core[]) 49 { 50 double sum = 0; 51 Object score; 52 for(int i = 0;i<score.length;i++) 53 sum+= score[i].getArea(); 54 return sum; 55 } 56 57 public double sumAllPerimeter(Shape score[]) 58 { 59 double sum = 0; 60 for(int i = 0;i<score.length;i++) 61 sum+= score[i].getPerimeter(); 62 return sum; 63 } 64 65 }
1 package Attention; 2 3 4 public abstract class Shape 5 { 6 double PI = 3.14;//不可变常量double PI,值为3.14; 7 //方法:public double getPerimeter();public double getArea()); 8 public abstract double getPerimeter(); 9 public abstract double getArea(); 10 }
1 package Attention; 2 3 public class Rectangle extends Shape //Rectangle继承自Shape类; 4 { 5 private double width; 6 private double length; 7 public Rectangle(double w,double l) 8 { 9 this.width=w; 10 this.length=l; 11 } 12 public double getPerimeter() 13 { 14 double Perimeter = (width+length)*2; 15 return Perimeter; 16 } 17 public double getArea() 18 { 19 double Area = width*length; 20 return Area; 21 } 22 23 public String toString() 24 { 25 return getClass().getName() + "[ width=" + width + "]"+ "[length=" + length + "]"; 26 } 27 }
1 package Attention; 2 3 4 public class Circle extends Shape//Circle继承自Shape类 5 { 6 7 private double radius; 8 public Circle(double r) 9 { 10 radius = r; 11 } 12 public double getPerimeter() 13 { 14 double Perimeter = 2*PI*radius; 15 return Perimeter; 16 } 17 public double getArea() 18 { 19 double Area = PI*radius*radius; 20 return Area; 21 } 22 public String toString() 23 { 24 return getClass().getName() + "[radius=" + radius + "]"; 25 } 26 }
实验3: 编程练习2
编制一个程序,将身份证号.txt 中的信息读入到内存中,输入一个身份证号或姓名,查询显示查询对象的姓名、身份证号、年龄、性别和出生地。
1 package Id; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 import java.util.Scanner; 11 12 public class ID { 13 14 public static People findPeopleByname(String name) { 15 People flag = null; 16 for (People people : peoplelist) { 17 if(people.getName().equals(name)) { 18 flag = people; 19 } 20 } 21 return flag; 22 23 } 24 25 public static People findPeopleByid(String id) { 26 People flag = null; 27 for (People people : peoplelist) { 28 if(people.getnumber().equals(id)) { 29 flag = people; 30 } 31 } 32 return flag; 33 34 } 35 36 private static ArrayList<People> peoplelist; 37 38 public static void main(String[] args) { 39 peoplelist = new ArrayList<People>(); 40 Scanner scanner = new Scanner(System.in); 41 File file = new File("E:\\面向对象程序设计Java\\实验\\身份证号.txt"); 42 try { 43 FileInputStream files = new FileInputStream(file); 44 BufferedReader in = new BufferedReader(new InputStreamReader(files)); 45 String temp = null; 46 while ((temp = in.readLine()) != null) { 47 48 Scanner linescanner = new Scanner(temp); 49 linescanner.useDelimiter(" "); 50 String name = linescanner.next(); 51 String number = linescanner.next(); 52 String sex = linescanner.next(); 53 String age = linescanner.next(); 54 String place = linescanner.nextLine(); 55 People people = new People(); 56 people.setName(name); 57 people.setnumber(number); 58 people.setage(age); 59 people.setsex(sex); 60 people.setplace(place); 61 peoplelist.add(people); 62 63 } 64 } catch (FileNotFoundException e) { 65 System.out.println("文件未找到"); 66 e.printStackTrace(); 67 } catch (IOException e) { 68 System.out.println("文件读取错误"); 69 e.printStackTrace(); 70 } 71 boolean isTrue = true; 72 while (isTrue) { 73 74 System.out.println("***********"); 75 System.out.println("1.按姓名查询"); 76 System.out.println("2.按身份证号查询"); 77 System.out.println("3.退出"); 78 System.out.println("***********"); 79 int nextInt = scanner.nextInt(); 80 switch (nextInt) { 81 case 1: 82 System.out.println("请输入姓名:"); 83 String peoplename = scanner.next(); 84 People person = findPeopleByname(peoplename); 85 if (people != null) { 86 System.out.println(" 姓名:"+
person.getName() + 87 " 身份证号:"+ person.getnumber() + 88 " 年龄:"+ person.getage()+ 89 " 性别:"+ person.getsex()+ 90 " 地址:"+ person.getplace() 91 ); 92 } else { 93 System.out.println("
此人不存在"); 94 } 95 break; 96 case 2: 97 System.out.println("请输入身份证号:"); 98 String peopleid = scanner.next(); 99 People person1 = findPeopleByid(peopleid); 100 if (people1 != null) { 101 System.out.println(" 姓名:"+person
1.getName()+ 102 " 身份证号:"+ person1.getnumber()+ 103 " 年龄:"+ person1.getage()+ 104 " 性别:"+ person1.getsex()+ 105 " 地址:"+ person1.getplace()); 106 } else { 107 System.out.println("此人不存在"); 108 } 109 break; 110 case 3: 111 isTrue = false; 112 System.out.println("byebye!"); 113 break; 114 default: 115 System.out.println("输入有误"); 116 } 117 } 118 } 119 120 121 }
1 package Id; 2 3 public class Person{ 4 5 private String name; 6 private String number; 7 private String age; 8 private String sex; 9 private String place; 10 11 public String getName() 12 { 13 return name; 14 } 15 public void setName(String name) 16 { 17 this.name = name; 18 } 19 public String getnumber() 20 { 21 return number; 22 } 23 public void setnumber(String number) 24 { 25 this.number = number; 26 } 27 public String getage() 28 { 29 return age; 30 } 31 public void setage(String age ) 32 { 33 this.age = age; 34 } 35 public String getsex() 36 { 37 return sex; 38 } 39 public void setsex(String sex ) 40 { 41 this.sex = sex; 42 } 43 public String getplace() 44 { 45 return place; 46 } 47 public void setplace(String place) 48 { 49 this.place = place; 50 } 51 }
第三部分:总结
上周实验课助教老师的详细讲解对我的来说有很大帮助,以及老师在课堂带领我们阅读代码 让我学到很多。总的来说,能力还是很低下,我意识到自己的自主学习能力比较差,这与自身素质方面有很大关系,希望我可以通过这次彻底的认识到自己的问题,提高自己的能力。
以上是关于杨其菊201771010134《面向对象程序设计(java)》第六周学习总结的主要内容,如果未能解决你的问题,请参考以下文章
杨其菊201771010134《面向对象程序设计(Java)》第三周学习总结
201771010134杨其菊《面向对象程序设计java》第十周学习总结
杨其菊201771010134《面向对象程序设计Java》第二周学习总结
201771010134杨其菊《面向对象程序设计java》第十二周学习总结