写在前面
大家前面的面向对象部分学的怎么样了,快来看看这些题你能不能快速地写出答案,面向对象在Java中是非常重要的,快来检测你的薄弱点在哪,及时查漏补缺!
使用面向对象思想编写下列题目:
1.使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定心情只能有“心情好”和“心情不好”两种情况,如果无效输入进行提示, 默认设置“心情好”。
3)设置构造函数实现对属性赋值
4)叫和跑的方法,需要根据心情好坏,描述不同的行为方式。
5)编写测试类,测试狗类的对象及相关方法(测试数据信息自定义)
运行效果图:
代码参考:
class Dog {private String kind;private int age;private String mod;private String name;public Dog() {}public Dog(String kind, int age, String mod, String name) {this.kind = kind;this.age = age;this.mod = mod(mod);this.name = name;}public String getKind() {return kind;}public void setKind(String kind) {this.kind = kind;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getMod() {return mod;}public void setMod(String mod) {this.mod = mod;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void show() {System.out.println("名字叫" + name + "年龄" + age + "岁的" + kind + "犬" + mod + ",");}private String mod(String mod) {if (mod == null || (!mod.equals("心情很好") && !mod.equals("心情不好"))) {mod = "心情很好";System.out.println("输入信息错误,这只狗狗今天" + mod + "!");}return mod;}public void run() {if (this.mod.equals("心情很好")) {System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",开心的围着主人身边转");} else if (this.mod.equals("心情不好")) {System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",伤心的一动不动");}}public void woof() {if (this.mod.equals("心情很好")) {System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",开心的汪汪叫");} else if (this.mod.equals("心情不好")) {System.out.println("名字叫" + this.name + "的" + this.kind + "犬" + this.mod + ",伤心的呜呜叫");}}
}class Test1 {public static void main(String[] args) {Dog d1 = new Dog("贵宾", 1, "哈哈哈", "甜心");Dog d2 = new Dog("贵宾", 1, "心情很好", "甜心");d2.run();d2.woof();System.out.println("========================================");Dog d3 = new Dog("德国牧羊", 1, "心情不好", "太子");d3.run();d3.woof();System.out.println("========================================");d2.show();d3.show();}
}
2.以面向对象的思想,编写自定义类描述IT从业者。设定属性包括:姓名,年龄,技术方向,工作年限, 工作单位和职务;方法包括:工作。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定 IT 从业人员必须年满 15 岁,无效信息需提示,并设置默认年龄为 15。
3)限定“技术方向”是只读属性(只提供 get 方法)
4)工作方法通过输入参数,接收工作单位和职务,输出个人工作信息
5)编写测试类,测试 IT 从业者类的对象及相关方法(测试数据信息自定义)
运行效果图:
代码参考:
class ItPractitioners {private String name;private int age;private String technicalDirection;private int workYear;private String workplace;private String office;public ItPractitioners() {}public ItPractitioners(String name, int age, int workYear, String technicalDirection) {this.name = name;this.age = age(age);this.workYear = workYear;this.technicalDirection = technicalDirection;}public ItPractitioners(String name, int age, int workYear, String technicalDirection, String workplace, String office) {this.name = name;this.age = age(age);this.workYear = workYear;this.technicalDirection = technicalDirection;this.workplace = workplace;this.office = office;}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 getTechnicalDirection() {return technicalDirection;}private void setTechnicalDirection(String technicalDirection) {this.technicalDirection = technicalDirection;}public int getWorkYear() {return workYear;}public void setWorkYear(int workYear) {this.workYear = workYear;}public String getWorkplace() {return workplace;}public void setWorkplace(String workplace) {this.workplace = workplace;}public String getOffice() {return office;}public void setOffice(String office) {this.office = office;}private int age(int age) {if (age < 15) {System.out.println("年龄信息无效!已修改默认年龄为15");age = 15;}return age;}public void work(String workplace, String office) {System.out.println("姓名:" + this.name);System.out.println("年龄:" + this.age);System.out.println("技术方向:" + this.technicalDirection);System.out.println("工作年限:" + this.workYear);System.out.println("目前就职于:" + workplace);System.out.println("职务是:" + office);}
}class Test2 {public static void main(String[] args) {ItPractitioners i1 = new ItPractitioners("马未龙", 35, 10, "数据库维护");i1.work("腾讯实业", "数据库维护工程师");System.out.println("===========================================");ItPractitioners i2 = new ItPractitioners("张凯", 14, 1, "Java开发");i2.work("鼎盛科技", "Java开发工程师");}
}
3.以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍 show()。
要求:
1)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
2)限定价格必须大于 10,如果无效进行提示
3)限定作者,书名为只读属性
4)设计构造方法实现对属性赋值
5)信息介绍方法描述图书所有信息
6)编写测试类,测试图书类的对象及相关方法(测试数据信息自定)
运行效果图:
代码参考:
class Book {private String bookName;private String author;private String publishingHouse;private double price;public Book() {}public Book(String bookName, String author, String publishingHouse, double price) {this.bookName = bookName;this.author = author;this.publishingHouse = publishingHouse;this.price = price(price);}public String getAuthor() {return author;}private void setAuthor(String author) {this.author = author;}public String getBookName() {return bookName;}private void setBookName(String bookName) {this.bookName = bookName;}public String getPublishingHouse() {return publishingHouse;}public void setPublishingHouse(String publishingHouse) {this.publishingHouse = publishingHouse;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}private double price(double price) {if (price <= 10.0) {System.out.println("该书的价格小于10元,是本无效的书!默认为10元");price = 10.0;}return price;}public void show() {System.out.println("书名:"+this.bookName);System.out.println("作者:"+this.author);System.out.println("出版社:"+this.publishingHouse);System.out.println("价格:"+this.price);}
}class Test3{public static void main(String[] args) {Book b1 = new Book("鹿鼎记","金庸","人民文学出版社",120.0);b1.show();System.out.println("========================================");Book b2 = new Book("绝代双骄","古龙","中国长安出版社",55.5);b2.show();System.out.println("========================================");Book b3 = new Book("鹿鼎记","金庸","人民文学出版社",8.0);b3.show();}
}
4.某公司要开发名为”我爱购物狂”的购物网站,请使用面向对象的思想设计描述商品信息。
要求:
1)分析商品类别和商品详细信息属性和方法,设计商品类别类和商品详细信息类
2)在商品详细信息类中通过属性描述该商品所属类别
3)设置属性的私有访问权限,通过公有的 get,set 方法实现对属性的访问
4)编写测试类,测试商品类别类和商品详细信息类的对象及相关方法(测试数据 信息自定)
5)创建包 info—存放商品类别类和商品详细信息类,创建包 test—存放测试类参考分析思路:
商品类别类:
属性:类别编号,类别名称商品详细信息类:
属性:商品编号,商品名称,所属类别,商品数量(大于 0),商品价格(大于 0),
方法:盘点的方法,描述商品信息。内容包括商品名称,商品数量,商品价格,现在商品总价以及所属类别信息
运行效果图:
代码参考:
public class ProductCategory {private String categoryId;private String categoryName;public ProductCategory() {}public ProductCategory(String categoryName, String categoryId) {this.categoryName = categoryName;this.categoryId = categoryId;}public String getCategoryName() {return categoryName;}public void setCategoryName(String categoryName) {this.categoryName = categoryName;}public String getCategoryId() {return categoryId;}public void setCategoryId(String categoryId) {this.categoryId = categoryId;}public void show(){System.out.println("商品类别ID为:"+categoryId+" 对应的类别为:"+categoryName);}}public class ProductInformation {private String productId;private String productName;private ProductCategory category;private int number;private double price;public ProductInformation() {}public ProductInformation(String productName, ProductCategory category, int number, double price) {this.productName = productName;this.category = category;this.number = number(number);this.price = price(price);}public ProductInformation(String productId, String productName, ProductCategory category, int number, double price) {this.productId = productId;this.productName = productName;this.category = category;this.number = number(number);this.price = price(price);}public String getProductId() {return productId;}public void setProductId(String productId) {this.productId = productId;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public ProductCategory getCategory() {return category;}public void setCategory(ProductCategory category) {this.category = category;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public double getPrice() {return price;}public void setPrice(int price) {this.price = price;}private int number(int number) {if (number < 0) {number = 0;System.out.println("库存数量异常,请联系管理员");}return number;}private double price(double price) {if (price < 0.0) {price = 0.0;System.out.println("输入价格无效!默认为0.0");}return price;}public void show1() {System.out.println("商品id为:" + productId + " 商品名称为:" + productName + " 所属类别为" + category.getCategoryName() + " 商品数量为:" + number + " 商品价格为:" + price);}public void show2() {System.out.println("商品名称:" + productName);System.out.println("所属类别:" + category.getCategoryName());System.out.println("商品售价:" + price);System.out.println("库存数量:" + number);System.out.println("商品总价:" + price * number);}}public class Test {public static void main(String[] args) {ProductCategory pc1 = new ProductCategory("洗发水", "00001");pc1.show();System.out.println("========================================");ProductInformation pi1 = new ProductInformation("潘婷洗发水400ml",pc1,16,40.5);pi1.show1();System.out.println("========================================");pi1.show2();System.out.println("========================================");ProductInformation pi2 = new ProductInformation("蜂花洗发水250ml",pc1,-1,11.5);pi2.show2();}
}
写在最后
之前分享了两个关于测试类的题目,在这里给出参考答案:
/*定义一个类Demo,其中定义一个求两个数据和的方法,定义一个测试类Test,进行测试*/
//3.0写法
class Demo {private int a;private int b;public Demo(int a, int b) {this.a = a;this.b = b;}public int getB() {return b;}public void setB(int b) {this.b = b;}public int getA() {return a;}public void setA(int a) {this.a = a;}public int sum() {return a + b;}
}class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入两个整数(以空格分隔):");int a = sc.nextInt();int b = sc.nextInt();Demo d1 = new Demo(a, b);int sum = d1.sum();System.out.println("您输入的两个数的和为:" + sum);}
}/*定义一个长方形类,定义 求周长和面积的方法,然后定义一个测试类Test2,进行测试。*/class Demo2 {private int a;private int b;public Demo2(int b, int a) {this.b = b;this.a = a;}public int getA() {return a;}public void setA(int a) {this.a = a;}public int getB() {return b;}public void setB(int b) {this.b = b;}public int zhouChang() {return 2 * (a + b);}public int mianJi() {return a * b;}
}class Test2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个矩形的长和宽(长和宽都应该是正整数,以空格分隔):");int h = sc.nextInt();int w = sc.nextInt();Demo2 d1 = new Demo2(h, w);int c = d1.zhouChang();int s = d1.mianJi();System.out.println("您输入的矩阵的周长为:" + c + "\n您输入的矩阵的面积为:" + s);}
}
好了,今天的分享到这就结束了,面向对象思想在初学时还是比较难懂的,大家一定要多多练习,查缺补漏,才能把它学好,如果有代码中有问题,欢迎在底下评论留言,代码仅供参考,不代表最终答案!