Java面向对象编程(封装/继承/多态)实例解析
本文主要介绍了面向对象的三大特征实例解析,下面看看具体内容。
封装
封装一个Teacher和Student类
packagecom.hz.test; publicclassTeacher{ privateStringname; privateStringmajorDirection; privateStringteachCourse; privateintteachAge; publicTeacher(){ super(); } publicTeacher(Stringname,StringmajorDirection,StringteachCourse,intteachAge){ this.name=name; this.majorDirection=majorDirection; this.teachCourse=teachCourse; this.teachAge=teachAge; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetMajorDirection(){ returnmajorDirection; } publicvoidsetMajorDirection(StringmajorDirection){ this.majorDirection=majorDirection; } publicStringgetTeachCourse(){ returnteachCourse; } publicvoidsetTeachCourse(StringteachCourse){ this.teachCourse=teachCourse; } publicintgetTeachAge(){ returnteachAge; } publicvoidsetTeachAge(intteachAge){ this.teachAge=teachAge; } publicStringtoString(){ return"姓名="+getName()+",专业方向="+getMajorDirection() +",所教课程="+getTeachCourse()+",教龄="+getTeachAge(); } }
Student类
packagecom.hz.test; importjava.util.Arrays; /** *@authorztw * */ publicclassStudent{ privateStringname; privateintage; privateString[]courses; privateStringinterest; publicStudent(){ super(); } publicStudent(Stringname,intage,String[]courses,Stringinterest){ this.name=name; this.age=age; this.courses=courses; this.interest=interest; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetName(){ returnname; } publicvoidsetAge(intage){ if(age<0){ System.out.println("年龄不能为负值"); }else{ this.age=age; } } publicintgetAge(){ returnage; } publicvoidsetCourses(String[]courses){ this.courses=courses; } publicStringgetCourses(){ returnArrays.toString(courses); } publicvoidsetInterest(Stringinterest){ this.interest=interest; } publicStringgetInterest(){ returninterest; } publicStringtoString(){ return"姓名="+getName()+",年龄="+getAge()+",课程="+getCourses() +",兴趣="+getInterest(); } }
测试类
packagecom.hz.test; publicclassTest{ publicstaticvoidmain(String[]args){ Stringarr[]={"阿斯达","是的","大概","太诱惑"}; Studentstu=newStudent("张三",21,arr,"打球"); Teachertea=newTeacher("王五","阿斯达","阿斯达",99); System.out.println(stu); System.out.println(tea); } }
输出结果:
姓名=张三,年龄=21,课程=[阿斯达,是的,大概,太诱惑],兴趣=打球
姓名=王五,专业方向=阿斯达,所教课程=阿斯达,教龄=99
继承
定义Play,TaoistPriest,Master,Warrior
publicclassPlay{ Stringmain; publicPlay(Stringmain){ this.main=main; } publicvoidhitMonster(){ System.out.println(main+"打怪"); } }
/** *TaoistPriest:道士 *@authorztw * */ publicclassTaoistPriestextendsPlay{ { System.out.print("我是道士:"); } publicTaoistPriest(Stringmain){ super(main); } }
/** *Master:法师 *@authorztw * */ publicclassMasterextendsPlay{ { System.out.print("我是法师:"); } publicMaster(Stringmain){ super(main); } }
/** *Warrior:武士 *@authorztw * */ publicclassWarriorextendsPlay{ { System.out.print("我是武士:"); } publicWarrior(Stringmain){ super(main); } }
测试类
publicclassTest{ publicstaticvoidmain(String[]args){ TaoistPriesttp=newTaoistPriest("灵魂火符"); tp.hitMonster(); Masterm=newMaster("雷电术"); m.hitMonster(); Warriorw=newWarrior("烈火术"); w.hitMonster(); } }
输出结果:
我是道士:灵魂火符打怪 我是法师:雷电术打怪 我是武士:烈火术打怪
多态
服务器,客户端交互
LoginListener
publicinterfaceLoginListener{ publicvoidsucceed(Stringmsg); publicvoidfailed(Stringmsg); }
MyLoginListener
publicclassMyLoginListenerimplementsLoginListener{ publicvoidsucceed(Stringmsg){ System.out.println(msg); } publicvoidfailed(Stringmsg){ System.out.println(msg); } }
Server
publicclassServer{ publicvoidlogin(StringuserName,Stringpassword,LoginListenerlistener){ System.out.print("loading"); for(inti=0;i<10;i++){ try{ Thread.sleep(100*i); System.out.print("."); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(userName.equals("zhangsan")&&password.equals("123")){ if(listener!=null){ listener.succeed("登录成功"); } }else{ if(listener!=null){ listener.succeed("登录失败"); } } } }
测试类
publicclassLoginTest{ publicstaticvoidmain(String[]args){ Scannersc=newScanner(System.in); System.out.println("请输入用户名:"); StringuserName=sc.next(); System.out.println("请输入用户密码:"); Stringpassword=sc.next(); Serverserver=newServer(); server.login(userName,password,newMyLoginListener()); } }
输出结果
请输入用户名: zhangsan 请输入用户密码: 123 loading……….登录成功
以上就是本文关于Java面向对象编程(封装,继承,多态)实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:解析Java编程之Synchronized锁住的对象、Java语言面向对象编程思想之类与对象实例详解等,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!