唐敬博-201871010118 《面向对象程序设计(java)》第七周学习总结
在博客园撰写博客(随笔),总结7周实验内容,作业格式要求如下:
- 博文名称:学号-姓名《面向对象程序设计(java)》第七周学习总结(1分)
- 博文正文开头格式:(2分)
项目 |
内容 |
这个作业属于哪个课程 |
https://www.cnblogs.com/nwnu-daizh/ |
这个作业的要求在哪里 |
https://www.cnblogs.com/nwnu-daizh/p/11435127.html |
作业学习目标 |
|
随笔博文正文内容包括:
实验内容和步骤
实验1:(20分)
实验内容:在“System.out.println(...);”语句处按注释要求设计代码替换...,观察代码录入中IDE提示,以验证四种权限修饰符的用法
代码如下:
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类的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();
parent.pMethod2();
//分别尝试用parent调用Paren类的方法、用son调用Son类的方法
}
}
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无修饰符修饰的方法"
);
}
}
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类的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无修饰符修饰的方法"
);
}
}
调用方法得到的结果:
调用方法四得到的结果;
得到的结果: 权限控制表
修饰词 本类 同一个包的类 继承类 其他类
private √ × × ×
无(默认) √ √ × ×
protected √ √ √ ×
public √ √ √ √
实验2:测试程序1(15分)
l 运行教材程序5-8、5-9、5-10,结合程序运行结果理解程序(教材174页-177页);
实验代码如下:
1,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 +
"]"
;
}
}
2,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());
}
}
删除程序中Employee类、Manager类中的equals()、hasCode()、toString()方法,背录删除方法,在代码录入中理解类中重写Object父类方法的技术要点。
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 boolean equals(Object otherObject) {
// TODO Auto-generated method stub
if(this==otherObject) return true;
if(this==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);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return Objects.hash(name,salary,hireDay);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return getClass().getName()+"[name="+name+",salary="+salary+",hireday="+hireDay+"]";
}
}
Manager类重写之后代码如下:
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());
}
}
(3)EmployeeTest类代码如下:
package equalspublic 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);
// TODO Auto-generated constructor stub
bonus = 0;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public double getSalary() {
// TODO Auto-generated method stub
double baseSalary= super.getSalary();
return baseSalary+bonus;
}
@Override
public boolean equals(Object otherObject) {
// TODO Auto-generated method stub
if(!super.equals(otherObject)) return false;
Manager other=(Manager)otherObject;
return bonus==other.bonus;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode()+17*new Double(bonus).hashCode();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString()+"[bonus="+bonus+"]";
}
}
运行结果如下:
实验2:测试程序2(15分)
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>();
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)
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());
}
}
(2)
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.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;
}
}
运行结果如下:
实验2:测试程序3(15分)
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);
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:测试程序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);
}
}
运行结果如下:
实验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分)
我觉得在学习理论知识时,可以跟着老师的思路,觉得已经掌握了知识点,但在实验过程中发现自己还是不会运用,肯平时没有多看书,对基础知识掌握不到位。对于老师设计的简单编程题,由于自己的知识还不够,因此几乎不能独立的做出完整的实验。所以在以后的学习中,我会多看翁恺老师的视频以及老师的课件来学习编程,来写出完整的程序。
唐敬博-201871010118 《面向对象程序设计(java)》第七周学习总结的更多相关文章
- 201771010134杨其菊《面向对象程序设计java》第九周学习总结
第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...
- 201871010132-张潇潇《面向对象程序设计(java)》第一周学习总结
面向对象程序设计(Java) 博文正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...
- 扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结
<面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...
- 201871010124 王生涛《面向对象程序设计JAVA》第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://edu.cnblogs.com/campus/xbsf/ ...
- 杨其菊201771010134《面向对象程序设计Java》第二周学习总结
第三章 Java基本程序设计结构 第一部分:(理论知识部分) 本章主要学习:基本内容:数据类型:变量:运算符:类型转换,字符串,输入输出,控制流程,大数值以及数组. 1.基本概念: 1)标识符:由字母 ...
- 201871010115——马北《面向对象程序设计JAVA》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201777010217-金云馨《面向对象程序设计(Java)》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201871010132——张潇潇《面向对象程序设计JAVA》第二周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201771010123汪慧和《面向对象程序设计Java》第二周学习总结
一.理论知识部分 1.标识符由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字.标识符可用作: 类名.变量名.方法名.数组名.文件名等.第二部分:理论知识学习部分 2.关键字就是Java语言 ...
- 20175314 《Java程序设计》第七周学习总结
20175314 <Java程序设计>第七周学习总结 教材学习内容总结 第八章:常用实用类 String()类代表字符串:Java 程序中的所有字符串字面值(如 "abc&quo ...
随机推荐
- HTML与CSS学习笔记(5)
1.文字阴影?(针对文字) text-shadow: 例如 text-shadow: 10px 10px 10px blue;四个值分别是 x y blur color blur表示模糊值,越大越模糊 ...
- C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符
#include <iostream> // overloading "operator == " outside class // == 是二元操作符 /////// ...
- lua 7 运算符
转自:http://www.runoob.com/lua/lua-miscellaneous-operator.html Lua提供了以下几种运算符类型: 算术运算符 关系运算符 逻辑运算符 其他运算 ...
- vue.js操作元素属性
vue动态操作div的class 看代码: <!doctype html> <html lang="en"> <head> <meta c ...
- 基于Apache和tomcat实现负载均衡
1.基于Apache和tomcat实现负载均衡 准备三个虚拟机一个安装Apache两个安装Tomcat 关闭防火墙 systemctl stop firewalld Iptabled -F Seten ...
- luogu1368 工艺
题目链接 思路 \(SAM\)练手题,将原串重复一遍插入到\(SAM\)中,然后贪心走长度为n的一个路径即可. 不用担心会直接走到终点,根据\(SAM\)的构造方式可以发现会先走到前面的路径. 代码 ...
- Unity编辑器扩展学习 转载
https://www.xuanyusong.com/archives/category/unity/unity3deditor 1 using UnityEngine; public class T ...
- hw笔试题-02
#include<stdio.h> #include<string.h> typedef struct { char *mem; char len; }m_table_t; i ...
- github README.md创建不了
在项目主页上,点击‘Add a README’按钮,如下图: 进入编辑界面,编辑好内容后,提交按钮的状态为灰化不可点击,如下图: 不知道为什么会出现这种情况,但是我无意中点击了Ctrl+Enter竟然 ...
- ubuntu18.04下安装无线网卡驱动心得
联想Lenovo的笔记本,装完系统wifi显示找不到适配器. lspci | grep Wireless 显示无线网卡类型为博通的BCM43162. 网上一查,果然有问题. apt install f ...