- class Person
- {
- private String name;
- public Person(String n)//2
- {
- name = n;//3
- }
-
- public void setName(String n)
- {
- name = n; //重新赋值
- }
- public String getName()//4
- {
- return name;
- }
- }
- class PersonTest
- {
- public static void main(String[] args)
- {
- //创建对象
-
- Person p2 = new Person("我自己是帅哥");//1
- p2.setName("你好啊");
- System.out.println(p2.getName());//5
- }
- }
复制代码 |