public class Test {
public static void main(String args[]) {
String home = "南京市浦口区天花南路28号";
String school = "解放东路303号的北京东路育才小学";
Person person1 = new Person("小明", home);
Bike bikeOfo = new Bike("黄色","ofo共享单车");
//小明调用方法,解锁共享单车
person1.getBike(bikeOfo);
//小明开始朝着目的地骑车
person1.rideBike(home,school);
//小明开始考试
person1.test();
}
}
- 测试类
public class Person {
private String name;
private String home;
private Bike bike;
private String location;//所在地
public Person() {
}
public Person(String name, String home) {
this.name = name;
this.home = home;
}
//构造函数,放入名称和家庭地址
public void getBike(Bike bike) {
this.bike = bike;
System.out.println(name + "骑" + bike.getCol() + "的" + bike.getBra());
}
//获得一辆自行车
public void rideBike(String start, String end) {
bike.ride(start, end);
location = end;
}
//骑车,调用自行车的骑行方法,方法结束后将当前所在地变为终点。
public void test() {
if(location.equals("解放东路303号的北京东路育才小学")) {
System.out.println("----------------参加考试-----------------");
}else {
System.out.println("没到考场!!!!!");
}
}
//考试,判断当前所在地是否在学校,在学校就进行考试,否则显示没到
}
- person类
public class Bike {
private String colour;
private String brand;//品牌
public Bike() {
}
public Bike(String col, String bra) {
this.colour = col;
this.brand = bra;
}
public String getCol() {
return colour;
}
public String getBra() {
return brand;
}
public void ride(String start, String end) {
System.out.println("从【" + start + "】骑行到【" + end + "】");
}
}
- bike类
输出:
练习内容:类的组合使用,以及对象的属性封装。