201871010112-梁丽珍《面向对象程序设计(java)》第七周学习总结
项目 |
内容 |
这个作业属于哪个课程 |
|
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11654436.html |
作业学习目标 |
|
实验内容和步骤
实验1: 在“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法。
package Demo; 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();//分别尝试显示Parent类的p1、p2、p3、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();
System.out.println(); //分别尝试用parent调用Paren类的方法、用son调用Son类的方法
}
}
运行结果:
父类与子类在同一个包内,子类可以直接访问父类public、proteced与默认访问特性的成员,不能直接访问private成员:
子类与父类不在同一个包内,子类继承父类public成员变量作为子类的成员变量以及方法:
实验2:导入第5章以下示例程序,测试并进行代码注释。
测试程序1:
运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);
删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。
5-8源代码:
package equals; /**
* This program demonstrates the equals method.
* @version 1.12 2012-01-26
* @author Cay Horstmann
*/
public class EqualsTest //此程序实现了Employee类和Manager类的equals,hashCode,toString方法
{
public static void main(String[] args) //定义方法
{
var alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
var alice2 = alice1;
var alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
var 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); var carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
var 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());
}
}
5-9源代码:
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; //this调用
hireDay = LocalDate.of(year, month, day);
} public String getName() // getName方法
{
return name;
} public double getSalary() //getSalary方法
{
return salary;
} public LocalDate getHireDay() //getHireDay方法
{
return hireDay;
} public void raiseSalary(double byPercent) //raiseSalary方法
{
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; //检测this与otherObject是否引用同一个对象 // must return false if the explicit parameter is null
if (otherObject == null) return false; //检测otherObject是否为null,如果为null,返回false // if the classes don't match, they can't be equal
if (getClass() != otherObject.getClass()) return false; //比较this与otherObject是否属于同一个类 // now we know otherObject is a non-null Employee
var other = (Employee) 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() //hashCode方法
{
return Objects.hash(name, salary, hireDay);
} public String toString() //调用超类的toString方法
{
return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay="
+ hireDay + "]";
}
}
5-10源代码:
package equals;
//定义Manager方法
public class Manager extends Employee
{
private double bonus; public Manager(String name, double salary, int year, int month, int day) //提供一个子类构造器
{
super(name, salary, year, month, day);
bonus = 0;
} public double getSalary() //getSalary方法
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
} public void setBonus(double bonus) //使用setBonus方法
{
this.bonus = bonus;
} public boolean equals(Object otherObject) //equals方法比较两个对象是否相同
{
if (!super.equals(otherObject)) return false;
var other = (Manager) otherObject; //强制类型转换
// super.equals checked that this and other belong to the same class (检查这个和其他都同属于一个类)
return bonus == other.bonus;
} public int hashCode() //hashCode方法,返回对象的散列码
{
return java.util.Objects.hash(super.hashCode(), bonus);
} public String toString() //Manager类中的toString方法,返回描述该对象的字符串
{
return super.toString() + "[bonus=" + bonus + "]";
}
}
程序运行结果:
测试程序2:
在elipse IDE中调试运行程序5-11(教材182页),结合程序运行结果理解程序;
掌握ArrayList类的定义及用法;
在程序中相关代码处添加新知识的注释;
设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法。
5-11源代码:
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
var staff = new ArrayList<Employee>(); //将Employee[]数组替换成ArrayList<Employee> //使用add方法将雇员对象添加到数组列表中
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% (把每个人的工资提高5%)
for (Employee e : staff)
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());
}//循环数组
}
运行结果:
设计适当的代码,测试ArrayList类的set()、get()、remove()、size()等方法的用法:
package project5; import java.util.ArrayList;
import arrayList.Employee; public class myArrayList { public static void main(String[] args) {
var staff = new ArrayList<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)); //size()
int c = staff.size();
System.out.println("ArrayList中存储的元素个数为:"+c);
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("LAKD",80000,1988,11,23));
Employee e = staff.get(0);
System.out.println("修改后为:name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay()); //remove()
staff.remove(1);
System.out.println("Now, Size of list::"+staff.size()); int size1=staff.size(); System.out.println("ArrayList中存储的元素个数为:"+size1);
for(int i=0; i<staff.size(); i++)
{
Employee q = staff.get(i);
System.out.println("name=" + q.getName() + ",salary=" + q.getSalary() + ",hireDay=" + q.getHireDay());
} for (Employee e1 : staff)
e1.raiseSalary(5); for (Employee e1 : staff)
System.out.println("name=" + e1.getName() + ",salary=" + e1.getSalary() + ",hireDay=" + e1.getHireDay());
}
}
运行结果:
测试程序3:
编辑、编译、调试运行程序5-12(教材189页),结合运行结果理解程序;
掌握枚举类的定义及用法;
在程序中相关代码处添加新知识的注释;
删除程序中Size枚举类,背录删除代码,在代码录入中掌握枚举类的定义要求。
5-12源代码:
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)
{
var in = new Scanner(System.in);
System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");
String input = in.next().toUpperCase(); //输入;字符串转换为大写
Size size = Enum.valueOf(Size.class, input); //使用静态方法valueOf
//Size.class是反射,取得Size类
//调用构造函数,并赋值返回枚举数组的值:Size.SMALL;Size.MEDIUM;Size.LARGE;Size.EXTRA_LARGE
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 _.");
in.close();
}
} //定义枚举类型 使用关键字enum
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; //定义属性
}
运行结果:
测试程序4:录入以下代码,结合程序运行结果了解方法的可变参数用法
代码:
package project5; 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);
}
}
运行结果:
实验:3:编程练习:参照输出样例补全程序,使程序输出结果与输出样例一致。
补全后的代码:
package project5; 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(true); //调用父类
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
super.method(); //调用父类method方法
}
}
运行结果:
3. 实验总结:
此次实验:
1、调试了private protected public 默认四种修饰符的使用特点,子类拥有父类的所有属性和方法,但父类的私有属性和方法,子类是无法直接访问到的。即只是拥有,但是无法使用。2、Object类即所有类的父类,它描述的所有方法子类都可以使用。如果一个类没有特别指定父类,那么默认继承自Object类。实验中学习了常用的API,public String toString():返回该对象的字符串表示。public boolean equals(Object obj):指示其他某个对象是否与此对象“相等”。3、学习了ArrayList类的定义方法及用途,通过add方法添加元素,int size()返回元素个数…(不太懂得这部分)4、使用enum关键字来定义枚举类,枚举的构造方法是私有的,所以不可以new对象,有对象要在它的内部实例化,如enum Size{ SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");} new Size时传参数为SMALL("S")。可变参数用法……
最新文章
- 别踩白块儿游戏源码Android版
- 【JSOI2007】麻将 bzoj 1028
- 【HOW】在InfoPath中如何为浏览和编辑模式设置不同的视图
- java温故系列之环境配置
- java前三本基础知识总结
- 【spring bean】bean的配置和创建方式
- sudo权限集中管理用法
- 《转》高级Unix命令
- squid3.0 隐藏 hearder 设置
- leetcode 最大矩形和
- 我的C# - Web - DAL- DBHelper.cs
- Cocos2d-xvision3.0加载失败,和,Vs2012环境搭建
- yum安装phpmyadmin小问题
- 如何在MacBook的以太网端口上成功运行DHCP服务器?
- The more... the more句型
- HDU1241 Oil Deposits 2016-07-24 13:38 66人阅读 评论(0) 收藏
- JavaScript 之 变量
- oracle 数据库记录
- MyBatis 是一款优秀的持久层框架
- arcgis android 中shapefile的加载