【Java学习】-Constructor

public class Main {

public static void main(String[] args) {
Human human1 = new Human("Rick", 65, 70);
Human human2 = new Human("Morty", 66, 77);
human2.eat();
human1.drink();
}
}

public class Human {

String name;
int age;
double weight;

Human(String name, int age, double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}

void eat() {
System.out.println(this.name + " is eating");
}
void drink() {
System.out.println(this.name + " is drinking");
}
}
上一篇:js 构造函数 constructor


下一篇:原型与原型链 - 构造函数与instanceof