博文正文开头格式:(2分)html
项目java |
内容编程 |
这个做业属于哪一个课程数组 |
https://www.cnblogs.com/nwnu-daizh/ide |
这个做业的要求在哪里函数 |
https://www.cnblogs.com/nwnu-daizh/p/11435127.html工具 |
做业学习目标post |
|
实验内容和步骤学习
实验1:在“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法。(20分)测试
实验代码以下:
package ppp; class Parent { private String p1 = "这是Parent的私有属性"; public String p2 = "这是Parent的公有属性"; protected String p3 = "这是Parent受保护的属性"; String p4 = "这是Parent的默认属性"; private void pMethod1() { System.out.println("我是Parent用private修饰符修饰的方法"); } public void pMethod2() { System.out.println("我是Parent用public修饰符修饰的方法"); } protected void pMethod3() { System.out.println("我是Parent用protected修饰符修饰的方法"); } void pMethod4() { System.out.println("我是Parent无修饰符修饰的方法"); } } class Son extends Parent{ private String s1 = "这是Son的私有属性"; public String s2 = "这是Son的公有属性"; protected String s3 = "这是Son受保护的属性"; String s4 = "这是Son的默认属性"; public void sMethod1() { System.out.println(p2);//分别尝试显示Parent类的p一、p二、p三、p4值 System.out.println("我是Son用public修饰符修饰的方法"); } private void sMethod2() { System.out.println("我是Son用private修饰符修饰的方法"); } protected void sMethod() { System.out.println("我是Son用protected修饰符修饰的方法"); } void sMethod4() { System.out.println("我是Son无修饰符修饰的方法"); } } public class Demo { public static void main(String[] args) { Parent parent=new Parent(); Son son=new Son(); parent.pMethod2(); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法 } }
Parent的代码以下:
package com.nwnu.demo1; public class Parent { private String p1 = "这是Parent的私有属性"; public String p2 = "这是Parent的公有属性"; protected String p3 = "这是Parent受保护的属性"; String p4 = "这是Parent的默认属性"; private void pMethod1() { System.out.println("我是Parent用private修饰符修饰的方法"); } public void pMethod2() { System.out.println("我是Parent用public修饰符修饰的方法"); } protected void pMethod3() { System.out.println("我是Parent用protected修饰符修饰的方法"); } void pMethod4() { System.out.println("我是Parent无修饰符修饰的方法"); } }
Son的代码以下:
package com.nwnu.demo2; import com.nwnu.demo1.Parent; public class Son extends Parent{ private String s1 = "这是Son的私有属性"; public String s2 = "这是Son的公有属性"; protected String s3 = "这是Son受保护的属性"; String s4 = "这是Son的默认属性"; public void sMethod1() { System.out.println(p2);//分别尝试显示Parent类的p一、p二、p三、p4值 System.out.println("我是Son用public修饰符修饰的方法"); } private void sMethod2() { System.out.println("我是Son用private修饰符修饰的方法"); } protected void sMethod() { System.out.println("我是Son用protected修饰符修饰的方法"); } void sMethod4() { System.out.println("我是Son无修饰符修饰的方法"); } }
运行结果以下:
调用p2以及方法2获得的结果:
调用方法获得的结果:
调用方法四获得的结果:
四种权限修饰符的访问范围的状况:
访问范围 | private | friendly(默认) | protected | public |
同一个类 | 能够访问 | 能够访问 | 能够访问 | 能够访问 |
同一个包内的类 | 不能够访问 | 能够访问 | 能够访问 | 能够访问 |
不一样包内的类 | 不能够访问 | 不能够访问 | 能够访问 | 能够访问 |
不一样包而且不是子类 | 不能够访问 | 不能够访问 | 不能够访问 | 能够访问 |
protected介于public和private之间,只能被类自己的方法和子类访问,即便子类在不一样的包中也能够访问。
默认访问的这种修饰符,只能被在同一个包中的类访问和引用,而不能被其余包中的类使用,即便其余包中有该类的子类。
实验2:测试程序1
l 运行教材程序5-八、5-九、5-10,结合程序运行结果理解程序(教材174页-177页);
实验代码以下:
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) { // a quick test to see if the objects are identical if (this == otherObject) return true; //检查这些值是否相等 // must return false if the explicit parameter is null if (otherObject == null) return false; //若是显示参数为空,则返回false // if the classes don't match, they can't be equal if (getClass() != otherObject.getClass()) return false; //若是类不相等,则它们不匹配 // now we know otherObject is a non-null Employee Employee other = (Employee) otherObject; //otherObject 是一个非空雇员对象 // test whether the fields have identical values return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); //检测它们是否具备相同的值 } public int hashCode() { return Objects.hash(name, salary, hireDay); } 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) //Manager构造器 { super(name, salary, year, month, day); bonus = 0; } 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 checked that this and other belong to the same class return bonus == other.bonus; } public int hashCode() { return java.util.Objects.hash(super.hashCode(), bonus); } public String toString() { return super.toString() + "[bonus=" + bonus + "]"; } }
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()); } }
运行结果以下:
l 删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。(15分)
Employee类重写后代码以下:
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; } @Override public int hashCode() { //重写hashCode方法,使相等的两个对象获取的HashCode也相等 // TODO Auto-generated method stub return Objects.hash(name, salary, hireDay); } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if (this == obj) return true; //快速测试几个类的根是否相同,便是否是同一个超类。这个if语句判断两个引用是不是同一个,若是是同一个,那么这两个对象确定相等。 if (obj == null) return false; //若是显示参数为空,则返回false if (getClass() !=obj.getClass()) return false; //用getClass()方法获得对象的类。若是几个类不匹配,则它们不相等 //其余对象是非空Employee类 //在以上判断完成,再将获得的参数对象强制转换为该对象,考虑到父类引用子类的对象的出现,而后再判断对象的属性是否相同 Employee other = (Employee) obj; //测试字段是否具备相同的值 return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay); } @Override public String toString() { //把其余类型的数据转为字符串类型的数据(toString方法能够自动生成) // TODO Auto-generated method stub return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]"; } }
Manager类重写以后代码以下:
package 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() //把其余类型的数据转为字符串类型的数据(toString方法能够自动生成) { return super.toString() + "[bonus=" + bonus + "]"; } }
EmployeeTest类代码以下:
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()); } }
运行结果以下:
一、equals方法:
Object类中的equals方法用于检测某个对象是否同另外一个对象相等。它在Object类中的实现是判断两个对象是否相等的引用。若是两个对象具备相同的引用,它们必定是相等的;
若是须要检测两个对象状态的相等性,就须要在新类的定义中须要覆盖equals方法;
定义子类的equals方法时,可调用超累的equals方法;
super. equals (otherObjecct)
二、hashCode方法:
Object类中的哈hashCode方法导出某个对象的散列码。散列码时任意整数,表示dui过的存储地址;
两个相等对象的散列码相等。
三、toString方法:
Object类中的toString方法返回一个表明该类对象域值的字符串;
定义子类的toString方法时,可先调用超类的toString方法;
super.toString()
toString方法是很是重要的调试工具。标准类库中,多数类定义了toString方法,以便用户得到对象状态的必要信息。
实验2:测试程序2
l 在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;
l 掌握ArrayList类的定义及用法;
l 在程序中相关代码处添加新知识的注释;
实验代码以下:
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) { // fill the staff array list with three Employee objects ArrayList<Employee> staff = new ArrayList<Employee>(); //用三个Employee对象填充数组 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)); // raise everyone's salary by 5% for (Employee e : staff) //把每一个人的薪资提升%5 e.raiseSalary(5); // print out information about all Employee objects for (Employee e : staff) //输出全部雇员对象的信息 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); //利用getName(),getSalary() 和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) //Employee构造器 { 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; } }
运行结构以下:
l 设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法。(15分)
实验代码以下:
package arrayList; import java.util.*; /** * This program demonstrates the ArrayList class. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class ArrayListTest { private static final Employee element = null; private static final int index = 0; public static void main(String[] args) { // fill the staff array list with three Employee objects ArrayList<Employee> staff = new ArrayList<Employee>(); //用三个Employee对象填充数组 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)); ArrayList<Employee> list = new ArrayList<Employee>(); //size()的用法 int size=staff.size(); System.out.println("arrayList中的元素个数是:"+size); for(int i=0;i<staff.size();i++) { //get()的用法 Employee e=staff.get(i); System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); } //set()的用法 staff.set(0, new Employee("llx", 20000, 1999, 11, 06)); Employee e=staff.get(0); System.out.println("修改后的数据为:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); //remove()的用法 staff.remove(2); System.out.println("将第一个数据删除后:"); int size1=staff.size(); System.out.println("arrayList中的元素个数是:"+size1); for(int i=0;i<staff.size();i++) { Employee p=staff.get(i); System.out.println("name=" + p.getName() + ",salary=" + p.getSalary() + ",hireDay=" + p.getHireDay()); } // raise everyone's salary by 5% for (Employee e1 : staff) //把每一个人的薪资提升%5 e1.raiseSalary(5); // print out information about all Employee objects for (Employee e1 : staff) //输出全部雇员对象的信息 System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay=" + e1.getHireDay()); //利用getName(),getSalary() 和getHireDay()方法输出全部雇员对象的信息 } }
运行结果以下:
1五、泛型数组列表:
Java中,利用ArrayList类,可容许程序在运行时肯定数组的大小;
ArrayList是一个采用类型参数的泛型类。为指定数组列表保存元素的对象类型,须要用一对尖括号将数组元素的对象类名括起来加在后面;
没有<>的ArrayList类将被认为是一个删去了类型参数的“原始”类型。
1六、数组列表的操做:
ArrayList的定义:ArrayList<T> 对象 = new ArrayList<T>();
API:
(1)ArrayList的构造器
——ArrayList<T>()构造一个空数组列表
——ArrayList<T>(int initialCapacity)构造一个具备指定容量的空数组列表;
(2)添加新元素:
API:boolean add(T obj)把元素obj追加到数组列表的结尾;
(3)统计个数:
API:int size() 返回数组列表中当前元素的个数;
(4)调整大小:
API:void trimToSize() 把数组列表的存储空间调整到当前的大小;
(5)访问:
API:void Set(int index, T obj) 将obj放入到数组列表index的位置,将覆盖这个位置原来的内容;
API:T get(int index) 将得到指定位置index的元素值;
(6)增长与删除:
API:boolean add(T obj) 向后移动元素,在第n个位置插入obj;
API:T remove(int index) 将第n个位置存放的对象删除,并将后面的元素向前
实验2:测试程序3
l 编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;
l 掌握枚举类的定义及用法;
l 在程序中相关代码处添加新知识的注释;
实验代码以下:
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); //首先构造一个Scanner对象,而且与 “标准输入流” System.in关联 System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) "); //将该字符串输出在控制台上 String input = in.next().toUpperCase(); //定义一个String类 变量input 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; }
运行结果以下:
一、建立枚举类型要使用 Enum 关键字。
二、枚举类对象的属性不该容许被改动, 因此应该使用 private final 修饰。
五、枚举类说明:
1)枚举类是一个类,它的隐含超类是Java.lang.Enum。
2)枚举值并非整型或其余类型,是被声明的枚举类自己实例;
3)枚举类不能有public 修饰的构造函数,构造函数都是隐含private,编译器自动处理;
4)枚举值隐含都是由public、static、final修饰的,无需本身添加这些修饰符;
5)在比较两个枚举类型的值时,永远不须要调用 equals方法,直接使用“==”进行比较;
实验2:测试程序4
录入如下代码,结合程序运行结果了解方法的可变参数用法(5分)
实验代码以下:
public class TestVarArgus { public static void dealArray(int... intArray){ //定义为整型,在下面就只能输入整型,不能输入其余的,例如字符串等等 for (int i : intArray) System.out.print(i +" "); System.out.println(); } public static void main(String args[]){ dealArray(); dealArray(1); dealArray(1, 2, 3); dealArray(1, 2, 3, 4, 5); dealArray(1, 2, 3, 4, 5, 6); } }
运行结果以下:
定义为字符串类型实验代码以下:
public class TestVarArgus { public static void dealArray(String... StringArray){ //定义为字符串,在下面就只能输入字符串,不能输入其余的,例如整型等等 for (String string : StringArray) System.out.print(string +" "); System.out.println(); } public static void main(String args[]){ dealArray(); dealArray("Welcome to normal university!"); dealArray("She hates to go my heaven alone!"); dealArray("It will be yours!"); dealArray("Envy!!!!!!!!!!"); } }
运行结果以下:
实验3:编程练习 参照输出样例补全程序,使程序输出结果与输出样例一致。( 10分)
实验代码以下:
public class Demo { public static void main(String[] args) { Son son = new Son(); son.method(); } } class Parent { Parent() { System.out.println("Parent's Constructor without parameter"); } Parent(boolean b) { System.out.println("Parent's Constructor with a boolean parameter"); } public void method() { System.out.println("Parent's method()"); } } class Son extends Parent { //补全本类定义 Son(){ super(false); System.out.println("Son's Constructor without parameter"); } public void method() { System.out.println("Son's method()"); super.method(); } }
运行结果以下
3. 实验总结:(15分)
在前几周的学习基础上,这周接着学习了第五章,相比国庆节前,加深了对继承类、抽象类以及多态的学习。但对于后面学习的各类方法以及枚举类等新知识,在学习理论知识时,能够跟着老师的思路,以为已经掌握了知识点,但在实验过程当中发现本身仍是不会运用,肯平时没有多看书,对基础知识掌握不到位。对于老师设计的简单编程题,因为本身的知识还不够,所以几乎不能独立的作出完整的实验。因此在之后的学习中,我会多看翁恺老师的视频以及老师的课件来学习编程,来写出完整的程序。