java是一种面向对象的语言,面向对象的语言具有三大特征:封装、继承、多态(有的书上称之为四大特征:封装、继承、多态、抽象),那么何为封装?今天我们就来了解一下
先建立一个Student类
class Student
{
String name;
int id;
int age;
public Student() //定义一个不带参数的构造函数
{
}
{
String name;
int id;
int age;
public Student() //定义一个不带参数的构造函数
{
}
public Student(String name,int id, int age) //定义一个带三个参数的构造函数
{
this.name = name;
this.id = id;
this.age = age;
}
{
this.name = name;
this.id = id;
this.age = age;
}
public void study()
{
System.out.println("student study");
}
{
System.out.println("student study");
}
}
class Student_test
{
public static void main(String [] args)
{
Student stu = new Student(); //定义一个Student类的对象stu并调用不带参数的构造函数赋初值
System.out.println(stu.name + " , "+ stu.id +" ," + stu.age );
Student stu1 = new Student("张三",1001,20); //定义一个Student类的对象stu1并调用带3个参数的构造函数初始化
System.out.println(stu1.name + " , "+ stu1.id +" ," + stu1.age );
}
}
{
public static void main(String [] args)
{
Student stu = new Student(); //定义一个Student类的对象stu并调用不带参数的构造函数赋初值
System.out.println(stu.name + " , "+ stu.id +" ," + stu.age );
Student stu1 = new Student("张三",1001,20); //定义一个Student类的对象stu1并调用带3个参数的构造函数初始化
System.out.println(stu1.name + " , "+ stu1.id +" ," + stu1.age );
}
}
这里我们定义了两个Student类的对象stu和stu1,而无论是stu还是stu1,都可以调用类中的成员变量name , id , age ,这就好比只要是进入小区中的人都能进你家,而进入小区的人不一定每一个都是好人,如果坏人进入你家会造成财产损失,而在编程语言中,如果每个对象都能随意访问类中的成员,则容易造成数据泄露,不利于数据安全和维护,所以,我们需要给它提供一种方法,只有通过了身份验证,才能进入你的家中访问。
将上述程序修改成:
class Student
{
private String name;
private int id;
private int age;
public Student() //定义一个不带参数的构造函数
{
}
{
private String name;
private int id;
private int age;
public Student() //定义一个不带参数的构造函数
{
}
public Student(String name,int id, int age) //定义一个带三个参数的构造函数
{
this.name = name;
this.id = id;
this.age = age;
}
{
this.name = name;
this.id = id;
this.age = age;
}
public void SetName(String name) //设置Name
{
this.name = name;
{
this.name = name;
}
public String getName() //获取name
{
return this.name;
}
{
return this.name;
}
public void SetId(int id) //设置id
{
this.id = id;
{
this.id = id;
}
public int getId() //获取id
{
return this.id;
}
{
return this.id;
}
public void SetAge(int age) //设置age
{
this.age = age;
{
this.age = age;
}
public int getAge() //获取age
{
return this.age;
}
{
return this.age;
}
public void study()
{
System.out.println("student study");
}
{
System.out.println("student study");
}
}
这个时候,再通过System.out.println(stu.age) 调用类中的成员编译就会报错,因为Student类中的成员变量已被定义成privare(私有的),不允许对象直接通过“ 对象名.成员变量”这种方式来访问,只提过一个公共的统一的接口,类中的对象必须通过这个接口才能访问类中的私有成员。
将测试类中的代码修改如下:
class Student_test
{
public static void main(String [] args)
{
Student stu = new Student(); //定义一个Student类的对象stu并调用不带参数的构造函数赋初值
Student stu1 = new Student("张三",1001,20); //定义一个Student类的对象stu1并调用带3个参数的构造函数初始化
stu.setName("李四");
stu.setId(1002);
stu.setAge(21);
{
public static void main(String [] args)
{
Student stu = new Student(); //定义一个Student类的对象stu并调用不带参数的构造函数赋初值
Student stu1 = new Student("张三",1001,20); //定义一个Student类的对象stu1并调用带3个参数的构造函数初始化
stu.setName("李四");
stu.setId(1002);
stu.setAge(21);
System.out.println(stu.getName()+" " + stu.getId()+ " " + stu.getAge());
}
}
这时再编译则不会报错,而以前的System.out.println(对象名.成员变量名)则会报错,因为类中的成员变量都已经变成私有的了。这就是一个简单的程序封装!