溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析

發(fā)布時(shí)間:2020-10-04 07:59:38 來(lái)源:腳本之家 閱讀:130 作者:書思BookReflect 欄目:編程語(yǔ)言

本文主要介紹了面向?qū)ο蟮娜筇卣鲗?shí)例解析,下面看看具體內(nèi)容。

封裝

封裝一個(gè)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() + ", 專業(yè)方向=" + 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("年齡不能為負(fù)值");
    }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();
  }  
}

測(cè)試類

package com.hz.test;
public class Test {
  public static void main(String[] args) {
    String arr[] = {"阿斯達(dá)","是的","大概","太誘惑"};
    Student stu = new Student("張三",21,arr,"打球");
    Teacher tea = new Teacher("王五","阿斯達(dá)","阿斯達(dá)",99);
    System.out.println(stu);
    System.out.println(tea);
  }
}

輸出結(jié)果:

姓名=張三, 年齡=21, 課程=[阿斯達(dá), 是的, 大概, 太誘惑], 興趣=打球
姓名=王五, 專業(yè)方向=阿斯達(dá), 所教課程=阿斯達(dá), 教齡=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);
  }
}

測(cè)試類

public class Test {
  public static void main(String[] args) {
    TaoistPriest tp = new TaoistPriest("靈魂火符");
    tp.hitMonster();
    Master m = new Master("雷電術(shù)");
    m.hitMonster();
    Warrior w = new Warrior("烈火術(shù)");
    w.hitMonster();
  }
}

輸出結(jié)果:

我是道士:靈魂火符打怪 
我是法師:雷電術(shù)打怪 
我是武士:烈火術(shù)打怪

多態(tài)

服務(wù)器,客戶端交互

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("登錄失敗");
      }
    }
  }
}

測(cè)試類

public class LoginTest {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("請(qǐng)輸入用戶名:");
    String userName = sc.next();
    System.out.println("請(qǐng)輸入用戶密碼:");
    String password = sc.next();
    Server server = new Server();
    server.login(userName, password, new MyLoginListener());
  }
}

輸出結(jié)果

請(qǐng)輸入用戶名: 
zhangsan 
請(qǐng)輸入用戶密碼: 
123 
loading……….登錄成功

總結(jié)

以上就是本文關(guān)于Java面向?qū)ο缶幊蹋ǚ庋b,繼承,多態(tài))實(shí)例解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:解析Java編程之Synchronized鎖住的對(duì)象、Java語(yǔ)言面向?qū)ο缶幊趟枷胫惻c對(duì)象實(shí)例詳解等,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI