用java面向对象写atm_Java第三次作业——面向对象基础

《Java第三次作业——面向对象基础》

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

面向对象的封装性就是指对外部不可见,为了避免用对象直接访问类的属性,所以将类中的属性封装。

为实现封装性,常将类的成员变量声明为private,再通过public的方法来对这个变量进行访问。

class Person{

private String name;

private int age;

public void tell(){

System.out.println("姓名:"+name+,年龄"+age);

}

}

public class Text{

public static void main(String arg[]){

Person per=new Person();

per.name="张三";

per.age=-30;

per.tell();

}

}

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进行封装后,A.secert不可视。

(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是非静态字段,不能引用非静态字段。

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

补充后:

class Book{

private static int counts;

int bookId;

String bookName;

double price;

public Book(){

}

public Book(int bookId,String bookName,double price){

this.bookId=bookId;

this.bookName=bookName;

this.price=price;

}

public int getBookId (){

return bookId;

}

public void setBookId(int bookId){

this.bookId=bookId;

}

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() {

counts++;

return counts;

}

public String toString(){

return"图书信息:\n"+"\t图书编号:"+bookId+"\n"+"\t图书名称:"+bookName+"\n"+"\t价格:"+price+"\n";

}

public class Text{

public static void main(String args[]){

Book[] books = {new Book(01,"c语言程序设计",29.3),

new Book(02,"数据库原理",30),

new Book(03,"Java学习笔记",68)};

System.out.println("图书总数为:"+ Book.totalBook());

for(Book book:books){

System.out.println(book.toString());

}

}

}

}

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

一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例。

特点: 1,一个类只能有一个实例

2,自己创建这个实例

3,整个系统都要使用这个实例

class Sun{

private static Sun instance=new Sun();

private Sun(){

}

public static Sun getInstance(){

return instance;

}

public void print(){

System.out.println("太阳");

}

}

public class Text{

public static void main(String arg[]){

Sun s1=Sun.getInstance();

Sun s2=Sun.getInstance();

Sun s3=Sun.getInstance();

s1.print();

s2.print();

s3.print();

}

}

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';

}

}

运行结果:你好 world

应为参数传递中str传递的是值,ch[]传递的是地址,所以运行完后只是将地址传递给了变量所以运行结果为你好 world。

(二)实验总结

1.职工表

程序设计思路:用构造方法,进行封装,然后分别写出三个累,最后将三个类联系在一起写出测试类输出。

问题:对部门以及职工赋值是出现问题

原因:类名没有统一

解决方案:修改类名

(三)代码托管

http://git.oschina.net/hebau_cs15/Java-CS02yxy/tree/master

7a7ea6e0c24adadb97c27d14c311a33e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值