1. 程式人生 > 實用技巧 >new 操作符到底做了什麼

new 操作符到底做了什麼

1、toString就是獲取物件中的資料

public class Student {
    private String name;
    private  int age;
    private boolean sex;
    private Date birthday;
    private Map<String,Object> location;
    private String[] hobbies;
    private List<String> skills;
    private Pet pet;

    public String getName() {
        
return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSex() { return sex; } public void setSex(boolean sex) {
this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Map<String, Object> getLocation() { return location; } public void setLocation(Map<String, Object> location) {
this.location = location; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public List<String> getSkills() { return skills; } public void setSkills(List<String> skills) { this.skills = skills; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", location=" + location + ", hobbies=" + Arrays.toString(hobbies) + ", skills=" + skills + ", pet=" + pet + '}'; } }

隱式呼叫方法

 Student student; 或者Student student = new Student();
 System.out.println(student);