- public class Temp
- {
- public static void main(String[] args)
- {
- Person p = new Person("项羽", 2246);
- p.speak();
- }
- }
- class Person
- {
- private String name;
- private int age;
- Person(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- public void speak()
- {
- System.out.println("我叫" + this.name + ",今年 " + this.age + "岁");
- }
- }
复制代码
把this, 想象成p 就行了, 这个例子中, 把 this 替换想象成 p . 那就成了 p.name , p.age 这样好理解吧? |