第一种
public class TestDemo {
public static void main(String[] args) {
Bike bike = new Bike("ofo自行车", "黄色的", 1880);
School school = new School("外国语中学", "长江路199号的浦口");
Person person = new Person("小明", 15, "天墉城十六街区");
person.goToSchool(bike, school);
Content content = new Content(5, 10);
person.exam(content);
}
}
public class Bike {
String name;
String color;
int price;
public Bike() {
}
public Bike(String name, String color, int price) {
this.name = name;
this.color = color;
this.price = price;
}
public void move(Person person, School school) {
System.out.println(person.age
+ "岁的"
+ person.name
+ "骑着一辆价值"
+ price
+ color
+ name
+ "去"
+ school.address
+ school.name);
}
}
public class School {
String name;
String address;
public School() {
}
public School(String name, String address) {
this.name = name;
this.address = address;
}
}
public class Content {
int chioce;
int judge;
public Content() {
}
public Content(int chioce, int judge) {
this.chioce = chioce;
this.judge = judge;
}
public void exam(Person person) {
for(int x = 0; x < chioce; x++) {
System.out.println(person.age
+ person.name
+ "做"
+ "选择题"
+ x);
}
for(int x = 0; x < judge; x++) {
System.out.println(person.name
+ "做"
+ "判断题"
+ x);
}
}
}
public class Person {
String name;
int age;
String address;
public Person() {
}
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void goToSchool(Bike bike, School school) {
bike.move(this, school);//把小明对象都传给move方法
}
public void exam(Content content) {
content.exam(this);
}
}
一种是把整个对象this都传给move方法,另外一种是只传name,和age
public class Person {
String name;
int age;
String address;
Bike bike;
public Person() {
}
public Person(String name, int age, String address, Bike bike) {
this.name = name;
this.age = age;
this.address = address;
this.bike = bike;
}
public void goToSchool(School school) {
bike.move(age, name, school);
}
public void exam(Content content) {
content.exam(name);
}
}
public class School {
String name;
String address;
public School() {
}
public School(String name, String address) {
this.name = name;
this.address = address;
}
}
public class Bike {
String name;
String color;
int price;
public Bike() {
}
public Bike(String name, String color, int price) {
this.name = name;
this.color = color;
this.price = price;
}
public void move(int userage, String username, School school) {
System.out.println(userage
+ username
+ "他要骑着价值为"
+ price
+ color
+ name
+ "去考试,"
+ school.address
+ school.name);
}
}
public class Content {
int chioce;
int judge;
public Content() {
}
public Content(int chioce, int judge) {
this.chioce = chioce;
this.judge = judge;
}
public void exam(String username) {
for(int x = 0; x < chioce; x++) {
System.out.println(username
+ "做选择题"
+ x);
}
for(int x = 0; x < judge; x++) {
System.out.println(username
+ "做判断题"
+ x);
}
}
}
public class TestDemo {
public static void main(String[] args) {
Bike bike = new Bike("ofo自行车", "黄色的", 1880);
School school = new School("外国语中学", "长江路199号的浦口");
Person person = new Person("小明", 15, "天墉城十六街区");
person.goToSchool(bike, school);
Content content = new Content(5, 10);
person.exam(content);
}
}