封裝作業

封裝和多態作業

一、 選擇題

1.

使用權限修飾符(  b  )修飾的類的成員變量和成員方法,可以被當前包中所有類訪問,也可以被它的子類(同一個包以及不同包中的子類)訪問。(選擇一項)

 

 

 

 

A

public

 

B.

protected

 

C.

默認

 

D.

private

 

2.

給出如下代碼,如何使成員變量m被方法fun()直接訪問( c   )。(選擇一項)

 

class Test {

private int m;

public static void fun() {

}

}

 

 

 

 

A

將private int m 改爲protected int m

 

B.

將private int m 改爲public int m

 

C.

將private int m 改爲static int m

 

D.

將private int m 改爲int m

 

二、 判斷題

1. 使用public修飾的成員屬性和方法可以被當前項目中所有包的所有類訪問。( dui  )

2. 類的方法通常設爲public,而類的實例變量一般也設爲public。(  cuo  

3. 與未加訪問控制符的缺省情況相比,publicprotected修飾符擴大了類及其屬性和方法的被訪問範圍,private修飾符則縮小了這種範圍。(  dui 

4. 訪問權限是private的變量,只能在本類和與本類同一個包中的其他類使用。(  cuo 

 

三、 簡答題

1. private、默認、protectedpublic四個權限修飾符的作用

2. 同一個類內    同一個包內    不同包內、子類內     所有類

3.  Private         default         protected            public

 

四、 編碼題

1. 使用面向對象的思想,編寫自定義描述狗的信息。設定屬性包括:品種,年齡,心情,名字;方法包括:叫,跑。

要求:

1) 設置屬性的私有訪問權限,通過公有的get,set方法實現對屬性的訪問

2) 限定心情只能有“心情好”和“心情不好”兩種情況,如果無效輸入進行提示,默認設置“心情好”。

3) 設置構造函數實現對屬性賦值

4) 叫和跑的方法,需要根據心情好壞,描述不同的行爲方式。

5) 編寫測試類,測試狗類的對象及相關方法(測試數據信息自定義)

運行效果圖:

 

package test.all.pxd;

 

public class Dog {

private String type;

private int age;

private String feeling="心情好";

private String name;

public Dog(String name,String type,int age,String feeling){

this.setAge(age);;

this.setFeeling(feeling);

this.setType(type);

this.setName(name);

}

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getFeeling() {

return feeling;

}

public void setFeeling(String feeling) {

if(feeling.equals("心情好") || feeling.equals("心情不好")){

this.feeling = feeling;

System.out.println("輸入信息正確,這隻狗狗"+feeling);

}else{

System.out.println("輸入信息錯誤,這隻狗狗今天"+this.feeling);

}

}

public void shout(){

System.out.println("名字叫"+this.name+"的"+this.type+"嗚嗚的叫");

}

public void run(){

System.out.println("名字叫"+this.name+"的"+this.type+"快樂的奔跑");

}

}

 

package test.all.pxd;

 

public class TestDog {

public static void main(String[] args) {

Dog d=new Dog("小黑","貴賓犬",12,"不知道") ;

d.shout();

d.run();

    System.out.println("##############################################");

Dog g=new Dog("太子","美國牧羊犬",8,"心情不好");

g.shout();

g.run();

    }

}

 

 

2. 以面向對象的思想,編寫自定義類描述IT從業者。設定屬性包括:姓名,年齡,技術方向,工作年限, 工作單位和職務;方法包括:工作

要求:

1)  設置屬性的私有訪問權限,通過公有的get,set方法實現對屬性的訪問

2)  限定IT從業人員必須年滿15歲,無效信息需提示,並設置默認年齡爲15

3)  限定“技術方向”是隻讀屬性

4)  工作方法通過輸入參數,接收工作單位和職務,輸出個人工作信息

5)  編寫測試類,測試IT從業者類的對象及相關方法(測試數據信息自定義)

運行效果圖:

 

package test.all.pxd;

 

public class ItWorker {

private String name;

private int age;

private String techDirection;

private int workYear;

private String workUnit;

private String position;

public ItWorker(String name,int age,String techDirection,int workYear,

String workUnit,String position){

if(age<15){

System.out.println("年齡無效,以修改爲默認值15");

this.age=15;

}else{

this.age=age;

}

this.setAge(age);

this.setName(name);

this.setPosition(position);

this.setTechDirection(techDirection);

this.setWorkUnit(workUnit);

this.setWorkYear(workYear);

}

public void work(){

System.out.println("姓名: "+this.name);

System.out.println("年齡: "+this.age);

System.out.println("技術方向: "+this.techDirection);

System.out.println("工作年限: "+this.workYear);

System.out.println("目前就職於: "+this.workUnit);

System.out.println("職務是: "+this.position);

}

 

public String getName() {

return name;

}

 

public void setName(String name) {

this.name = name;

}

 

public int getAge() {

return age;

}

 

public void setAge(int age) {

this.age = age;

}

 

public String getTechDirection() {

return techDirection;

}

 

public void setTechDirection(String techDirection) {

this.techDirection = techDirection;

}

 

public int getWorkYear() {

return workYear;

}

 

public void setWorkYear(int workYear) {

this.workYear = workYear;

}

 

public String getWorkUnit() {

return workUnit;

}

 

public void setWorkUnit(String workUnit) {

this.workUnit = workUnit;

}

 

public String getPosition() {

return position;

}

 

public void setPosition(String position) {

this.position = position;

}

}

 

