Java的类的练习

package aa4;

public class People{

//成员变量

private String name;//姓名name

private int age;//年龄age

private String sex;//性别sex

private double height;//身高height

private double weight;//体重weight

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 + "代码";

}

public String introduce() {

return name +"年龄"+ age + "岁,性别"+sex+"身高"+height+"米,体重"+weight+"千克";

}

public double getBMI() {

return weight/(height*height);

}

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;

}

public People() {

}

}

Java的类的练习_第1张图片

Java的类的练习_第2张图片 

 

package aa4;

public class PeopleTest {

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);

}

}

Java的类的练习_第3张图片

Java的类的练习_第4张图片 

 

你可能感兴趣的:(java,开发语言,android)