栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

Java类和对象之对象引用之模拟手机功能

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java类和对象之对象引用之模拟手机功能

目录

任务描述

编程要求

代码展示


 

任务描述

本关任务:实现手机的基本功能。

手机具有属性:品牌(brand)、型号(type)、价格(price)、操作系统(os)和内存(memory);

具有功能:查看手机信息(about())、打电话(call(int number))、玩游戏(play())。

编程要求

仔细阅读右侧编辑区内给出的代码框架及注释,按照提示编写程序代码。

代码展示

    class Phon {

    // 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码

    

        // 定义五个变量,分别表示品牌、型号、价格、操作系统和内存

       public String brand;

       public String type;

       public double price;

       public String os;

       public int memory;

        // 无参构造

//       public Phone()

//          {}

        // 有参构造

       public Phon(String brand,String type,double price,String os,int memory)

       {

         this.brand=brand;

         this.type=type;

         this.price=price;

         this.os=os;

         this.memory=memory;

         }

        

       public void xin()

       {

          System.out.println("品牌:"+brand);

          System.out.println("型号:"+type); 

          System.out.println("操作系统:"+os); 

          System.out.println("价格:"+price); 

          System.out.println("内存:"+memory); 

       }

        

       public void call(int num)

       {

          if(num==1)

            System.out.println("正在给爸爸打电话"); 

          else if(num==2)

              System.out.println("正在给妈妈打电话"); 

          else if(num==3)

               System.out.println("正在给姐姐打电话"); 

          else

              System.out.println("你所拨打的电话为空号"); 

       }

        

       public void music(String song)

       {

            System.out.println("正在播放"+song); 

       }

    }

public class Phone {

        // 定义主方法

       public static void main(String[] args) {

        // 通过无参构造创建手机对象

           Phon p=new Phon("小米","小米9",2599,"Android 9",8);

        // 设置手机品牌为小米,型号为小米9,操作系统为Android 9,价格为2599,运行内存为8

        // 查询手机信息

         p.xin();

        // 给妈妈拨打电话

         p.call(2);

        // 播放浮夸这首歌

         p.music("浮夸");

       }

    

}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号