面向對象入門實例:小明去考試1.1版本加入了封裝

 

運行結果java

D:\java312\xm>javac -encoding utf-8 -d classes src/*.java

D:\java312\xm>java -classpath .;classes com.dayuanit.xm.test.TestDemo
家住天墉城十六街區的15歲的小明騎着價值爲1990元的黃色的ofo自行車去考試,考試地點是
長江路199號
小明作選擇題1
小明作選擇題2
小明作選擇題3
小明作選擇題4
小明作選擇題5
小明作判斷題1
小明作判斷題2
小明作判斷題3
小明作判斷題4
小明作判斷題5
小明作判斷題6
小明作判斷題7
小明作判斷題8
小明作判斷題9
小明作判斷題10

D:\java312\xm>

 

package com.dayuanit.xm.test;

import com.dayuanit.xm.user.Person;
import com.dayuanit.xm.edu.*;
import com.dayuanit.xm.tools.Bike;

public class TestDemo {
	public static void main(String[] args) {
		 Person person = new Person();
		 person.setName("小明");
		 person.setAge(15);
		 person.setAddress("天墉城十六街區");

		 Bike bike = new Bike();
		 bike.setBrand("ofo自行車");
		 bike.setPrice(1990);
		 bike.setColor("黃色的");

		 School school = new School();
		 school.setName("外國語中學");
		 school.setAddress("長江路199號");

		 person.goToSchool(bike, school);

		 Content content = new Content();
		 content.setChioce(5);
		 content.setJudge(10);
		 person.exam(content);
	}
}
package com.dayuanit.xm.user;

import com.dayuanit.xm.tools.Bike;
import com.dayuanit.xm.edu.*;

public class Person {
	private String name;
	private int age;
	private 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對象傳給exam方法
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}
}
package com.dayuanit.xm.tools;

import com.dayuanit.xm.user.Person;
import com.dayuanit.xm.edu.School;

public class Bike {
	int price;
	String brand;
	String color;

	public Bike() {

	}

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

	public void move(Person person, School school) {
		System.out.println("家住"
			+ person.getAddress()
			+ "的"
			+ person.getAge() 
			+ "歲的"
			+ person.getName()
			+ "騎着價值爲"
			+ price 
			+ "元的"
			+ color
			+ brand 
			+ "去考試,"
			+ "考試地點是"
			+ school.getAddress());
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getPrice() {
		return price = price;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getBrand() {
		return brand;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getColor() {
		return color;
	}
}

 

package com.dayuanit.xm.edu;

public class School {
	String address;
	String name;

	public School() {

	}

	public School(String address, String name) {
		this.address = address;
		this.name = name;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

 

package com.dayuanit.xm.edu;

import com.dayuanit.xm.user.Person;

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 = 1; x <= chioce; x++) {
			System.out.println(person.getName()
				+ "作選擇題"
				+ x);
		}

		for(int x = 1; x <= judge; x++) {
			System.out.println(person.getName()
				+ "作判斷題"
				+ x);
		}
	}

	public void setChioce(int chioce) {
		this.chioce = chioce;
	}

	public int getChioce() {
		return chioce;
	}

	public void setJudge(int judge) {
		this.judge = judge;
	}

	public int getJudge() {
		return judge;
	}
}

 

編譯用javac -encoding utf-8 -d classes src/*.javathis

-d表示把編譯的自解碼放在後面制定的文件夾javac -d . xxx.javayunspa

運行java -classpath .; classes com.dayuanit.xm.test.TestDemocode

相關文章
相關標籤/搜索