java成员变量和成员方法复习

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

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值