定义一个计算机类(Computer)
属性:品牌(PC_name)、颜色(color)、CPU型号(pc_type)、内存容量(pc_ram)、硬盘容量(pc_harddisk)、价格(pc_price)、工作状态(pc_state)
方法:打开(pc_open)、关闭(pc_close)、挂起(pc_hitch)
1)为计算机撰写一个带有默认(default)构造参数的类(无参)
2)为计算机类重构一个构造函数,使它接受参数
3)应用字段访问器get和字段设置器set。
假设笔记本电脑(BookComputer)是整个计算机类的一个子类,试编写一段程序实现这个子类。
笔记本的特殊属性与方法:
属性:机箱长度(Bookpc_long)、机箱宽度(Bookpc_width)、机箱厚度(Bookpc_thick)、重量(Bookpc_weight)、电池状态(Bookpc_cellstate)
方法:电池断电保护(bookpc_cellpost)
1)为笔记本电脑类撰写一个带有默认(default)构造参数的类(无参)
2)为笔记本电脑类重构一个构造函数,使它接受参数
3)应用字段访问器get和字段设置器set。
4)创建子类的对象,子类对象就是父类对象,子类可以调用父类的方法,但是子类还可以有自己的属性和方法

解决方案 »

  1.   

    package zy208;public class Computer { /**
     * Computer  Class
     */
    protected String PC_name;   //属性  名字 
    protected String pc_color;  //.. 颜色
    protected String pc_type;   //.. 型号
    protected int pc_ram;       //内存容量
    protected int pc_harddisk;  //硬盘容量
    protected float pc_price; //价格
     
    protected int pc_state=0; //    工作状态  0为 停止   1 运行  -1 挂起  



    Computer(){    //无参的构造函数,什么都不做

    }

    public Computer(String[] args)   //带参数的构造函数
    {

    }


    public void pc_open(){     //打开方法
    pc_state=1;
    }
    public void pc_close(){    //关闭方法 
    pc_state=0;
    }
    public void pc_hitch(){     //挂其方法 
       pc_state=-1;
    }

    public void set(String myname,String mycolor,String mytype,int myram,int myhard,float myprice){  //set方法,设置各属性值   
    this.PC_name=myname;
    this.pc_color=mycolor;
    this.pc_type=mytype;
    this.pc_ram=myram;
    this.pc_harddisk=myhard;
    this.pc_price=myprice;
    }
    public void get(){     //get方法输出各字短值
    System.out.println(PC_name);
    System.out.println(pc_color);
    System.out.println(pc_type);
    System.out.println(pc_ram);
    System.out.println(pc_harddisk);
    System.out.println(pc_price);
    System.out.println(pc_state);
    }
    }package zy208;public class BookComputer extends Computer { /**
     * 笔记本class
     */

    private float Bookpc_long; //机箱长度
    private float Bookpc_width;//机箱宽度
    private float Bookpc_thick;//机向后度
    private float Bookpc_weight;//机葙重量
    private boolean  Bookpc_cellstate=false;//电池状态  注: false为不工作状态,true为电池工作状态;莫认为0    BookComputer(){  //默认的无参构造函数

    }
    public BookComputer(String[] args){  //带参数的构造函数

    }


    public void bookpc_cellpost(){
    Bookpc_cellstate=true;
    }

    public void set(String myname,String mycolor,String mytype,int myram,int myhard,float myprice,float mylong,float mywidth,float mythick,float myweight){
    this.PC_name=myname;
    this.pc_color=mycolor;
    this.pc_type=mytype;
    this.pc_ram=myram;
    this.pc_harddisk=myhard;
    this.pc_price=myprice;
    this.Bookpc_long=mylong;
    this.Bookpc_width=mywidth;
    this.Bookpc_thick=mythick;
    this.Bookpc_weight=myweight;

    }

    public void get(){
    System.out.println(PC_name);
    System.out.println(pc_color);
    System.out.println(pc_type);
    System.out.println(pc_ram);
    System.out.println(pc_harddisk);
    System.out.println(pc_price);
    System.out.println(Bookpc_long);
    System.out.println(Bookpc_width);
    System.out.println(Bookpc_thick);
    System.out.println(Bookpc_weight);
    }

    public static void main(String[] args){
    BookComputer f=new BookComputer();

    }




    }