1.封装实现的步骤
封装就是隐藏属性,通过我们的方法来得到属性
具体样例:
package com.imooc;
public class Inital {
public static void main(String[] args) {
// TODO Auto-generated method stub
hello phone = new hello();
hello phone2=new hello(5.0f,1.4f,2.0f);
phone2.cpu=35;
phone2.setScreen(6.8f);
System.out.println("screen:"+phone2.getScreen());
System.out.println(phone2.cpu);
}
}
package com.imooc;
import java.util.Scanner;
import java.util.Arrays;
public class hello {
//属性(成员变量)有什么
private float screen;
float cpu;
private float mem;
private int var =14;
public float getScreen() {
return screen;
}
public void setScreen(float newScreen) {
screen = newScreen;
}
//方法 干什么
public hello() {
System.out.println("输出流");
}
public hello(float newScreen,float newCpu,float newMem) {
screen=newScreen;
cpu=newCpu;
newMem=mem;
System.out.println(screen);
}