zoukankan      html  css  js  c++  java
  • 课堂所讲整理:多态

    创建CPU的接口:

    1 package org.hanqi.pn0120;
    2  
    3 //cpu的接口
    4 public interface ICPU {             //创建的接口名一般前面加I
    5                                      //接口的关键字  interface
    6     //控制
    7     public void command();    
    8 
    9 }

    创建显卡的接口:

    1 package org.hanqi.pn0120;
    2 
    3 public interface IDisplayCard {
    4     
    5     //显示
    6     public void display();
    7 }

    创建Intel类与ICPU相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class Intel implements ICPU {                 //相关联的关键字 implements
     4 
     5     @Override
     6     public void command() {
     7 
     8         System.out.println("我是Intel的CPU");
     9     }
    10 }

    创建AMD类与ICPU相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class AMD implements ICPU {
     4 
     5     @Override
     6     public void command() {
     7         
     8         System.out.println("我是AMD的CPU");
     9     }
    10 }

    创建AMD2类与ICPU相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class AMD2 implements ICPU, IDisplayCard {
     4 
     5     @Override
     6     public void display() {
     7         
     8         System.out.println("AMD的CPU显卡");
     9 
    10     }
    11 
    12     @Override
    13     public void command() {
    14         
    15         System.out.println("AMD的自带显卡的CPU");
    16     }
    17 }

    创建AMD3类与ICPU相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class AMD3 extends AMD implements IDisplayCard {
     4 
     5     @Override
     6     public void display() {
     7         
     8         System.out.println("继承了AMD的一代CPU,增加了显卡功能");
     9     }
    10 }

    创建Ati类与IDisplayCard相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class Ati implements IDisplayCard {
     4 
     5     @Override
     6     public void display() {
     7 
     8     System.out.println("我是A卡");
     9 
    10     }
    11 }

    创建Nivdia类与IDisplayCard相关联:

     1 package org.hanqi.pn0120;
     2 
     3 public class Nivdia implements IDisplayCard {
     4 
     5     @Override
     6     public void display() {
     7         
     8         System.out.println("我是N卡");
     9 
    10     }
    11 }

    最后创建Computer类进行输出运算:

     1 package org.hanqi.pn0120;
     2 
     3 public class Computer {
     4     
     5     //品牌
     6     private String pinpai;
     7     
     8     //CPU
     9     private ICPU cpu;
    10     
    11     //显卡
    12     private IDisplayCard displaycard;
    13 
    14     public String getPinpai() {
    15         return pinpai;
    16     }
    17 
    18     public void setPinpai(String pinpai) {
    19         this.pinpai = pinpai;
    20     }
    21 
    22     public ICPU getCpu() {
    23         return cpu;
    24     }
    25 
    26     public void setCpu(ICPU cpu) {
    27         this.cpu = cpu;
    28     }
    29 
    30     public IDisplayCard getDisplaycard() {
    31         return displaycard;
    32     }
    33 
    34     public void setDisplaycard(IDisplayCard displaycard) {
    35         this.displaycard = displaycard;
    36     }
    37     
    38     //运行
    39     public void run()
    40     {
    41         //cpu
    42         this.cpu.command();
    43         //显卡
    44         this.displaycard.display();
    45         System.out.println("我组装的"+pinpai+"电脑开始运行");
    46     }
    47     
    48     public Computer(String pinpai)
    49     {
    50         super();   
    51         this.pinpai=pinpai;
    52     }
    53     public static void main(String[] args)
    54     {
    55         //组装电脑
    56         Computer c1 = new Computer("hp");
    57         
    58         //第一种
    59         AMD a1 = new AMD();        
    60         c1.setCpu(a1);
    61         //第二种  匿名类 new Ati()
    62         c1.setDisplaycard(new Ati());        
    63         c1.run();
    64         
    65         System.out.println();
    66         
    67         Computer c2 = new Computer("华硕");
    68         c2.setCpu(new Intel());
    69         c2.setDisplaycard(new Nivdia());
    70         c2.run();
    71         
    72         System.out.println();
    73         AMD2 a2 = new AMD2();
    74         Computer c3 = new Computer("acer");
    75         c3.setCpu(a2);
    76         c3.setDisplaycard(a2);
    77         c3.run();
    78         
    79         System.out.println();
    80         AMD3 a3 = new AMD3();
    81         Computer c4 = new Computer("acer");
    82         c4.setCpu(a3);
    83         c4.setDisplaycard(a3);
    84         c4.run();
    85     }
    86 }

    运行结果为:

    附思维导图:

  • 相关阅读:
    有点成熟的短句,最新个性签名
    ACM2039_三角形三边关系
    Android 绘制中国地图
    Opengl-法线贴图(用来细化表面的表现表现的凹凸)
    Go的sync
    Laravel Study(使用 Laravel )
    对于宅男来说,硬盘里的数据就是命
    设计模式之模板方法模式
    游戏掉落道具掉落
    NEWMING
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5259378.html
Copyright © 2011-2022 走看看