Java从入门到精通章节练习题——第八章

Exercise 1 划火柴

方法一:使用标识火柴是否被点燃的布尔值判断火柴状态

interface Ignitable {
    
    
    void ignite();
}

class Match implements Ignitable {
    
    
    private boolean ignited = false;

    public void ignite() {
    
    
        if (!ignited) {
    
    
            System.out.println("火柴点燃了!");
            ignited = true;
        } else {
    
    
            System.out.println("火柴已经被点燃过了!");
        }
    }
}

创建Match对象来使用:

Match match1 = new Match();
match1.ignite(); // 输出 "火柴点燃了!"
match1.ignite(); // 输出 "火柴已经被点燃过了!"

Match match2 = new Match();
match2.ignite(); // 输出 "火柴点燃了!"

方法二:构造器私有化,保证火柴对象只能创建一次,并执行一次点燃方法

public class StrikeMatch {
    
    

    /**
     * 综合练习1:划火柴 定义一个点燃接口,再定义一个火柴类实现点燃接口,
     * 每根火柴对象只能调用一次点燃方法,这种对象该如何创建?
     */

    public static void main(String[] args) {
    
    
        Match.createMatch();
        Match.createMatch();
    }
}

interface Ignite {
    
    
    void ignite();
}

class Match implements Ignite {
    
    

    /**
     * 创建火柴对象的时候则调用点燃方法
     */
    private Match() {
    
    
        ignite();
    }

    public static Match createMatch() {
    
    
        return new Match();
    }


    @Override
    public void ignite() {
    
    
        System.out.println( Math.random() + "火柴被点燃");
    }
}

Exercise 2 跳动的心脏

package org.hj.chapter8;

public class BeatingHeart {
    
    

    /**
     * 综合练习2:跳动的心脏 心脏是动物的重要器官,不断跳动的心脏就意味着鲜活的生命力。
     * 现在创建一个人类,把心脏类设计为人类里面的一个成员内部类。心脏类有一个跳动的方法,
     * 在一个人被创建时,心脏就开始不断地跳动。
     */

    public static void main(String[] args) {
    
    
        new Person();
        new Person();
        new Person();
    }
}

class Person {
    
    
    public Person() {
    
    
        new Heart().beating();
    }

    public class Heart {
    
    
        public void beating() {
    
    
            System.out.println(Math.random() + "心脏开始跳动");
        }
    }
}

Exercise 3 吃水果

package org.hj.chapter8;

public class EatFruit {
    
    

    /**
     * 综合练习3:吃水果 创建一个抽象的水果类,类中有一个获取水果名称的抽象方法。
     * 创建人类,人类有个吃的方法,参数类型为水果类型,并可以在控制台打印吃了什么。
     * 请用匿名类创建吃方法的参数,让人类吃苹果和香蕉。
     */

    public static void main(String[] args) {
    
    

        People people = new People();

        // 使用匿名类创建吃方法的参数,让人类吃苹果和香蕉
        people.eat(new Fruit() {
    
    
            @Override
            String getFruit() {
    
    
                return "苹果";
            }
        });
        people.eat(new Fruit() {
    
    
            @Override
            String getFruit() {
    
    
                return "香蕉";
            }
        });
    }
}

abstract class Fruit {
    
    

    abstract String getFruit();
}

class People {
    
    

    public void eat(Fruit fruit) {
    
    

        System.out.println("吃了一个" + fruit.getFruit());
    }
}

Exercise 4 匿名类实现让小狗跑

package org.hj.chapter8;

public class RunningDog {
    
    

    /**
     * 综合练习4:匿名类实现让小狗跑 参照下面的代码,创建Moveable接口的匿名子类对象,
     * 重写run()方法,执行该方法后会在控制台打印“小狗向前跑”字样。
     */

    public static void main(String[] args) {
    
    

        //创建匿名内部类对象
        new Moveable() {
    
    
            @Override
            public void run() {
    
    
                System.out.println("小狗向前跑");
            }
        }.run();//调用方法
    }
}

interface Moveable {
    
    
    void run();
}

猜你喜欢

转载自blog.csdn.net/dedede001/article/details/130284996