Files
javaSE251223/day06/src/com/inmind/object_private03/StudentTest.java

38 lines
1.1 KiB
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.object_private03;
/*
为了学生类的代码的安全性,要使用封装
封装的步骤:
1.使用private修饰符
2.提供对应属性的getXXX+setXXX方法
private权限修饰符最小一个权限被它修饰的内容只能够在本类中访问,
private只能修饰成员变量和成员方法,表示私有化。
成员方法:处于成员位置的非静态方法。
非静态不被static修饰的方法
*/
public class StudentTest {
public static void main(String[] args) {
//创建一个学生对象
Student s = new Student();
System.out.println(s);
s.study("java");
//s.属性 = 值
//对象的属性的设置值
s.setId(1);
s.setAge(-20);
s.setName("李四");
s.study("java");
//对象的属性的获取值
System.out.println(s.getAge());
System.out.println(s.getId());
System.out.println(s.getName());
//使用满参构造方法创建出学生对象2王五22
Student s1 = new Student("王五", 22, 2);
s1.study("python");
}
}