面向对象(封装,继承,多态)

水深无声 2022-07-14 14:24 422阅读 0赞

封装

封装一个Teacher和Student类

  1. package com.hz.test;
  2. public class Teacher {
  3. private String name;
  4. private String majorDirection;
  5. private String teachCourse;
  6. private int teachAge;
  7. public Teacher() {
  8. super();
  9. }
  10. public Teacher(String name,String majorDirection,String teachCourse,int teachAge) {
  11. this.name = name;
  12. this.majorDirection = majorDirection;
  13. this.teachCourse = teachCourse;
  14. this.teachAge = teachAge;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getMajorDirection() {
  23. return majorDirection;
  24. }
  25. public void setMajorDirection(String majorDirection) {
  26. this.majorDirection = majorDirection;
  27. }
  28. public String getTeachCourse() {
  29. return teachCourse;
  30. }
  31. public void setTeachCourse(String teachCourse) {
  32. this.teachCourse = teachCourse;
  33. }
  34. public int getTeachAge() {
  35. return teachAge;
  36. }
  37. public void setTeachAge(int teachAge) {
  38. this.teachAge = teachAge;
  39. }
  40. public String toString() {
  41. return "姓名=" + getName() + ", 专业方向=" + getMajorDirection()
  42. + ", 所教课程=" + getTeachCourse() + ", 教龄=" + getTeachAge();
  43. }
  44. }

Student类

  1. package com.hz.test;
  2. import java.util.Arrays;
  3. /**
  4. * @author ztw
  5. *
  6. */
  7. public class Student {
  8. private String name;
  9. private int age;
  10. private String[] courses;
  11. private String interest;
  12. public Student() {
  13. super();
  14. }
  15. public Student(String name,int age,String[] courses,String interest) {
  16. this.name = name;
  17. this.age = age;
  18. this.courses = courses;
  19. this.interest = interest;
  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. if(age<0){
  29. System.out.println("年龄不能为负值");
  30. }else{
  31. this.age = age;
  32. }
  33. }
  34. public int getAge(){
  35. return age;
  36. }
  37. public void setCourses(String[] courses){
  38. this.courses = courses;
  39. }
  40. public String getCourses(){
  41. return Arrays.toString(courses);
  42. }
  43. public void setInterest(String interest){
  44. this.interest = interest;
  45. }
  46. public String getInterest(){
  47. return interest;
  48. }
  49. public String toString() {
  50. return "姓名=" + getName() + ", 年龄=" + getAge() + ", 课程=" + getCourses()
  51. + ", 兴趣=" + getInterest();
  52. }
  53. }

测试类

  1. package com.hz.test;
  2. public class Test {
  3. public static void main(String[] args) {
  4. String arr[] = {
  5. "阿斯达","是的","大概","太诱惑"};
  6. Student stu = new Student("张三",21,arr,"打球");
  7. Teacher tea = new Teacher("王五","阿斯达","阿斯达",99);
  8. System.out.println(stu);
  9. System.out.println(tea);
  10. }
  11. }

输出结果:

  • 姓名=张三, 年龄=21, 课程=[阿斯达, 是的, 大概, 太诱惑], 兴趣=打球
  • 姓名=王五, 专业方向=阿斯达, 所教课程=阿斯达, 教龄=99

继承

定义Play,TaoistPriest,Master,Warrior

  1. public class Play {
  2. String main;
  3. public Play(String main) {
  4. this.main = main;
  5. }
  6. public void hitMonster() {
  7. System.out.println(main+"打怪");
  8. }
  9. }
  10. /**
  11. * TaoistPriest:道士
  12. * @author ztw
  13. *
  14. */
  15. public class TaoistPriest extends Play {
  16. {
  17. System.out.print("我是道士:");
  18. }
  19. public TaoistPriest(String main) {
  20. super(main);
  21. }
  22. }
  23. /**
  24. * Master:法师
  25. * @author ztw
  26. *
  27. */
  28. public class Master extends Play{
  29. {
  30. System.out.print("我是法师:");
  31. }
  32. public Master(String main) {
  33. super(main);
  34. }
  35. }
  36. /**
  37. * Warrior:武士
  38. * @author ztw
  39. *
  40. */
  41. public class Warrior extends Play{
  42. {
  43. System.out.print("我是武士:");
  44. }
  45. public Warrior(String main) {
  46. super(main);
  47. }
  48. }

测试类

  1. public class Test {
  2. public static void main(String[] args) {
  3. TaoistPriest tp = new TaoistPriest("灵魂火符");
  4. tp.hitMonster();
  5. Master m = new Master("雷电术");
  6. m.hitMonster();
  7. Warrior w = new Warrior("烈火术");
  8. w.hitMonster();
  9. }
  10. }
  • 输出结果:

我是道士:灵魂火符打怪
我是法师:雷电术打怪
我是武士:烈火术打怪

多态

服务器,客户端交互

LoginListener

  1. public interface LoginListener {
  2. public void succeed(String msg);
  3. public void failed(String msg);
  4. }

MyLoginListener

  1. public class MyLoginListener implements LoginListener{
  2. public void succeed(String msg) {
  3. System.out.println(msg);
  4. }
  5. public void failed(String msg) {
  6. System.out.println(msg);
  7. }
  8. }

Server

  1. public class Server {
  2. public void login(String userName,String password,LoginListener listener) {
  3. System.out.print("loading");
  4. for (int i = 0; i < 10; i++) {
  5. try {
  6. Thread.sleep(100*i);
  7. System.out.print(".");
  8. } catch (InterruptedException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. }
  13. if(userName.equals("zhangsan") && password.equals("123")){
  14. if(listener!=null){
  15. listener.succeed("登录成功");
  16. }
  17. }else{
  18. if(listener!=null){
  19. listener.succeed("登录失败");
  20. }
  21. }
  22. }
  23. }

测试类

  1. public class LoginTest {
  2. public static void main(String[] args) {
  3. Scanner sc = new Scanner(System.in);
  4. System.out.println("请输入用户名:");
  5. String userName = sc.next();
  6. System.out.println("请输入用户密码:");
  7. String password = sc.next();
  8. Server server = new Server();
  9. server.login(userName, password, new MyLoginListener());
  10. }
  11. }
  • 输出结果

请输入用户名:
zhangsan
请输入用户密码:
123
loading……….登录成功

发表评论

表情:
评论列表 (有 0 条评论,422人围观)

还没有评论,来说两句吧...

相关阅读