封装和多态作业
一、 选择题
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. 与未加访问控制符的缺省情况相比,public和protected修饰符扩大了类及其属性和方法的被访问范围,private修饰符则缩小了这种范围。( dui )
4. 访问权限是private的变量,只能在本类和与本类同一个包中的其他类使用。( cuo )
三、 简答题
1. private、默认、protected、public四个权限修饰符的作用
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();
}
}