面向对象入门实例:小明去考试

本文通过一个具体的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,和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);
	}
}

转载于:https://my.oschina.net/u/3536141/blog/1031114

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值