第六章课堂,类的成员变量和构造,复习

package lianhua;

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

 

 

package lianhua;

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值