java03

1.什么是面向对象的封装性,Java中是如何实现封装性的?试举例说明。

setter和getter方法。

class Employee { 
  private int empno ; 
  private String ename ; 
  private double sal ; 
  private double comm ; 
  public Employee(){}  // 无参构造方法 
  public Employee(int empno,String ename,double sal,double comm){ 
      this.empno = empno ; 
      this.ename = ename ; 
      this.sal = sal ; 
      this.comm = comm ; 
  } 
 public void setEmpno(int empno) {
    this.empno = empno;   
    } 
public int getEmpno() {
    return empno;    
    }
public void setEname(String ename) {
    this.ename = ename;  
    }
public String getEname() {
    return ename;   
    } 
public void setSal(double sal) {
    this.sal = sal;   
    }
public double getSal() {
    return sal;   
    } 
public void setComm(double comm) {
    this.comm = comm;    
    } 
public double getComm() {
    return comm;    
    }

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

不能因为private具有封装性需要setter和getter方法进行赋值和输出。

(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不为stztic声明无法被方法调用。

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

class Book{
    int bookId;
    String bookName;
    double price;
    static int y = 1000;  
    
     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 Book(){
    	
    }
    public Book(String a,Double b){
    	setBookName(a);
    	setPrice(b);
    	y++;
    	bookId=y;
    }
    public String toString(){
    	return  bookId+"/t"+getBookName()+"      "+getPrice();
    }
    public static int totalBook(){
    	return (y-1000);
    }
}
public class Question{
    public static void main(String args[]){ 
        Book[] books = {new Book("c语言程序设计",29.3),
                        new Book("数据库原理",30.0),
                        new Book("Java学习笔记",68.0)};
        System.out.println("图书总数为:"+ Book.totalBook()); 
        for(Book book:books){
            System.out.println(book.toString());
        }
    }   
}

4.什么是单例设计模式?它具有什么特点?用单例设计模式设计一个太阳类Sun。

class Sun{
private static Sun instance = new Sun() ;
private Sun(){ 

}
public static Sun getInstance(){
	return instance ;
}
}

5.理解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
一个传的是副本一个传的是地址,地址那个可以改地址里的内容副本改了不会影星到原来的数值。
只是传过来一个值,然后生成了一个str的副本和一开始的str的值一样。想改变原来的值可以用this.str。

(三)代码托管

http://git.oschina.net/shuoge/java03

posted @ 2017-03-31 20:04  朔朔  阅读(232)  评论(2)    收藏  举报