实验三

(一)学习总结

1.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。
例:管理充值卡的程序。(卡号、余额、红利点数)

class CashCard {
String number;
int balance;
int bonus;
}

2.阅读下面程序,分析是否能编译通过?如果不能,说明原因。

(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++);
    }
}

不能通过,原因是secret是在class里面封装的,所以不能在外面调用,如果想要调用的话,可以写一个gtes()函数

package test;

class A{
    private int secret = 5;
    public int gets(){
    	return secret;
    }
}
public class test{
    public static void main(String args[]){
        A a = new A();
        System.out.println(a.gets());
    }
}

(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();
    }
}

不能通过,x必须是static类型,不加static是实例变量,在类中对变量赋值都要是static.

package test;

public class test{
    static int x = 50;
    static int y = 200;
    public static void method(){
        System.out.println(x+y);
    }
    public static void main(String args[]){
        test.method();
    }
}

3 . 使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。下面给出了测试类代码和Book类的部分代码,将代码补充完整。

class Book{
    int bookId;
    String bookName;
    double price;
    // 声明静态变量
    
    //定义静态代码块对静态变量初始化
    
    //构造方法
    
     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;
    }  
    //定义方法求图书总册数
    
    //重写toString方法
    
}
public class Test{
    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());
        }
    }   
}
package test;

class Book{
    int bookId;
    String bookName;
    double price;
    // 声明静态变量
    
    //定义静态代码块对静态变量初始化
    
    //构造方法
    public Book(int bookId,String bookName){
    	this.bookId=bookId;
    	this.bookName=bookName;
    }
     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 ;
	}
    
    //重写toString方法
    
}
public class test{
    public static void main(String args[]){ 
        Book[] books = {new Book(29,"c语言程序设计"),
                        new Book(15,"数据库原理"),
                        new Book(17,"Java学习笔记")};
        System.out.println("图书总数为:"+ Book.totalBook()); 
        for(Book book:books){
            System.out.println(book.toString());
        }
    }   
}

4.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。
单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。
优点

一、实例控制

单例模式会阻止其他对象实例化其自己的单例对象的副本,从而确保所有对象都访问唯一实例。

二、灵活性

因为类控制了实例化过程,所以类可以灵活更改实例化过程。

折叠 缺点

一、开销

虽然数量很少,但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销。可以通过使用静态初始化解决此问题。

二、可能的开发混淆

使用单例对象(尤其在类库中定义的对象)时,开发人员必须记住自己不能使用new关键字实例化对象。因为可能无法访问库源代码,因此应用程序开发人员可能会意外发现自己无法直接实例化此类。

三、对象生存期

不能解决删除单个对象的问题。在提供内存管理的语言中(例如基于.NET Framework的语言),只有单例类能够导致实例被取消分配,因为它包含对该实例的私有引用。在某些语言中(如 C++),其他类可以删除对象实例,但这样会导致单例类中出现悬浮引用。

class Sun{
    private static Sun instance = new Sun() ;
    private Sun(){
    }
    public static Sun getInstance(){
        return instance ;
    }
    public void print(){
        Ststem.out.println("Hello World!");
    }
}
public class Test{
    public static void main(String args[]){
        Sun s1 = Sun.getInstance() ;
        s1.print();
    }
}

5.理解Java参数传递机制,阅读下面的程序,运行结果是什么?说明理由。

public class Test {
    String str = new String("你好  ");
    char[] ch = { 'w','o','r','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';
    }
}

运行结果:你好 World
ch[0]中“W”代替了“w”.
(二)实验总结
1.程序设计思路:首先分别设计4个类,一个日期类,一个职工类,一个部门类,一个测试类,对它们的属性进行封装,再进行实现关联----对象引用,设计一个部门只有一个经理,员工在部门中保存员工的信息,最后用toString的方法输出。
无问题。

