Files
javaSE-0113/day06/src/com/inmind/object01/Demo04.java

30 lines
952 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.object01;
/*
面向对象之封装的作用:类的安全性
如何封装呢??
1.使用private关键字修饰成员变量
2.使用get/setXXX方法来对private修饰的成员变量进行取值和赋值
*/
public class Demo04 {
public static void main(String[] args) {
//创建一个学生,并给学生设置属性值,展示该学生
Student s = new Student("lisi");
System.out.println("s的地址"+s);//s中保存的是地址
//设置属性
//s.name = "张三";
s.setName("张三");
//s.age = -23;//有点不安全
s.setAge(23);
System.out.println(s.getName());
System.out.println(s.getAge());
s.show();
System.out.println("-------------");
//通过有参构造,创建学生对象
Student s1 = new Student("王五", 22);
s1.show();
s1.setName("王五1");
s1.show();
}
}