天天看點

作業42018.12.10作業

2018.12.10作業

4、定義兩個類,描述如下: [必做題]

4.1定義一個人類Person:

4.1.1定義一個方法sayHello(),可以向對方發出問候語“hello,my name is XXX”

4.1.2有三個屬性:名字、身高、體重

4.2定義一個PersonCreate類:

4.2.1建立兩個對象,分别是zhangsan,33歲,1.73;lishi,44,1.74

4.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();
	}

}