黑马程序员技术交流社区

标题: 类的继承,抽象类综合练习01 [打印本页]

作者: _J2EE_LiXiZhen    时间: 2017-11-8 00:18
标题: 类的继承,抽象类综合练习01
请用代码描述:
20岁1.75米的男孩慕容紫英有一个18岁1.6米瓜子脸女朋友欧阳青青
欧阳青青在洗一件白色的李宁牌子的衣服
慕容紫英去散步(和欧阳青青)
慕容紫英用带有阳刚之气的声音对欧阳青青说:我会守护你一生一世
欧阳青青微笑着用于甜美的声音对慕容紫英说:有你在我就安心.
要求: 男孩和女孩的共有成员提取到人类中,使用抽象类和抽象方法
[Java] 纯文本查看 复制代码
public abstract class Person {
       
        public Person() {
                // TODO Auto-generated constructor stub
        }
       
        public Person(String name, int age, double height) {
                super();
                this.name = name;
                this.age = age;
                this.height = height;
        }

        private String name;
        private int age;
        private double height;
       
       
       
        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 double getHeight() {
                return height;
        }

        public void setHeight(double height) {
                this.height = height;
        }

        public abstract void say(String content);
}

public class Clothes {

        public Clothes() {
                // TODO Auto-generated constructor stub
        }

        public Clothes(String color, String brand) {
                super();
                this.color = color;
                this.brand = brand;
        }

        private String color;
        private String brand;

        // get/set
        public String getColor() {
                return color;
        }

        public void setColor(String color) {
                this.color = color;
        }

        public String getBrand() {
                return brand;
        }

        public void setBrand(String brand) {
                this.brand = brand;
        }
}

public class Boy extends Person{
       
        public Boy() {
                // TODO Auto-generated constructor stub
        }
       
        public Boy(String name,int age,double height,Girl grilFriend) {
                this.setName(name);
                this.setAge(age);
                this.setHeight(height);
                this.girlFriend = grilFriend;
        }
       
        private Girl girlFriend;
       
        //get/set
        public Girl getGirlFriend() {
                return girlFriend;
        }

        public void setGirlFriend(Girl girlFriend) {
                this.girlFriend = girlFriend;
        }
       
        //说话
        public void say(String content) {
                System.out.println(this.getName()+"用带有阳刚之气的声音对"+this.girlFriend.getName()+"说:"+content);
        }
       
        //散步
        public void walking() {
                System.out.println(this.getName()+"和"+this.girlFriend.getName()+"在河边的林荫小道上手牵着手散步");
        }
}

public class Girl extends Person{
       
        public Girl() {
                // TODO Auto-generated constructor stub
        }
       
        public Girl(String name,int age,double height,String face) {
                this.face = face;
                this.setName(name);
                this.setAge(age);
                this.setHeight(height);
        }
       
        private String face;

        public String getFace() {
                return face;
        }
       
        //get/set
        public void setFace(String face) {
                this.face = face;
        }
       
        //说话
        public void say(String content) {
                System.out.println(this.getName()+"微笑着用于甜美的声音对"+content+"说:"+content);
        }
       
        //洗衣服
        public void wash(Clothes c) {
                System.out.println(this.getName()+"在洗一件"+c.getColor()+"的"+c.getBrand()+"牌子的衣服");
        }
}

/*a)提供main方法
                b)在main方法中
                i.创建衣服对象 c,品牌初始化为李宁,颜色初始化为白色
                ii.创建女孩对象 girl,姓名赋值为欧阳青青,年龄赋值为18,**赋值为1.6,脸型赋值为瓜子脸
                iii.创建男孩对象 boy,姓名赋值为慕容紫英,年龄赋值以为20,**赋值1.75,女朋友赋值为girl
                iv.调用对象girl的洗衣服方法
                v.调用对象boy的散步方法
                vi.调用对象boy的说话方法,传入:”我会守护你一生一世”
                vii.调用对象 girl 的说话方法,传入:  “有你在我就安心.”*/

public class Test {
        public static void main(String[] args) {
                Clothes c = new Clothes("白色", "李宁");
                Girl g = new Girl("欧阳青青", 18, 1.6, "瓜子脸");
                Boy b = new Boy("慕容紫英", 20, 1.75, g);
               
                System.out.println(b.getAge()+"岁"+b.getHeight()+"米的男孩"+b.getName()+"有一个"+
                                   g.getAge()+"岁"+g.getHeight()+"米"+g.getFace()+"女朋友"+g.getName());
               
                g.wash(c);
                b.walking();
                b.say("我会守护你一生一世");
                g.say("有你在我就安心");
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2