来到黑马一周多了,学习了很多知识,让我觉得自己提升很大,但是很多基础的只是还是没有掌握熟练,学到了第七天,已经讲到了面向对象和构造方法,让我印象最深的就是定义一个女朋友类。女朋友的属性包含:姓名,身高,体重。行为包含:洗衣服wash(),做饭cook()。另外定义一个用于展示三个属性值的show()方法。请在测试类中通过有参构造方法创建对象并赋值,然后分别调用展示方法、洗衣服方法和做饭方法。
具体代码如下:
标准类:
package HomeWork;
public class GirlFriend {
private String name;
private int height;
private int weight;
public GirlFriend() {
}
public GirlFriend(String name, int height, int weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void show() {
System.out.println("我女朋友叫:" + name + "," + "她的身高是:" + height + "她的体重是:" + weight);
}
public void wash() {
System.out.println("我女朋友帮我洗衣服");
}
public void cook() {
System.out.println("我女朋友帮我做饭");
}
}
测试类:
package HomeWork;
public class GirlFriend1 {
public static void main(String[] args) {
GirlFriend i=new GirlFriend("111",222,333);
i.show();
i.cook();
i.wash();
}
}
完成了这个作业以后仿佛我有了一个女朋友哈哈~~
|
|