您的位置:首页 > 其它

重写 去考试的小明题目

2017-06-30 00:00 190 查看
一边思考 一边写 先写person类属性和方法 方法内容先不写 再写方法里 用到的自定义类 进行传值用

根据这样的逻辑 再往下写其他的 自定义类 需要注意的是 打包时功能相同 到一个包中 导入时要精确导入

用到哪个包中的类 就导入哪个包中的类 单词拼写错误 容易出现找不到符号的错误 或者牵涉到传值时 不会有显示输出 贴一张出现bug的图片



重写一遍的感受是 有些细节还需要注意 比如 测试类的对象实例化的格式和顺序 要有层次感 贴代码如下

package com.dayuanit.test;
import com.dayuanit.person.Person;
import com.dayuanit.goschool.*;
import com.dayuanit.paper.Paper;

public class PersonGoTest {
public static void main(String[] args) {
Person person = new Person(15, "小明", "家住天润城");
Bicycle bicycle = new Bicycle(1880, "yellow", "ofo");
School school = new School("花果山水帘洞", "悟空大闹天宫学校");
Paper paper = new Paper(5, 10);

person.goSchool(bicycle, school);
person.goTest(paper);
}

}

Person类代码如下 重写时 其调用其他类的方法时 写的比上次更顺利 没有犹豫 知道传值传的内容是什么

调用很干脆 知道要不要加S.O.P

package com.dayuanit.person;
import com.dayuanit.goschool.*;
import com.dayuanit.paper.Paper;

public class Person {
private String name;
private int age;
private String home;

public Person() {

}

public Person(int age, String name, String home) {
this.name = name;
this.age = age;
this.home = home;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public String getHome() {
return home;
}

public void goSchool(Bicycle bicycle, School school) {
bicycle.runBicycle(this);
school.getAddress();
school.getSchoolName();
System.out.println("考试");

}

public void goTest(Paper paper) {
paper.doChoice(this);
paper.doCheck(name);// paper.doCheck(this.name); paper.doCheck(getName());
}

}

this 表示当前对象 在 runBicycle();方法中传的值是Person类

package com.dayuanit.goschool;
import com.dayuanit.person.Person;

public class Bicycle {
private String brand;
private int price;
private String color;

public Bicycle() {

}

public Bicycle(int price, String color, String brand) {
this.price = price;
this.color = color;
this.brand = brand;
}

public String getBrand() {
return brand;

}

public String getColor() {
return color;
}

public int getPrice() {
return price;
}

public void runBicycle(Person person) {
System.out.print(person.getAge() + person.getName() + person.getHome() +"骑着一辆" + "价值为" + this.price + color + getBrand());
} // 在不同包中 (private 修饰的类的成员变量) 其他类来调用Person类变量  只能用 get 方法来读取

}

Person类属性私有 呼应上述注释

package com.dayuanit.paper;
import com.dayuanit.person.Person;

public class Paper {
private int choice;
private int check;

public Paper() {

}

public Paper(int choice, int check) {
this.choice = choice;
this.check = check;
}

public void setChoice() {
this.choice = choice;
}

public void setCheck() {
this.check = check;
}

public int getChoice() {
return choice;
}

public int getCheck() {
return check;
}

public void doChoice(Person person) {
for(int i = 0; i < choice; i++) {
System.out.println(person.getName() + "DoChoice" + i);
}
}

public void doCheck(String username) {
for (int i = 0; i < check; i++) {
System.out.println(username + "DoCheck" + i);
}
}

}

运行结果 如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: