创建一个Person类,声明属性和行为,在测试类中实例化对象,并通过调用对象的方法打印以下内容:
“小王是一个男胖子,体重200.0kg。某天他开始减肥,经过不懈努力,减了40kg。减肥后的体重是160.0kg。”
package lianxi01;
public class Person {
public String name;
public char sex;
public double weight;
public Person(){
}
//初始化成员变量
public Person(String name, char sex, double weight) {
super();
this.name = name;
this.sex = sex;
this.weight = weight;
}
public void jianfei(double i){
String s = "他";
if(sex == '女'){
s = "她";
}
System.out.println(name+"是一个"+sex+"胖子,体重"+weight+"kg。某天"+s+"开始减肥,经过不懈努力,减了"+i+"kg。减肥后的体重是"+(weight-i)+"kg。");
}
}
package lianxi01;
public class Test {
public static void main(String[] args) {
// Person p = new Person();
// p.name = “小王”;
// p.sex = ‘男’;
// p.weight = 200;
Person p = new Person("小王",'男',200);
p.jianfei(40);
Person p2 = new Person("铁锤",'女',260);
p2.jianfei(42);
}
}