栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java封装和多态练习题答案

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java封装和多态练习题答案

选择题

B
C


判断题


×

×

简答题

答案略

编码题

使用面向对象的思想,编写自定义描述狗的信息。设定属性包括:品种,年龄,心情,名字;方法包括:叫,跑。
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();
    }
}

以面向对象的思想,编写自定义类描述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开发工程师");        
    }    
}

可选题
以面向对象的思想,编写自定义类描述图书信息。设定属性包括:书名,作者,出版社名,价格;方法包括:信息介绍
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();
    }
}

某公司要开发名为”我爱购物狂”的购物网站,请使用面向对象的思想设计描述商品信息
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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号