java编程题
1.创建类Person,其中存储的成员数据为:age(int),sex(boolean),weight(int),至少有一个构造函数可以初始化这三个属性值,同时提供获取这...
1. 创建类 Person,其中存储的成员数据为:age(int),sex(boolean),weight(int),至少有一个构造函数可以初始化这三个属性值,同时提供获取这三个属性值的public方法。
展开
1个回答
展开全部
public class Person {
private int age;
private boolean sex;
private int weight;
public Person(int age, boolean sex, int weight) {
this.age = age;
this.sex = sex;
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
追问
怎么调用
追答
public class TestPerson {
public static void main(String[] args) {
Person p1 = new Person(21, false, 80);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
p1.setAge(22);
p1.setSex(true);
p1.setWeight(60);
System.out.println(p1.getAge());
System.out.println(p1.isSex());
System.out.println(p1.getWeight());
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |