Java面向对象编程(封装/继承/多态)实例解析
程序员文章站
2024-02-19 11:52:58
本文主要介绍了面向对象的三大特征实例解析,下面看看具体内容。
封装
封装一个teacher和student类
package com.hz.test;...
本文主要介绍了面向对象的三大特征实例解析,下面看看具体内容。
封装
封装一个teacher和student类
package com.hz.test; public class teacher { private string name; private string majordirection; private string teachcourse; private int teachage; public teacher() { super(); } public teacher(string name,string majordirection,string teachcourse,int teachage) { this.name = name; this.majordirection = majordirection; this.teachcourse = teachcourse; this.teachage = teachage; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getmajordirection() { return majordirection; } public void setmajordirection(string majordirection) { this.majordirection = majordirection; } public string getteachcourse() { return teachcourse; } public void setteachcourse(string teachcourse) { this.teachcourse = teachcourse; } public int getteachage() { return teachage; } public void setteachage(int teachage) { this.teachage = teachage; } public string tostring() { return "姓名=" + getname() + ", 专业方向=" + getmajordirection() + ", 所教课程=" + getteachcourse() + ", 教龄=" + getteachage(); } }
student类
package com.hz.test; import java.util.arrays; /** * @author ztw * */ public class student { private string name; private int age; private string[] courses; private string interest; public student() { super(); } public student(string name,int age,string[] courses,string interest) { this.name = name; this.age = age; this.courses = courses; this.interest = interest; } public void setname(string name){ this.name = name; } public string getname(){ return name; } public void setage(int age){ if(age<0){ system.out.println("年龄不能为负值"); }else{ this.age = age; } } public int getage(){ return age; } public void setcourses(string[] courses){ this.courses = courses; } public string getcourses(){ return arrays.tostring(courses); } public void setinterest(string interest){ this.interest = interest; } public string getinterest(){ return interest; } public string tostring() { return "姓名=" + getname() + ", 年龄=" + getage() + ", 课程=" + getcourses() + ", 兴趣=" + getinterest(); } }
测试类
package com.hz.test; public class test { public static void main(string[] args) { string arr[] = {"阿斯达","是的","大概","太诱惑"}; student stu = new student("张三",21,arr,"打球"); teacher tea = new teacher("王五","阿斯达","阿斯达",99); system.out.println(stu); system.out.println(tea); } }
输出结果:
姓名=张三, 年龄=21, 课程=[阿斯达, 是的, 大概, 太诱惑], 兴趣=打球
姓名=王五, 专业方向=阿斯达, 所教课程=阿斯达, 教龄=99
继承
定义play,taoistpriest,master,warrior
public class play { string main; public play(string main) { this.main = main; } public void hitmonster() { system.out.println(main+"打怪"); } }
/** * taoistpriest:道士 * @author ztw * */ public class taoistpriest extends play { { system.out.print("我是道士:"); } public taoistpriest(string main) { super(main); } }
/** * master:法师 * @author ztw * */ public class master extends play{ { system.out.print("我是法师:"); } public master(string main) { super(main); } }
/** * warrior:武士 * @author ztw * */ public class warrior extends play{ { system.out.print("我是武士:"); } public warrior(string main) { super(main); } }
测试类
public class test { public static void main(string[] args) { taoistpriest tp = new taoistpriest("灵魂火符"); tp.hitmonster(); master m = new master("雷电术"); m.hitmonster(); warrior w = new warrior("烈火术"); w.hitmonster(); } }
输出结果:
我是道士:灵魂火符打怪 我是法师:雷电术打怪 我是武士:烈火术打怪
多态
服务器,客户端交互
loginlistener
public interface loginlistener { public void succeed(string msg); public void failed(string msg); }
myloginlistener
public class myloginlistener implements loginlistener{ public void succeed(string msg) { system.out.println(msg); } public void failed(string msg) { system.out.println(msg); } }
server
public class server { public void login(string username,string password,loginlistener listener) { system.out.print("loading"); for (int i = 0; i < 10; i++) { try { thread.sleep(100*i); system.out.print("."); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } if(username.equals("zhangsan") && password.equals("123")){ if(listener!=null){ listener.succeed("登录成功"); } }else{ if(listener!=null){ listener.succeed("登录失败"); } } } }
测试类
public class logintest { public static void main(string[] args) { scanner sc = new scanner(system.in); system.out.println("请输入用户名:"); string username = sc.next(); system.out.println("请输入用户密码:"); string password = sc.next(); server server = new server(); server.login(username, password, new myloginlistener()); } }
输出结果
请输入用户名: zhangsan 请输入用户密码: 123 loading……….登录成功
总结
以上就是本文关于java面向对象编程(封装,继承,多态)实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:解析java编程之synchronized锁住的对象、java语言面向对象编程思想之类与对象实例详解等,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
上一篇: ASP.NET中验证控件的使用方法
推荐阅读
-
Java面向对象编程(封装/继承/多态)实例解析
-
面向对象思想自我总结 博客分类: 面向对象封装多态java总结抽象继承 Java面向对象总结抽象 继承 封装 多态
-
面向对象思想自我总结 博客分类: 面向对象封装多态java总结抽象继承 Java面向对象总结抽象 继承 封装 多态
-
PHP面向对象编程之深入理解方法重载与方法覆盖(多态)_php实例
-
Java面向对象编程之类的继承详解
-
python3.x面向对象编程之继承(代码实例)
-
Java面向对象三大特性及多态解析
-
Java面向对象(二)——三大特征封装、继承、多态
-
Python学习笔记(二):面向对象编程小实例士兵突击封装案例
-
Java中类、对象、封装、继承和多态(详细阐述)