package xiaofu;
//成员变量
public class People {
private String name;
private int age;
private String sex;
private double height;
private double weight;
//对外提供getName方法
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//成员方法
public String code(String language) {
return name+"正在写"+language+"代码";
}
//做个人介绍 方法名:introduce 传入参数为空
//返回参数:”某某某年龄x岁,性别x,身高x米,体重x千克“
public String inttroduce() {
return name + "年龄" + age + "岁,性别" + age + ",身高" + sex + "米,体重" + weight + "千克";
public double getBMI() {
return weight/(height*height);
}
//构造方法
public People() {
}
public People(String name,int age,String sex) {
this.name=name;
this.age=age;
this.sex=sex;
}
public People(String name, int age, String sex, double height, double weight) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
this.weight = weight;
}
package xiaofu;
public class PeopleTast {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义一个people类型的引用,用new关键词调用people类,形成people1
//使用有三个参数的构造方法新建对象
People people1 = new People("张三",18,"女");
String code =people1.code("C语言");//调用成员方法code将返回参数存入code变量
System.out.println(code);
//使用setter方法设置身高体重
people1.setHeight(1.65);
people1.setWeight(46.5);
//使用成员方法geiBMI将返回参数存入/BMI变量
double bmi =people1.getBMI();
System.out.println(bmi);
//使用有五个参数的构造方法新建对象
People people2 =new People("王五",18,"女",1.65,46.5);
//使用成员方法introduce将返回参数存入introduce变量
String introduce1 =people1.introduce();
String introduce2 =people2.introduce();
System.out.println(introduce1);
System.out.println(introduce2);
}