当前位置:   article > 正文

面向对象入门实例:小明去考试1.1版本加入了封装

小明与选择题csdn

 

运行结果

  1. D:\java312\xm>javac -encoding utf-8 -d classes src/*.java
  2. D:\java312\xm>java -classpath .;classes com.dayuanit.xm.test.TestDemo
  3. 家住天墉城十六街区的15岁的小明骑着价值为1990元的黄色的ofo自行车去考试,考试地点是
  4. 长江路199
  5. 小明做选择题1
  6. 小明做选择题2
  7. 小明做选择题3
  8. 小明做选择题4
  9. 小明做选择题5
  10. 小明做判断题1
  11. 小明做判断题2
  12. 小明做判断题3
  13. 小明做判断题4
  14. 小明做判断题5
  15. 小明做判断题6
  16. 小明做判断题7
  17. 小明做判断题8
  18. 小明做判断题9
  19. 小明做判断题10
  20. D:\java312\xm>

 

  1. package com.dayuanit.xm.test;
  2. import com.dayuanit.xm.user.Person;
  3. import com.dayuanit.xm.edu.*;
  4. import com.dayuanit.xm.tools.Bike;
  5. public class TestDemo {
  6. public static void main(String[] args) {
  7. Person person = new Person();
  8. person.setName("小明");
  9. person.setAge(15);
  10. person.setAddress("天墉城十六街区");
  11. Bike bike = new Bike();
  12. bike.setBrand("ofo自行车");
  13. bike.setPrice(1990);
  14. bike.setColor("黄色的");
  15. School school = new School();
  16. school.setName("外国语中学");
  17. school.setAddress("长江路199号");
  18. person.goToSchool(bike, school);
  19. Content content = new Content();
  20. content.setChioce(5);
  21. content.setJudge(10);
  22. person.exam(content);
  23. }
  24. }
  1. package com.dayuanit.xm.user;
  2. import com.dayuanit.xm.tools.Bike;
  3. import com.dayuanit.xm.edu.*;
  4. public class Person {
  5. private String name;
  6. private int age;
  7. private String address;
  8. public Person() {
  9. }
  10. public Person(String name, int age, String address) {
  11. this.name = name;
  12. this.age = age;
  13. this.address = address;
  14. }
  15. public void goToSchool(Bike bike, School school) {
  16. bike.move(this, school);//将对象整个传给move方法
  17. }
  18. public void exam(Content content) {
  19. content.exam(this);//将this对象传给exam方法
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public String getName() {
  25. return name;
  26. }
  27. public void setAge(int age) {
  28. this.age = age;
  29. }
  30. public int getAge() {
  31. return age;
  32. }
  33. public void setAddress(String address) {
  34. this.address = address;
  35. }
  36. public String getAddress() {
  37. return address;
  38. }
  39. }
  1. package com.dayuanit.xm.tools;
  2. import com.dayuanit.xm.user.Person;
  3. import com.dayuanit.xm.edu.School;
  4. public class Bike {
  5. int price;
  6. String brand;
  7. String color;
  8. public Bike() {
  9. }
  10. public Bike(int price, String brand, String color) {
  11. this.price = price;
  12. this.brand = brand;
  13. this.color = color;
  14. }
  15. public void move(Person person, School school) {
  16. System.out.println("家住"
  17. + person.getAddress()
  18. + "的"
  19. + person.getAge()
  20. + "岁的"
  21. + person.getName()
  22. + "骑着价值为"
  23. + price
  24. + "元的"
  25. + color
  26. + brand
  27. + "去考试,"
  28. + "考试地点是"
  29. + school.getAddress());
  30. }
  31. public void setPrice(int price) {
  32. this.price = price;
  33. }
  34. public int getPrice() {
  35. return price = price;
  36. }
  37. public void setBrand(String brand) {
  38. this.brand = brand;
  39. }
  40. public String getBrand() {
  41. return brand;
  42. }
  43. public void setColor(String color) {
  44. this.color = color;
  45. }
  46. public String getColor() {
  47. return color;
  48. }
  49. }

 

  1. package com.dayuanit.xm.edu;
  2. public class School {
  3. String address;
  4. String name;
  5. public School() {
  6. }
  7. public School(String address, String name) {
  8. this.address = address;
  9. this.name = name;
  10. }
  11. public void setAddress(String address) {
  12. this.address = address;
  13. }
  14. public String getAddress() {
  15. return address;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. }

 

  1. package com.dayuanit.xm.edu;
  2. import com.dayuanit.xm.user.Person;
  3. public class Content {
  4. int chioce;
  5. int judge;
  6. public Content() {
  7. }
  8. public Content(int chioce, int judge) {
  9. this.chioce = chioce;
  10. this.judge = judge;
  11. }
  12. public void exam(Person person) {
  13. for(int x = 1; x <= chioce; x++) {
  14. System.out.println(person.getName()
  15. + "做选择题"
  16. + x);
  17. }
  18. for(int x = 1; x <= judge; x++) {
  19. System.out.println(person.getName()
  20. + "做判断题"
  21. + x);
  22. }
  23. }
  24. public void setChioce(int chioce) {
  25. this.chioce = chioce;
  26. }
  27. public int getChioce() {
  28. return chioce;
  29. }
  30. public void setJudge(int judge) {
  31. this.judge = judge;
  32. }
  33. public int getJudge() {
  34. return judge;
  35. }
  36. }

 

编译用javac -encoding utf-8 -d classes src/*.java

-d表示把编译的自解码放在后面制定的文件夹javac -d . xxx.javayun

运行java -classpath .; classes com.dayuanit.xm.test.TestDemo

 

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/237710
推荐阅读
相关标签
  

闽ICP备14008679号