输出新闻

package com.neuedu.chapter6_1105.inter_prac;

public class Happy implements News{

public void addNews() {

System.out.println("发了一条娱乐新闻");

}

}


package com.neuedu.chapter6_1105.inter_prac;

public class Sports extends Object implements News{

public void addNews() {

System.out.println("发布了一条体育新闻");

}

}


package com.neuedu.chapter6_1105.inter_prac;

public class ZhengZhi implements News {

public void addNews() {

System.out.println("发了一条政治新闻");

}

}


package com.neuedu.chapter6_1105.inter_prac;

public interface News {

// 发布新闻的方法

void addNews();

}


package com.neuedu.chapter6_1105.inter_prac;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

while(true) {

Scanner scan = new Scanner(System.in);

System.out.println("请输入要发布的新闻类型");

System.out.println("1-----------娱乐");

System.out.println("2-----------体育");

System.out.println("3-----------政治");

System.out.println("4-----------退出");

int num = scan.nextInt();

News n = null;

switch(num) {

case 1:

n = new Happy();

break;

case 2:

n = new Sports();

break;

case 3:

n = new ZhengZhi();

break;

case 4:

return;

default :

System.out.println("无此选项");

continue;

}

n.addNews();

}

}

}

你可能感兴趣的:(输出新闻)