集册 Java实例教程 创建抽象类

创建抽象类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

550
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建抽象类

import java.util.*;
/** from NowJava.com - 时代Java**/

public class AbstractClassTest {

    public static void main(String[] args) {


        //check out the class alien one

        AlienCreatureOne aOne = new AlienCreatureOne();

        System.out.format("%s%n", aOne.lifeBegins());

        System.out.format("%s%n", aOne.live());

        System.out.format("%s%n", aOne.lifeEnds());


        //check out the class alien two

        AlienCreatureTwo aTwo = new AlienCreatureTwo();

        System.out.format("%s%n", aTwo.lifeBegins());

        System.out.format("%s%n", aTwo.live());

        System.out.format("%s%n", aTwo.lifeEnds());

    }


}

abstract class AlienCreature {

    // all creaturesd are born// 来自 nowjava.com - 时  代  Java

    String lifeBegins() {

        String me = "Hello, I'm here.";

        return me;

    }


    // creatures live by eating different material..so this is abstract

    abstract String live();


    // all creatures die

    String lifeEnds() {

        String meEnd = "Oh no, it all ends - bye. ";

        return meEnd;

    }

}


//nonabstract subclass 1

class AlienCreatureOne extends AlienCreature {

    String live() {

        String living = "We alien ones live happily on alien one nutrition.";

        return living;

    }

}

//nonabstract subclass 2


class AlienCreatureTwo extends AlienCreature {

    String live() {

        String living = "We alien twos live happily on alien two nutrition.";

        return living;

    }

}

展开阅读全文