(一)学习总结
1.什么是构造方法?什么是构造方法的重载?下面的程序是否可以通过编译?为什么?
public class Test {
public static void main(String args[]) {
Foo obj = new Foo();
}
}
class Foo{
int value;
public Foo(int intValue){
value = intValue;
}
}
构造方法名字与类名完全相同,没有返回类型,构造方法的调用是在创建一个对象时使用new操作进行的。构造方法的作用是初始化对象。
构造方法重载:方法名称相同,但是参数类型和数量不一样。
不能编译
方法有参数,但是初始化的时候没有实参。
2.运行下列程序,结果是什么?分析原因,应如何修改。
public class Test {
public static void main(String[] args) {
MyClass[] arr=new MyClass[3];
arr[1].value=100;
}
}
class MyClass{
public int value=1;
}
没结果
没对arr实例化
3.运行下列程序,结果是什么?说明原因。
public class Test {
public static void main(String[] args) {
Foo obj1 = new Foo();
Foo obj2 = new Foo();
System.out.println(obj1 == obj2);
}
}
class Foo{
int value = 100;
}
结果是false
比较的时候是对地址进行比较,所以是false。
4.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。
封装性
封装性是面向对象的方法所应遵循的一个重要原则,它有两个含义:一是值把对象的属性和行为看成一个密不可分的整体,将这两者“封装”在一个不可分割的独立单位(即对象)中,另一层含义值“信息隐蔽”,把不需要让外界知道的信息隐藏起来,有些对象的属性及行为允许外界用户知道或使用,但不允许更改,而另一些属性或行为。则不允许外界知道,或只允许适用对象的功能,而尽可能隐蔽对象的功能实现细节 (课本93页)
如何实现封装
属性封装:private 属性类型 属性名称;
方法封装:private 方法返回值 方法名称{}
只要是被封装的属性,必须通过setter和getter方法设置和取得。
用private声明的属性或方式只能在其类的内部被调用,而不能在外部调用。
5.阅读下面程序,分析是否能编译通过?如果不能,说明原因。
(1)
class A{
private int secret = 5;
}
public class Test{
public static void main(String args[]){
A a = new A();
System.out.println(a.secret++);
}
}
不能,private使得属性secret变为私有,不能由对象直接进行访问
(2)
public class Test{
int x = 50;
static int y = 200;
public static void method(){
System.out.println(x+y);
}
public static void main(String args[]){
Test.method();
}
}
不能,都为static才可以
6.使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出测试类代码和Book类的部分代码,将代码补充完整。
补充后如下:
package zuoye;
class Book {
int bookId;
String bookName;
double price;
static // 声明静态变量
int i = 1000;
// 定义静态代码块对静态变量初始化
// 构造方法
public Book(String bookName, double price) {
i++;
this.bookName = bookName;
this.price = price;
bookId = i;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// 定义方法求图书总册数
public static int totalBook() // 定义方法求图书总册数
{
return i - 1000;
}
// 重写toString方法
public String toString() // 重写toString方法
{
return "书名:" + this.getBookName() + "
" + "价格:" + this.getPrice() + "
" + "编号:" + bookId + "
";
}
}
public class Zuoye1 {
public static void main(String args[]) {
Book[] books = { new Book("c语言程序设计", 29.3), new Book("数据库原理", 30), new Book("Java学习笔记", 68) };
System.out.println("图书总数为:" + Book.totalBook());
for (Book book : books) {
System.out.println(book.toString());
}
}
}
7.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。
单个类在使用过程中只有一个实例,单利模式的类一个类只有一个实例。即一个类只有一个对象实例。
class Sun {
private String Sun;
public void Sun() {
}
public void sun(String Sun) {
this.Sun=Sun;
}
public String getSun() {
return Sun;
}
public void setSun(String sun) {
Sun = sun;
}
}
8.理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由。
public class Test {
String str = new String("你好 ");
char[] ch = { 'w','o','l','l','d' };
public static void main(String args[]) {
Test test = new Test();
test.change(test.str, test.ch);
System.out.print(test.str);
System.out.print(test.ch);
}
public void change(String str, char ch[]) {
str = "hello";
ch[0] = 'W';
}
}
结果是:你好 Wolld
ch[]和str传递的内容不一样,前者传递的是地址,后者传递的是值。
9.总结:
理解面向对象和面向过程的区别
对象实例化
方法重载
(二)实验总结
本次实验包括实验二和实验三两次的内容:
1.用面向对象思想完成评分系统
测试类:
package shiyan1;
import java.util.Arrays;
import java.util.Scanner;
public class Shiyan {
public static void main(String args[]){
int l=0;
Scanner a = new Scanner(System.in);
System.out.println("输入选手数:");
int n = a.nextInt();
Player []m= new Player[n];
System.out.println("输入评委数:");
int pws = a.nextInt();
int score[]=new int [pws];
do {
Pingwei p = new Pingwei(pws);
System.out.println("输入选手编号:");
String bianhao = a.next();
System.out.println("输入选手姓名:");
String name = a.next();
m[l] = new Player(bianhao,name);
System.out.println("请为" + bianhao + "号选手打分");
for(int i=0;i<score.length;i++) {
score[i]=a.nextInt();
}
p.InputScore(score);
m[l].setgrade(p.ave());
System.out.print("去掉一个最高分:"+p.getx()+"
去掉一个最低分:"+p.gety()+"
");
System.out.print(m[l].getno());
System.out.printf("最终得分为:%.2f
",m[l].getgrade());
l++;
}while(l<n);
Arrays.sort(m);
for(int i=0;i<m.length;i++) {
System.out.println(m[i].toString());
}
}
}
评委类:
package shiyan1;
import java.util.*;
public class Pingwei {
private int max,min;
private double z;
private int score[] = null;
private int num;
public Pingwei() {}
public Pingwei(int num) {
this.num = num;
score=new int [num];
// System.out.println(num);
}
public void setnum(int num) {
this.num = num;
}
public int getnum() {
return num;
}
public void InputScore(int score[]) {
int index=0;
for (int i : score) {
this.score[index++] = i;
}
Arrays.sort(score);
max=score[num-1];
min=score[0];
ave();
}
public int getx() {
return max;
}
public int gety() {
return min;
}
public double ave() {
int sum=0;
double average;
for(int i=1;i<num-1;i++) {
sum+=score[i];
}
average=(double)sum/(num-2);
return average;
}
public String toString() {
return null;
}
}
选手类:
package shiyan1;
public class Player implements Comparable<Player> {
private String no;
private String name;
private double grade;
public Player() {
}
public Player(String no, String name) {
this.no = no;
this.name = name;
}
public void setno(String no) {
this.no = no;
}
public String getno() {
return no;
}
public void setname(String name) {
this.name = name;
}
public String getname() {
return name;
}
public void setgrade(double grade) {
this.grade = grade;
}
public double getgrade() {
return grade;
}
public String toString() {
return "编号"+this.no+" 姓名"+this.name+" 成绩"+this.grade;
}
public int compareTo(Player play) {
if(this.grade>play.grade) {
return -1;
}
else if(this.grade<play.grade) {
return 1;
}
else
{
return 0;
}
}
}
2.Email验证
package zifuchuan;
import java.util.*;
public class Email {
public static void main(String args[]) {
int flag = 0;
Scanner str = new Scanner(System.in);
String str1 = str.nextLine();
// System.out.println(str1);
judge(str1);
}
public static void judge(String str) {
int a, b;
String s;
a = str.indexOf('@');
b = str.indexOf('.');
s = str.substring(b + 1);
if ((a != -1 && b != -1 && a < b && a != 0) && (s.equals("com") || s.equals("cn") || s.equals("net")
|| s.equals("gov") || s.equals("edu") || s.equals("org"))) {
System.out.println("地址合法");
} else {
System.out.println("地址不合法");
}
}
}
3.查找子串
package zifuchuan;
import java.util.*;
public class Cishu {
public static void main(String args[]) {
Scanner str = new Scanner(System.in);
System.out.println("输入字符串:");
String str1 = str.nextLine();
System.out.println("输入查找的子串:");
String str2 = str.nextLine();
chazhao(str1, str2);
}
public static void chazhao(String str1, String str2) {
int a, b = 0, i = 0;
while ((a = str1.indexOf(str2, i)) != -1) {// 从指定位置查找子串
i = a + str2.length();//
b++;
}
System.out.println("子串有" + b + "个");
}
}
4.统计文件
package zifuchuan;
import java.util.*;
public class Wenjian {
public static void main(String args[]) {
String c;
Scanner str = new Scanner(System.in);
System.out.println("输入文件和扩展名,用,隔开");
String str1 = str.nextLine();
String s[] = str1.split(",");
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
System.out.println("首字母大写输出:");
for (int i = 0; i < s.length; i++) {// 取出首字母大写
char b = s[i].charAt(0);// 取出首字母
b = (char) (b - 32);// 将首字母变大写
c = s[i].substring(1);// 去除首字母
System.out.println(b + c);// 连接
}
String m[] = new String[s.length];// 存后缀
for (int i = 0; i < s.length; i++) {
int y = s[i].lastIndexOf(".");// 查找最后一个点的位置
m[i] = s[i].substring(y + 1);// 将最后一个点后的字符串存到m
}
System.out.println("统计如下:");
System.out.println("文件" + " " + "数量");
int sz[] = new int[s.length];
for (int i = 0; i < m.length; i++) {
if (m[i].equals("N") != true) {// 自己算一次,先++
sz[i]++;
}
for (int j = i + 1; j < m.length; j++) {// 在m数组查找,有相同的,把相同的换掉
if (m[i].equals(m[j])) {
m[j] = "N";
sz[i]++;
}
}
if (m[i].equals("N") != true) {
System.out.println(m[i] + " " + sz[i]);
}
}
}
}
5.类的设计
日期类:
package zhigong;
public class Data {
private int year;
private int month;
private int day;
public void Data() {
}
public Data(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void setyear(int year) {
this.year = year;
}
public int getyear() {
return year;
}
public void setmonth(int month) {
this.month = month;
}
public int getmonth() {
return month;
}
public void setday(int day) {
this.day = day;
}
public int getday() {
return day;
}
public String tostring() {
return this.year + "-" + this.month + "-" + this.day;
}
}
部门类:
package zhigong;
public class Bumen {
private int Bmno;
private String Bmname;
private Zhigong emps[];
private Zhigong manager1;
public Bumen() {
}
public Bumen(int Bmno, String Bmname) {
this.Bmno = Bmno;
this.Bmname = Bmname;
}
public int getBmno() {
return Bmno;
}
public void setBmno(String bmno) {
this.Bmno = Bmno;
}
public String getBmname() {
return Bmname;
}
public void setBmname(String bmname) {
this.Bmname = Bmname;
}
public Zhigong getManager1() {
return manager1;
}
public void setManager1(Zhigong manager1) {
this.manager1 = manager1;
}
public Zhigong[] getEmps() {
return emps;
}
public void setEmps(Zhigong[] emps) {
this.emps = emps;
}
public String tostring() {
return "部门编号:" + this.Bmno + "
部门名称:" + this.Bmname;
}
}
职工类:
package zhigong;
public class Zhigong {
private int no;
private String name;
private String sex;
private int gzsj;
private Data shengri;
private Bumen gzbm1;
public Zhigong(int no, String name, String sex, int gzsj) {
this.no = no;
this.name = name;
this.sex = sex;
this.gzsj = gzsj;
}
public Zhigong() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getGzsj() {
return gzsj;
}
public void setGzsj(int gzsj) {
this.gzsj = gzsj;
}
public Data getShengri() {
return shengri;
}
public void setShengri(Data shengri) {
this.shengri = shengri;
}
public Bumen getGzbm1() {
return gzbm1;
}
public void setGzbm1(Bumen gzbm1) {
this.gzbm1 = gzbm1;
}
public String tostring() {
return "
员工编号:" + this.no + "
姓名:" + this.name + "
性别:" + this.sex + "
生日:" + shengri.tostring()
+ "
参加工作时间:" + this.gzsj+"
";
}
}
测试类:
package zhigong;
public class Test {
public static void main(String[] args) {
Bumen bm1 = new Bumen(1, "中路部");
Bumen bm2 = new Bumen(2, "下路部");
Zhigong zg1 = new Zhigong(1001, "阿狸", "女", 2000);// 员工实例化
Zhigong zg2 = new Zhigong(1002, "安妮", "女", 2001);
Zhigong zg3 = new Zhigong(1003, "吉格斯", "男", 2002);
Zhigong zg4 = new Zhigong(1004, "拉克丝", "女", 2003);
Zhigong zg5 = new Zhigong(1005, "亚索", "男", 2004);
Zhigong zg6 = new Zhigong(1006, "崔丝塔娜", "女", 2005);
Zhigong zg7 = new Zhigong(1007, "薇恩", "女", 2006);
Zhigong zg8 = new Zhigong(1008, "伊泽瑞尔", "男", 2007);
Zhigong zg9 = new Zhigong(1009, "德莱文", "男", 2008);
Zhigong zg10 = new Zhigong(1010, "卢锡安", "男", 2009);
bm1.setManager1(zg1);
bm2.setManager1(zg6);
Data sr1 = new Data(1997, 1, 1);
Data sr2 = new Data(1997, 2, 2);
Data sr3 = new Data(1997, 3, 3);
Data sr4 = new Data(1997, 4, 4);
Data sr5 = new Data(1997, 5, 5);
Data sr6 = new Data(1997, 6, 6);
Data sr7 = new Data(1997, 7, 7);
Data sr8 = new Data(1997, 8, 8);
Data sr9 = new Data(1997, 9, 9);
Data sr10 = new Data(1997, 10, 10);
zg1.setShengri(sr1);
zg2.setShengri(sr2);
zg3.setShengri(sr3);
zg4.setShengri(sr4);
zg5.setShengri(sr5);
zg6.setShengri(sr6);
zg7.setShengri(sr7);
zg8.setShengri(sr8);
zg9.setShengri(sr9);
zg10.setShengri(sr10);
zg1.setGzbm1(bm1);
zg2.setGzbm1(bm1);
zg3.setGzbm1(bm1);
zg4.setGzbm1(bm1);
zg5.setGzbm1(bm1);
zg6.setGzbm1(bm2);
zg7.setGzbm1(bm2);
zg8.setGzbm1(bm2);
zg9.setGzbm1(bm2);
zg10.setGzbm1(bm2);
bm1.setEmps(new Zhigong[] { zg1, zg2, zg3, zg4, zg5 });
bm2.setEmps(new Zhigong[] { zg6, zg7, zg8, zg9, zg10, });
// System.out.println(zg1.getName() + "所在部门:
" + zg1.getGzbm1().tostring()+"
");
// System.out.println(zg1.getName() + "所在部门部门经理:" + zg1.getGzbm1().getManager1().tostring()+"
");
System.out.println(bm1.getBmname() + "部门经理:" + bm1.getManager1().tostring());
System.out.println(bm1.getBmname() + "员工:");
for (int i = 0; i < bm1.getEmps().length; i++) {
System.out.println(bm1.getEmps()[i].tostring());
}
System.out.println(bm2.getBmname() + "部门经理:" + bm2.getManager1().tostring());
System.out.println(bm2.getBmname() + "员工:");
for (int i = 0; i < bm2.getEmps().length; i++) {
System.out.println(bm2.getEmps()[i].tostring());
}
}