• 接口实例


      1 package 面向对象;
      2 
      3 public interface ICPU {
      4 
      5     //定义CPU接口
      6     public void PinPai1();
      7     public void Name1();
      8     public void XingHao1();
      9     
     10 }
     11 
     12 
     13 
     14 public interface IScrean {
     15 
     16     //定义显示器接口
     17     public void PinPai2();
     18     public void XingHao2();
     19     public void Size2();
     20 }
     21 
     22 
     23 
     24 public class SHIXIAN implements ICPU, IScrean {
     25     //实现接口
     26     @Override
     27     public void PinPai2() {
     28         System.out.println("中华映管");
     29 
     30     }
     31 
     32     @Override
     33     public void XingHao2() {
     34         System.out.println("CPT17AB");
     35 
     36     }
     37 
     38     @Override
     39     public void Size2() {
     40         System.out.println("14英寸");
     41 
     42     }
     43 
     44     @Override
     45     public void PinPai1() {
     46         System.out.println("Intel");
     47 
     48     }
     49 
     50     @Override
     51     public void Name1() {
     52         System.out.println("酷睿");
     53 
     54     }
     55 
     56     @Override
     57     public void XingHao1() {
     58         System.out.println("LGA2011");
     59 
     60     }
     61 
     62 }
     63 
     64 
     65 
     66 //创建电脑类
     67 public class Computer {
     68 
     69     //定义成员变量
     70     private ICPU cpu;
     71     private IScrean screan;
     72     //生成get、set方法
     73     public ICPU getCpu() {
     74         return cpu;
     75     }
     76     public void setCpu(ICPU cpu) {
     77         this.cpu = cpu;
     78     }
     79     public IScrean getScrean() {
     80         return screan;
     81     }
     82     public void setScrean(IScrean screan) {
     83         this.screan = screan;
     84     }
     85     //成员方法
     86     public boolean zuzhuang()
     87     {
     88         boolean b = false;
     89         if(cpu==null)
     90         {
     91             System.out.println("没有CPU,电脑未组装成功。");
     92             b = false;
     93         }
     94         else if(screan == null)
     95         {
     96             System.out.println("没有显示器。电脑未组装成功。");
     97             b = false;
     98         }
     99         else
    100         {
    101             System.out.println("有CPU,有显示器。电脑组装成功。");
    102             b = true;
    103         }
    104         return b;
    105         
    106     }
    107 }
    108 
    109 
    110 
    111 //测试
    112 public class testcar {
    113 
    114     public static void main(String[] args) {
    115 
    116         Computer computer = new Computer();
    117         computer.setCpu(new SHIXIAN());
    118         computer.setScrean(new SHIXIAN());
    119         computer.zuzhuang();
    120    }
    121 }
  • 相关阅读:
    ModbusRTU模式和结束符(转)
    modbus字符串的结束符介绍
    IAR平台移植TI OSAL到STC8A8K64S4A12单片机中
    实时系统概念
    单片机的存储区范例
    如何实现返回上一个页面,就像点击浏览器的返回按钮一般
    spring项目中的定时任务实现和问题解决
    context-param与init-param的区别与作用
    Chapter 1 First Sight——16
    一个好用简单的布局空间EasyUI
  • 原文地址:https://www.cnblogs.com/hellodp/p/5150068.html
Copyright © 2020-2023  润新知