
public class Puppy{public Puppy(){}public Puppy(String name){// 这个构造器仅有一个参数:name}}
调用
Person person = new Person();
构造
package com.example.demo;public class Person {private String name;private int age;public Person() {}public Person(int age) {this.name = name;this.age = age;}public Person(String name, int age) {this.name = name;this.age = age;}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;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}}
private私有变量,意思是改变这个变量要用set()方法改变,get()方法直接返回
构造器、构造方法、constructor没有参数
构造器、构造方法、constructor有参数
构造器、构造方法、constructor定义了有参数构造器,就要再写一个没有参数的构造器
get()、set(),可以判断这个传入的数符不符合规矩
this,当方法传入时,用(String name)要this.name = name,不可以name.name
构造器里调用this();说明调用无参构造器
构造器里调用this(age);说明调用有参构造器
this();只能在第一行
this();只可以调用构造器数-1次
this();1个构造器不可以同时调用2个构造器
重写toString方法,可以看到数据Student{a=’s’, b=1}
重写hashCode
重写equals方法,可以比较内容,而不是对比对象