封装和多态作业
一、选择题
-
使用权限修饰符( )修饰的类的成员变量和成员方法,可以被当前包中所有类访问,也可以被它的子类(同一个包以及不同包中的子类)访问。(选择一项)
A public
B. protected
C. 默认
D. private -
给出如下代码,如何使成员变量m被方法fun()直接访问( )。(选择一项)
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修饰的成员属性和方法可以被当前项目中所有包的所有类访问。( )
2.类的方法通常设为public,而类的实例变量一般也设为public。( )
3.与未加访问控制符的缺省情况相比,public和protected修饰符扩大了类及其属性和方法的被访问范围,private修饰符则缩小了这种范围。( )
4.访问权限是private的变量,只能在本类和与本类同一个包中的其他类使用。( )
三、简答题
1.private、默认、protected、public四个权限修饰符的作用
四、编码题
1.使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑。
要求:
1)设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
2)限定心情只能有“心情好”和“心情不好”两种情况,如果无效输入进行提示,默认设置“心情好”。
3)设置构造函数实现对属性赋值
4)叫和跑的方法,需要根据心情好坏,描述不同的行为方式。
5)编写测试类,测试狗类的对象及相关方法(测试数据信息自定义)
运行效果图:
2.以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限, 工作单位和职务;方法包括:工作
要求:
- 设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
- 限定IT从业人员必须年满15岁,无效信息需提示,并设置默认年龄为15。
- 限定“技术方向”是只读属性
- 工作方法通过输入参数,接收工作单位和职务,输出个人工作信息
- 编写测试类,测试IT从业者类的对象及相关方法(测试数据信息自定义)
运行效果图:
五、可选题
1.以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍
要求:
1)设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
2)限定介格必须大于10,如果无效进行提示
3)限定作者,书名境外为只读属性
4)设计构造方法实现对属性赋值
5)信息介绍方法描述图书所有信息
6)编写测试类,测试图书类的对象及相关方法(测试数据信息自定)
运行效果图:
2.某公司要开发名为”我爱购物狂”的购物网站,请使用面向对象的思想设计描述商品信息
要求:
1)分析商品类别和商品详细信息属性和方法,设计商品类别类和商品详细信息类
2)在商品详细信息类中通过属性描述该商品所属类别
3)设置属性的私有访问权限,通过公有的get,set方法实现对属性的访问
4)编写测试类,测试商品类别类和商品详细信息类的对象及相关方法(测试数据信息自定)
5)创建包info—存放商品类别类和商品详细信息类,创建包test—存放测试类
参考分析思路:
商品类别类:
属性:类别编号,类别名称
商品详细信息类:
属性:商品编号,商品名称,所属类别,商品数量(大于0),商品价格(大于0),
方法:盘点的方法,描述商品信息。内容包括商品名称,商品数量,商品价格,现在商品总价以及所属类别信息
运行效果图:
封装作业答案
一、选择题
1.B
2.C
二、判断题
1.√
2.×
3.√
4.×
三、简答题
答案略
四、编码题
1.使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑。
public class Dog {
private String strain;
private int age;
private String mood;
private String name;
public Dog() {
super();
}
public Dog(String strain, int age, String mood,String name) {
super();
this.strain = strain;
this.age = age;
//this.mood = mood;
this.setMood(mood);
this.name = name;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMood() {
return mood;
}
public void setMood(String mood) {
if("心情好".equals(mood) || "心情不好".equals(mood)){
this.mood = mood;
}else{
System.out.println("输入信息有误,这只狗狗今天心情很好");
this.mood ="心情好";
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//跑
public void run(){
if("心情好".equals(mood)){
System.out.println("名字叫"+name+"的"+strain
+mood+",开心的围着主人身边转");
}else{
System.out.println("名字叫"+name+"的"
+strain+mood+",伤心的一动不动");
}
}
//叫
public void bark(){
if("心情好".equals(mood)){
System.out.println("名字叫"+name+"的"+strain
+mood+",开心的汪汪叫");
}else{
System.out.println("名字叫"+name+"的"
+strain+mood+",伤心的呜呜叫");
}
}
}
public class TestDog {
public static void main(String[] args) {
//1.过来一个狗狗
Dog dog1 = new Dog();
dog1.setName("甜心");
dog1.setAge(2);
dog1.setMood("心情不咋地");
dog1.setStrain("贵宾犬");
dog1.run();
dog1.bark();
System.out.println("================================");
//2.再过来一只狗狗
Dog dog2 = new Dog("德国牧羊犬", 3, "心情不好", "太子");
dog2.run();
dog2.bark();
}
}
2.以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限;方法包括:工作
public class ITWork {
private String name; //姓名
private int age;//年龄
private String tend;//技术方向
private int workAge;//工作年限
public ITWork() {
}
public ITWork(String name, int age, String tend, int workAge) {
super();
this.name = name;
//this.age = age;
this.setAge(age);
this.tend = tend;
this.workAge = workAge;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age < 15){
this.setAge(15);
System.out.println("年龄信息无效!已修改默认年龄为15");
}else{
this.age = age;
}
}
public String getTend() {
return tend;
}
public void setTend(String tend) {
this.tend = tend;
}
public int getWorkAge() {
return workAge;
}
public void setWorkAge(int workAge) {
this.workAge = workAge;
}
public void work(String company,String position){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("技术方向:"+tend);
System.out.println("工作年限:"+workAge);
System.out.println("目前就职于:"+company);
System.out.println("职务是:"+position);
}
}
public class Test {
public static void main(String[] args) {
ITWork it = new ITWork("马未龙", 35, "数据库维护", 10);
it.work("腾讯实业","数据库维护工程师");
System.out.println("======================");
ITWork it2 = new ITWork("张凯", 10, "Java开发", 15);
it2.work("鼎盛科技","Java开发工程师");
}
}
五、可选题
1.以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍
public class Book {
private String name;
private String author;
private String publisher;
public double price;
public Book() {
super();
}
public Book(String name, String author, String publisher, double price) {
super();
this.name = name;
this.author = author;
this.publisher = publisher;
//this.price = price;
this.setPrice(price);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(price<=10){
System.out.println("价格必须大于10");
this.price = price;
}else{
this.price = price;
}
}
/**
* 显示图书信息
*/
public void showInfo(){
System.out.println("书名:"+this.name);
System.out.println("作者:"+this.author);
System.out.println("出版社:"+this.publisher);
System.out.println("价格:"+this.price);
}
}
public class TestBook {
public static void main(String[] args) {
Book book1 = new Book();
book1.setName("鹿鼎记");
book1.setAuthor("金庸");
book1.setPublisher("人民文学出版社");
book1.setPrice(120);
book1.showInfo();
System.out.println("=============================");
Book book2 = new Book("绝代双骄", "古龙", "中国长安出版社", 55.5);
book2.showInfo();
}
}
2.某公司要开发名为”我爱购物狂”的购物网站,请使用面向对象的思想设计描述商品信息
package info;
public class ProductCategory {
private String cid;
private String name;
public ProductCategory() {
super();
}
public ProductCategory(String cid, String name) {
super();
this.cid = cid;
this.name = name;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package info;
public class Product {
private String pid;
private String name;
private int amount;
private double price;
private ProductCategory category;
public Product() {
super();
}
public Product(String pid, String name, int amount, double price) {
super();
this.pid = pid;
this.name = name;
this.amount = amount;
this.price = price;
}
public Product(String pid, String name, int amount, double price,
ProductCategory category) {
super();
this.pid = pid;
this.name = name;
this.setAmount(amount);
this.price = price;
this.category = category;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
if(amount<0){
System.out.println("库存数量异常,请联系管理员");
this.amount = 0;
}else{
this.amount = amount;
}
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public ProductCategory getCategory() {
return category;
}
public void setCategory(ProductCategory category) {
this.category = category;
}
public void check(){
System.out.println("商品名称:"+this.name);
System.out.println("所属类别:"+this.category.getName());
System.out.println("库存数量:"+this.price);
System.out.println("商品售价:"+this.amount);
System.out.println("商品总价:"+this.price*this.amount);
}
}
public class TestProduct {
public static void main(String[] args) {
//指定商品信息并盘点
ProductCategory category1 = new ProductCategory("11", "洗发水");
Product p1 = new Product("111", "潘婷洗发水400ml",
16, 40.5, category1);
p1.check();
System.out.println("==============");
//指定商品信息并盘点
Product p2 = new Product();
p2.setPid("222");
p2.setName("蜂花洗发水250ml");
p2.setPrice(11.5);
p2.setAmount(-5);
p2.setCategory(category1);
p2.check();
}
}