多态、抽象类、接口练习:愤怒的小鸟(java项目实战愤怒的小鸟)

  本篇文章为你整理了多态、抽象类、接口练习:愤怒的小鸟(java项目实战愤怒的小鸟)的详细内容,包含有关于愤怒的抽象画 java项目实战愤怒的小鸟 表达愤怒的抽象画 多态、抽象类、接口练习:愤怒的小鸟,希望能帮助你了解 多态、抽象类、接口练习:愤怒的小鸟。

   Scanner sc = new Scanner(System.in);

   System.out.println("请输入你想实例化以下哪种鸟:");

   System.out.println("1:肥胖鸟");

   System.out.println("2:分裂鸟");

   System.out.println("3:爆炸鸟");

   int chiose = sc.nextInt();

   switch (chiose){

   case 1:

   brid = new FatBrid();

   System.out.println("肥胖鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName( sc.next());

   brid.setShout(new NoShout(brid.name));

   break;

   case 2:

   brid = new LieBrid();

   System.out.println("分裂鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName(sc.next());

   brid.setShout(new ZhaShout(brid.name));

   break;

   case 3:

   brid = new BoomBrid();

   System.out.println("爆炸鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName( sc.next());

   brid.setShout(new AoShout(brid.name));

   break;

   brid.hit();

   brid.fly();

   brid.shout1();

  }

 

 

  需求说明:

  模拟实现愤怒的小鸟,定义鸟叫的接口,定义"嗷嗷叫",“喳喳叫"两种实现类

  定义抽象类,“鸟”,将接口定义为鸟的属性,并定义两个子类,“炸弹鸟”,“分裂鸟”

  定义测试类,测试"炸弹鸟”,和"分裂鸟" 叫 和 飞 的行为

  1.定义一个接口 鸟叫的接口

  

package com.chen.pojo.DuoTai1;

 

  public interface Shout {

   public void shout();

  }

 

  2.定义嗷嗷叫的实现类

  

public class AoShout implements Shout{

 

   String name;

   public AoShout(String name) {

   this.name = name;

   public String getName() {

   return name;

   public void setName(String name) {

   this.name = name;

   @Override

   public void shout() {

   System.out.println(name+"嗷嗷叫,实现Shout接口");

  }

 

  
3.定义喳喳叫的实现类

  

public class ZhaShout implements Shout{

 

   String name;

   public String getName() {

   return name;

   public void setName(String name) {

   this.name = name;

   public ZhaShout(String name) {

   this.name = name;

   @Override

   public void shout() {

   System.out.println(name+"喳喳叫,实现Shout接口");

  }

 

  4.定义不会叫的实现类

  

public class NoShout implements Shout{

 

   String name;

   public String getName() {

   return name;

   public void setName(String name) {

   this.name = name;

   public NoShout(String name) {

   this.name = name;

   @Override

   public void shout() {

   System.out.println(name+"不叫,实现Shout接口");

  }

 

  
5.定义一个抽象类 鸟

  

public abstract class Brid {

 

   String name;

   public String getName() {

   return name;

   public void setName(String name) {

   this.name = name;

   private Shout shout;

   public Shout getShout() {

   return shout;

   public void setShout(Shout shout) {

   this.shout = shout;

   public void fly(){

   System.out.println(name+"在Brid抽象父类中飞。。。");

   public void shout1(){

   shout.shout();

   public abstract void hit();

  }

 

  6.定义鸟的子类,分裂鸟

  

public class LieBrid extends Brid{

 

   @Override

   public void hit() {

   System.out.println(name+"在LieBrid中扔蛋攻击》》》");

  }

 

  
7.定义鸟的子类,炸弹鸟

  

public class BoomBrid extends Brid{

 

   @Override

   public void hit() {

   System.out.println(name+"在BoomBrid中扔蛋攻击》》》");

  }

 

  8.定义鸟的子类,肥胖鸟

  

public class FatBrid extends Brid{

 

   @Override

   public void hit() {

   System.out.println(name+"在FatBrid中扔蛋攻击》》》");

  }

 

  9.定义一个测试类

  

package PolymorphismAngryBird;

 

  public class BridTest {

   public static void main(String[] args) {

   Brid brid = null;

   Scanner sc = new Scanner(System.in);

   System.out.println("请输入你想实例化以下哪种鸟:");

   System.out.println("1:肥胖鸟");

   System.out.println("2:分裂鸟");

   System.out.println("3:爆炸鸟");

   int chiose = sc.nextInt();

   switch (chiose){

   case 1:

   brid = new FatBrid();

   System.out.println("肥胖鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName( sc.next());

   brid.hit();

   brid.fly();

   brid.setShout(new NoShout(brid.name));

   brid.shout1();

   break;

   case 2:

   brid = new LieBrid();

   System.out.println("分裂鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName( sc.next());

   brid.hit();

   brid.fly();

   brid.setShout(new ZhaShout(brid.name));

   brid.shout1();

   break;

   case 3:

   brid = new BoomBrid();

   System.out.println("爆炸鸟实例化成功");

   System.out.println("请给小鸟取个名字:");

   brid.setName( sc.next());

   brid.hit();

   brid.fly();

   brid.setShout(new AoShout(brid.name));

   brid.shout1();

   break;

  }

 

  
10.输出的结果

  

请输入你想实例化以下哪种鸟:

 

  1:肥胖鸟

  2:分裂鸟

  3:爆炸鸟

  肥胖鸟实例化成功

  请给小鸟取个名字:

  小胖在FatBrid中扔蛋攻击》》》

  小胖在Brid抽象父类中飞。。。

  小胖不叫,实现Shout接口

  Process finished with exit code 0

 

  

  以上就是多态、抽象类、接口练习:愤怒的小鸟(java项目实战愤怒的小鸟)的详细内容,想要了解更多 多态、抽象类、接口练习:愤怒的小鸟的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: