惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 段静迪

跟我学LINQ之一:为什么要学习LINQ C语言游戏 JAVA猜字游戏 在.NET平台下实现打印超市收银票据 JAVA上课源代码 基于三层架构的软件开发技术 MS-SQL数据库开发常用汇总 基于C语言的个人所得税计税系统 JAVA 教学实例 在.NET平台下使用SQL2000 Image类型数据 在.NET平台下使用SQL2000 Image类型数据 对SQL Server2000数据块进行查询 界面和业务代码分离的记事本 在.Net平台下读写文件流 键盘游戏 多线程摇奖机程序源代码 网页第五章 HTML标签详解 JDK 目录下的*.exe文件的使用
计算机类在Java中的设计于实现码
段静迪 · 2007-10-31 · via 博客园 - 段静迪
 

计算机类在Java中的设计于实现码

问题描述:

       一台计算机是由主板、CPU、显卡、声卡等部件组成的,这些部件通过接口可以直接安插在主板的插槽上,也就是说只要将这些部件简单的安插在一起就可以成功组装出一台电脑。为什么如此高端技术的产品却能如此简单的组装呢?原因是“主板提供了对外能够即插即用的接口,各类卡片和CPU实现了这些接口”。双方满足固定的通讯协议-接口,因此实现了电脑的模块级组装。

    本试验要求设计电脑的主板类、显卡类和它们的接口,并且利用这些实现一台电脑,把主板对象和显卡对象组装在一起,并且配合工作。实验目的:

问题分析:

定义接口。

所谓接口,是指一组通讯协议的集合。它只是规定了实现任务,但是没有任务的具体实现。整个问题的关键是规定好主板和卡片之间的接口,主板要预留接口,卡片要实现接口,最重要的是主板能够通过接口控制卡片。

设计主板类。

主板类是所有卡片的载体,它预留接口,可以通过接口控制和管理其它已经实现了接口的硬件模块。当然主板也拥有自己的方法,如设置自己和初始化其它模块。

设计显卡类。

显卡类是组装在主板上的一个硬件模块,它必须实现所有接口方法,根据自己的功能需要,具体的实现方法独立于主板和接口。

实现电脑并测试。

实例化主板类、显卡类对象,并组装,调试。关键代码解析

关键代码解析

定义接口

public interface PCI{

   //定义PCI接口

   String privder = "IBM";//定义接口的提供商

   String GetName();//卡片必须提供产品品牌

   void UseDate(); //卡片必须能够接收、使用主板发送的数据

}

定义主板类

class Mainboard 

{

   PCI pci;//主板预留PCI接口

   public void SetPci1(PCI pci)

   //设置PCI接口部件

   {

      this.pci = pci;//设置PCI接口部件

   }

   public void Work()

   {

      System.out.println ("Main Reset OK");

      System.out.println ("IBM");

      System.out.println (vci.privder);

      System.out.println (pci.GetName();    

      vci.UseData();//使卡片参与工作

      System.out.println ("Computer is Running...........");

   }

}

定义显卡类

class VideoCard implements PCI//显卡要实现PCI接口

{

   String name =null;

   //保存自己的品牌

   public VideoCard()

   {

   } 

   public VideoCard(String name )

   {

      this.name = name;

   }

   public String GetName()

   {//实现String GetName();提供产品品牌

      return name;

   } 

   public void UseData()

   {//实现void UseDate(); 能够接收、使用主板发送的数据

      System.out.println ("Video Cadr is working.....");

   } 

}

定义测试类,实现电脑

class Computer

{

   public static void main (String[] args) {

     Mainboard m =new Mainboard();

     //购买主板

     VideoCard vc = new VideoCard("sulong");

     //购买显卡

     m.SetPci(vc);

     //主板上电后,设置PCI接口。

     //public void SetPci(PCI pci)方法需要使用一个PCI接口对象,

     //VideoCard类 implements PCI接口,VideoCard类是PCI的子类,

     //所以可以直接把vc对象当作PCI对象传递给SetPci方法。

     m.Work();

     //主板正常工作

    }

}

存在问题及解决

问题扩充

       完整的实现一台电脑