大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Java面向对象编程(封装/继承/多态)实例解析

本文主要介绍了面向对象的三大特征实例解析,下面看看具体内容。

德清网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

封装

封装一个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语言面向对象编程思想之类与对象实例详解等,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


本文名称:Java面向对象编程(封装/继承/多态)实例解析
网页网址:http://dzwzjz.com/article/jdiejc.html
在线咨询
服务热线
服务热线:028-86922220
TOP