Person
public class Person {
String name; // 成员变量默认的修饰符是default,只能在当前类或者同一个包中访问
public void showName() {
System.out.println("我是" + this.name);
}
}
Demo01Anonymous
/**
* 匿名对象
* 普通标准格式
* 类名称 变量 = new 类();
*
* 匿名:new 类()
* 注意事项,每一次都是新建的对象,所以一般在只需要使用一次的场合下使用
*/
public class Demo01Anonymous {
public static void main(String[] args) {
// 普通
Person one = new Person();
one.name = "杨幂";
one.showName();
// 匿名
new Person().name = "范冰冰";
new Person().showName(); // null,因为冰冰已经凉了
}
}