第一種java
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,和agethis
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); } }