Java是面向对象的三大特征 ,就是把对象的属性和操作(服务) 结合起来变成一个整体,并尽可能的去隐藏内部的信息
Java封装 对属性的封装
class BeautifulGirl {
//成员变量 用来存储数据
private String name;//姓名
private int age;//年龄
private double weight;//体重
private double height;//身高
private String color;//肤色
//无参构造 创建对象的时候 可以借助于无参构造方法
public BeautifulGirl() {
}
//有参构造 建对象的时候 可以借助于有参构造方法 对属性(成员变量)进行赋值
public BeautifulGirl(String name, int age, double weight, double height, String color) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
this.color = color;
}
//get方法 对变量进行取值
public String getName() {
return name;
}
//set方法 对变量进行赋值的
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Demo8 {
public static void main(String[] args) {
//要么使用有参的进行赋值
BeautifulGirl bg = new BeautifulGirl("哈哈", 28, 280, 280, "yellow");
//要么使用set方法进行赋值
bg.setName("嘻嘻");
System.out.println(bg.getName());//取值
}
}