封装
来源:互联网 发布:源码是啥 编辑:程序博客网 时间:2024/05/07 06:26
概念:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
好处:
- 只能通过规定的方法访问数据
- 隐藏类的实例细节,方便修改和实现
package com.imooc;public class Telphone { private float screen; private float cpu; private float mem; public Telphone(){ System.out.println("无参的构造函数执行了"); } public Telphone(float newScreen,float newCpu,float newMem){ System.out.println("有参的构造函数执行了!"); if(newScreen<3.5){ System.out.println("输入尺寸错误,默认为3.5"); screen=3.5f; } else{ screen=newScreen; } cpu=newCpu; mem=newMem; } public void setScreen(float newScreen){ screen=newScreen; } public float getScreen(){ System.out.println("screen="+screen); return screen; }}
package com.imooc;public class InitailTelphone { public static void main(String[] args) { // TODO Auto-generated method stub Telphone phone=new Telphone(); //Telphone phone2=new Telphone(1.5f,2.0f,1.2f); phone.setScreen(5.5f); phone.getScreen(); }}
0 0
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 封装
- 转:eclipse编写python程序解决中文乱码问题
- 关于树形结构左右值编码的数据库设计实现
- POJ 3418 Quadratic Functions 可能会
- pthread_create 未定义的引用
- 【JZOJ3214】【SDOI2013】方程
- 封装
- 读书笔记 effective c++ Item 11 在operator=中处理自我赋值
- 用stringByAddingPercentEncodingWithAllowedCharacters取代CFURLCreateStringByAddingPercentEscapes
- 文件夹操作
- NullReferenceException
- 总结一下 Spring的IOC、DI
- CSS完成一个带跳转功能的导航栏页面
- 经典排序算法 - 冒泡排序Bubble sort
- UVA 208 Firetruck 消防车(回溯 + 剪枝)