2018.12.10作业
5、定义两个类,描述如下: [必做题]
5.1定义一个人类Person:
5.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
5.1.2有三个属性:名字、身高、体重
5.1.3通过构造方法,分别给三个属性赋值
5.2定义一个Constructor类:
5.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
5.2.2分别调用对象的sayHello()方法。
public class People {
String name;
int age;
double height;
void sayHello(){
System.out.println("hello,my name is "+this.name+"\n身高是:"+this.height+"\n年龄是:"+this.age);
}
void add(String name,int age,double height) {
this.name =name;
this.age = age;
this.height = height;
}
}
public class People1 {
public static void main(String[] args) {
People p1 = new People();
p1.add("张三",33,1.73);
p1.sayHello();
People p2 = new People();
p2.add("李四",44,1.74);
p2.sayHello();
}
}