java封裝範例程式碼

來源:互聯網
上載者:User

標籤:

package com.imooc;public class Telphone {    private float screen;    private float cpu;    private float mem;        public float getScreen(){        return screen;    }        public void setScreen(float newScreen){        screen = newScreen;    }        public Telphone(){        System.out.println("無參的構造方法執行了!");    }        public Telphone(float newScreen, float newCpu, float newMem){        //構造方法確保賦給成員的變數的值是一個合理值        if(newScreen<3.5f){            System.out.println("您輸入的參數有問題,自動賦值3.5");            screen = 3.5f;        }else{            screen = newScreen;        }                cpu = newCpu;        mem = newMem;        System.out.println("有參的構造方法執行了!");    }}
package com.imooc;public class InitialTelphone {    public static void main(String[] args) {        // TODO Auto-generated method stub        //通過無參的構造方法可以建立對象        Telphone phone1 = new Telphone();        //通過有參的構造方法可以建立對象,並給對象的執行個體變數賦初值        Telphone phone2 = new Telphone(1.5f, 1.4f, 2.2f);                //通過setter方法修改變數的值        phone1.setScreen(5.0f);        //通過getter方法擷取變數的值        System.out.println("screen:" + phone1.getScreen());       }}

 

java封裝範例程式碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.