class Day{
	private Employee birthday;
	public String toString(){
		return  birthday.birth.substring(0,3)+"-"+birthday.birth.substring(5,6)+"-"+birthday.birth.substring(8,9);
	}
}
class Employee{
	private int eno;
	private String ena;
	private String sex;
	public  String birth;
	private String jobday;
	private Dept dept;
	public Employee (int eno,String ena,String sex,String jobday,String birth){
		this.eno=eno;
		this.ena=ena;
		this.sex=sex;
		this.birth=birth;
		this.jobday=jobday;
	}
	public int getEno() {  return eno;  }
    public void setEno(int eno) {  this.eno = eno;  }
    public String getEname() {  return ena;  }
    public void setEname(String ena) {  this.ena = ena;  }
    public String getBirthday() {  return birth;  }
    public void setBirthday(String birthday) {  this.birth = birthday;  }
    public String getSex() {  return sex;  }
    public void setSex(String sex) {  this.sex = sex;  }
    public Dept getDept() {
 	return dept;
    }
    public void setDept(Dept dept) {
 	this.dept = dept;
    }
    public String toString (){
    	return "员工编号:"+this.eno+"   姓名:"+this.ena+"  性别:"+this.sex+"	生日:"+this.birth+"入职日期:"+this.jobday ;
    }
}
class Dept{
	private String dno;
	private String dname;
	private Employee manager;
	private Employee emps[];
	public Dept(String dno, String dname) {
	this.dno = dno;
	this.dname = dname;
	}     
    public Employee getManager() {
	return manager;
    }
    public void setManager(Employee manager) {
    this.manager = manager;
   }
   
    public Employee[] getEmps() {
           return emps;
    }
     public void setEmps(Employee[] emps) {
          this.emps = emps;
    }
	public String getDno() {   return dno;   }
	public void setDno(String dno) {   this.dno = dno;  }
	public String getDname() {   return dname;   }
	public void setDname(String dname) {   this.dname = dname;  }
	public String toString(){
	return "部门编号:"+this.dno+"  部门名称:"+this.dname ;
	}
}
public class test{
	public static void main(String []args){
		Dept dept1=new Dept("01","技术部");
		Dept dept2=new Dept("02","销售部");
		Dept dept3=new Dept("03","设计部");
		Employee emp1=new Employee(0001,"张明","男","2002","1990-10-08");			//经理
		Employee emp2=new Employee(0002,"王强","男","2008","1989-9-08");
		Employee emp3=new Employee(0003,"李红","女","2009","1990-11-26");			//经理
		Employee emp4=new Employee(0004,"刘明","男","2005","1991-05-08");
		Employee emp6=new Employee(0005,"赵双","男","2008","1990-08-27");
		Employee emp5=new Employee(0006,"马博","男","2007","1991-06-18");			//经理
		dept1.setManager(emp1);														//设置技术部经理
		dept2.setManager(emp3);														//设置销售部经理
		dept3.setManager(emp5);														//设置设计部经理	
		emp2.setDept(dept1);
		emp4.setDept(dept2);
		emp6.setDept(dept3);
		dept1.setEmps(new Employee[]{emp1,emp2});
		dept2.setEmps(new Employee[]{emp2,emp3});
		dept3.setEmps(new Employee[]{emp5,emp6});	
		System.out.println(emp2.getEname()+"所在部门:" +emp2.getDept(). toString());
		System.out.println(emp2.getEname()+"的部门经理:"+emp2.getDept().getManager(). toString());
		System.out.println(emp4.getEname()+"所在部门:" +emp4.getDept(). toString());
		System.out.println(emp4.getEname()+"的部门经理:"+emp4.getDept().getManager(). toString());
		System.out.println(emp6.getEname()+"所在部门:" +emp6.getDept(). toString());
		System.out.println(emp6.getEname()+"的部门经理:"+emp6.getDept().getManager(). toString());
		System.out.println(dept1.getDname()+"的部门经理:"+dept1.getManager(). toString());
		System.out.println("*********************************************");
		System.out.println(dept1.getDname()+"员工:");
		for(int i=0;i<dept1.getEmps().length;i++){
			System.out.println(dept1.getEmps()[i]. toString());
				}
		System.out.println(dept2.getDname()+"的部门经理:"+dept2.getManager(). toString());
		System.out.println(dept2.getDname()+"员工:");
		for(int i=0;i<dept2.getEmps().length;i++){
			System.out.println(dept2.getEmps()[i]. toString());
				}
		System.out.println(dept3.getDname()+"的部门经理:"+dept3.getManager(). toString());
		System.out.println(dept3.getDname()+"员工:");
		for(int i=0;i<dept3.getEmps().length;i++){
			System.out.println(dept3.getEmps()[i]. toString());
				}
	}
}

posted on 2017-04-06 22:45  一开Stephen  阅读(167)  评论(1)    收藏  举报