package test.all.pxd;

 

public class TestIW {

public static void main(String[] args) {

ItWorker i=new ItWorker("馬未龍",35,"數據庫維護",10,"騰訊實業","數據庫維護工程師");

i.work();

System.out.println("=============================================");

ItWorker w=new ItWorker("張凱",13,"Java開發",1,"鼎盛科技","Java開發工程師");

w.work();

}

}

 

 

五、 可選題

 

1. 以面向對象的思想,編寫自定義類描述圖書信息。設定屬性包括:書名,作者,出版社名,價格;方法包括:信息介紹

要求:

1) 設置屬性的私有訪問權限,通過公有的get,set方法實現對屬性的訪問

2) 限定介格必須大於10,如果無效進行提示

3) 限定作者,書名境外爲只讀屬性

4) 設計構造方法實現對屬性賦值

5) 信息介紹方法描述圖書所有信息

6) 編寫測試類,測試圖書類的對象及相關方法(測試數據信息自定)

運行效果圖:

 

 

package test.all.pxd;

 

public class Library {

private final String name;

private final String author;

private String press;

private double price;

public Library(String name1,String author1,String press,double price){

this.name=name1;

this.author=author1;

this.press=press;

if(price>10){

this.price=price;

}else{

System.out.println("價格無效,必須大於10");

}

}

public String getName() {

return name;

}

public String getAuthor() {

return author;

}

public String getPress() {

return press;

}

public void setPress(String press) {

this.press = press;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public void information(){

System.out.println("書名: "+this.name);

System.out.println("作者: "+this.author);

System.out.println("出版社: "+this.press);

System.out.println("價格: "+this.price);

}

}

 

package test.all.pxd;

 

public class TestLibrary {

 

public static void main(String[] args) {

Library l=new Library("鹿鼎記","金庸","人民文學出版社",120.0);

l.information();

System.out.println("================================");

Library b=new Library("絕代雙驕","古龍","中國長安出版社",55.5);

b.information();

}

 

}

 

2. 某公司要開發名爲”我愛購物狂”的購物網站,請使用面向對象的思想設計描述商品信息

要求:

1) 分析商品類別和商品詳細信息屬性和方法,設計商品類別類和商品詳細信息類

2) 在商品詳細信息類中通過屬性描述該商品所屬類別

3) 設置屬性的私有訪問權限,通過公有的get,set方法實現對屬性的訪問

4) 編寫測試類,測試商品類別類和商品詳細信息類的對象及相關方法(測試數據信息自定)

5) 創建包info—存放商品類別類和商品詳細信息類,創建包test—存放測試類

     參考分析思路:

商品類別類:

屬性:類別編號,類別名稱

商品詳細信息類:

屬性:商品編號,商品名稱,所屬類別,商品數量(大於0),商品價格(大於0),

方法:盤點的方法,描述商品信息。內容包括商品名稱,商品數量,商品價格,現在商品總價以及所屬類別信息

運行效果圖:

 

package info;

 

public class GoodsType {

private String typeName;

private long typeNum;

public String getTypeName() {

return typeName;

}

public void setTypeName(String typeName) {

this.typeName = typeName;

}

public long getTypeNum() {

return typeNum;

}

public void setTypeNum(long typeNum) {

this.typeNum = typeNum;

}

}

package info;

 

public class GoodsInfor {

GoodsType gt=new GoodsType();

private String goodsName;

private double price;

private int inventory;

public GoodsInfor(GoodsType gt,String goodsName,double price,int inventory){

if(inventory>0 ){

this.inventory=inventory;

}else{

System.out.println("庫存數量異常,請聯繫管理員");

}

this.gt=gt;

this.goodsName=goodsName;

this.price=price;

}

public GoodsType getGt() {

return gt;

}

public void setGt(GoodsType gt) {

this.gt = gt;

}

public String getGoodsName() {

return goodsName;

}

public void setGoodsName(String goodsName) {

this.goodsName = goodsName;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public int getInventory() {

return inventory;

}

public void setInventory(int inventory) {

this.inventory = inventory;

}

public void print(){

System.out.println("商品名稱: "+this.goodsName);

System.out.println("所屬類別: "+this.gt.getTypeName());

System.out.println("庫存數量: "+this.inventory);

System.out.println("商品售價: "+this.price);

System.out.println("商品總價: "+this.price*this.inventory);

}

}

 

package test.goods;

 

import info.GoodsInfor;

import info.GoodsType;

 

public class TestGoods {

 

public static void main(String[] args) {

GoodsType gt=new GoodsType();

gt.setTypeName("洗髮水");

GoodsInfor g=new GoodsInfor(gt,"潘婷洗髮水400ml",40.5,16);

g.print();

System.out.println("##################################");

gt.setTypeName("洗髮水");

GoodsInfor i=new GoodsInfor(gt,"蜂花洗髮水250ml",11.1,0);

i.print();

 

}

 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章