Java的类的练习

文章展示了如何在Java中创建一个名为People的类,包含姓名、年龄、性别、身高和体重等属性,以及对应的getter和setter方法。还定义了计算BMI的方法和介绍个人信息的方法。在测试类PeopleTest中,通过构造方法创建People对象并调用其方法进行操作,如设置属性、计算BMI并打印结果。
摘要由CSDN通过智能技术生成

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() {

}

}

 

 

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

}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若曦世

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值