class Person {
private String name;// private 对外不可修改,对类内部是可见的;setter getter 设置或获得属性;
private int age;// 98% 都需要封装的;
// 构造方法的重载;
public Person(){
name = "无名氏";
age = -2;
}
public Person(String n){
name = "赵高";
}
// 构造方法:不能有返回值;
public Person(String n,int a){
name = n;
age = a;
}
public void tell(){
System.out.println("姓名:" + name + ",年龄:" + age);
}
}
public class ImoocStudent {
public static void main(String[] args) throws Exception{
//Person per = new Person();
//Person per = new Person("牛二");
// Person per = new Person("张三",99);// 实例化方法一
// per.tell();
new Person("匿名大侠",200).tell();// 匿名对象,使用完会被gc回收;
}
}
标签:name,构造方法,5.8,per,Person,匿名,public,String
From: https://www.cnblogs.com/pansidong/p/17451027.html