MENU

继承案例分析

• November 14, 2020 • 面向对象编程

继承案例分析

案例一:学生类

建立一个人类( Person)和学生类( Student),功能要求如下:
(1) Person中包含4个私有型的数据成员name、adr、sex、age,分别为字符串型、字符串型、字符型及整型,表示姓名、地址、性别和年龄。一个4参构造方法个两参构造方法、一个无参构造方法、一个输出方法显示4种属性
(2) Student类继承 Person类,并增加成员math、eng1ish存放数学和英语成绩。一个6参构造方法、一个两参构造方法、一个无参构造方法和重写输出方法用于显示6种属性

/** 人类 */
class Person {
  /**四个私有属性*/
  private String name;
  private String address;
  private char sex;
  private int age;
    
  /**四参构造方法*/
  public Person(String name, String address, char sex, int age) {
    this.name = name;
    this.address = address;
    this.sex = sex;
    this.age = age;
  }
  /**无参构造方法*/
  public Person(){
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getAddress() {
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
  public char getSex() {
    return sex;
  }
  public void setSex(char sex) {
    this.sex = sex;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  /**输出   显示四种属性*/
  public void display(){
    System.out.println("姓名:"+this.getName()+"   地址:"+this.getAddress()+"   性别:"+this.getSex()+"   年龄:"+this.getAge());
  }
}

/** 学生类 */
class Student extends Person {
  private int math;
  private int english;
    
  /**六参构造方法*/
  public Student(String name, String address, char sex, int age, int math, int english) {
    super(name, address, sex, age);
    this.math = math;
    this.english = english;
  }
  /**两参构造方法*/
  public Student(int math, int english) {
    this.math = math;
    this.english = english;
  }
  /**无参构造方法*/
  public Student(){
  }
  /**重写方法  输出六个信息*/
  @Override
  public void display(){
    System.out.println("姓名:"+this.getName()+"   地址:"+this.getAddress()+"   性别:"+this.getSex()+"   年龄:"+this.getAge()+"   数学成绩:"+this.getMath()+"   英语成绩:"+this.getEnglish());
  }
  public int getMath() {
    return math;
  }
  public void setMath(int math) {
    this.math = math;
  }
  public int getEnglish() {
    return english;
  }
  public void setEnglish(int english) {
    this.english = english;
  }
}

public class Main {
  public static void main(String[] args) {
    Student student = new Student("小李","北京市XXXXX",'男',15,99,100);
    student.display();
  }
}

运行结果:

建立在简单JAVA类的基础上来实现这个案例

案例二:管理人员与职员

定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,并有自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性所属部门和月薪

/**员工类*/
class Emp{
    /**三个基本属性*/
    private String name;
    private int age;
    private char sex;
    
    /**空构造器*/
    public Emp(){
    }
    /**三参构造器*/
    public Emp(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public String getInfo(){
        return "姓名:"+this.getName()+"   年龄:"+this.getAge()+"   性别:"+this.getSex();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
}

/**管理层*/
class Manager extends Emp{
    /**职务和年薪*/
    private String job;
    private double yearSala;
    /**两参构造*/
    public Manager(String job, double yearSala) {
        this.job = job;
        this.yearSala = yearSala;
    }
    /**五参构造*/
    public Manager(String name, int age, char sex, String job, double yearSala) {
        super(name, age, sex);
        this.job = job;
        this.yearSala = yearSala;
    }
    /**无参构造*/
    public Manager(){
    }
    /**重写getInfo()*/
    @Override
    public String getInfo(){
        return "姓名:"+this.getName()+"   年龄:"+this.getAge()+"   性别:"+this.getSex()+"   职务:"+this.getJob()+"   年薪:"+this.getYearSala();
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public double getYearSala() {
        return yearSala;
    }
    public void setYearSala(double yearSala) {
        this.yearSala = yearSala;
    }
}

/**职员类*/
class Staff extends Emp{
    /**所属部门和月薪*/
    private String dopt;
    private double monthSala;
    /**两参构造*/
    public Staff(String dopt, double monthSala) {
        this.dopt = dopt;
        this.monthSala = monthSala;
    }
    /**五参构造*/
    public Staff(String name, int age, char sex, String dopt, double monthSala) {
        super(name, age, sex);
        this.dopt = dopt;
        this.monthSala = monthSala;
    }
    /**无参构造*/
    public Staff(){
    }
    /**重写getInfo()*/
    @Override
    public String getInfo(){
        return "姓名:"+this.getName()+"   年龄:"+this.getAge()+"   性别:"+this.getSex()+"   部门:"+this.getDopt()+"   月薪:"+this.getMonthSala();
    }
    public String getDopt() {
        return dopt;
    }
    public void setDopt(String dopt) {
        this.dopt = dopt;
    }
    public double getMonthSala() {
        return monthSala;
    }
    public void setMonthSala(double monthSala) {
        this.monthSala = monthSala;
    }
}

案例三:字符串统计

编写程序,统计出字符串" want you to know one thing"中字母n和字母o的出现次数。【利用继承思想】

/**字符串类*/
class StringUtil {
  /**字符串变量*/
  private String string;
  
  public String getString() {
    return string;
  }
  public void setString(String string) {
    this.string = string;
  }
  /**空构造器*/
  public StringUtil(){
  }
  /**带参构造器*/
  public StringUtil(String string) {
    this.string = string;
  }
}

/**统计字符类*/
class CharCount extends StringUtil {
  /**n,o字符数变量*/
  private int oCount;
  private int nCount;
  
  /**构造器*/
  public CharCount(String string) {
    super(string);
  }
  /**获取o数量*/
  public int getoCount() {
    int len = this.getString().length();
    for (int i = 0; i < len; i++) {
      if (this.getString().charAt(i) == 'o' || this.getString().charAt(i) == 'O') {
        this.oCount++;
      }
    }
    return oCount;
  }
  public void setoCount(int oCount) {
    this.oCount = oCount;
  }
  /**获取n数量*/
  public int getnCount() {
    int len = this.getString().length();
    for (int i = 0; i < len; i++) {
      if (this.getString().charAt(i) == 'n' || this.getString().charAt(i) == 'N') {
        this.nCount++;
      }
    }
    return nCount;
  }
  public void setnCount(int nCount) {
    this.nCount = nCount;
  }
}

/**测试类*/
public class Main {
  public static void main(String[] args) {
    CharCount charCount = new CharCount("want you to know one thing");
    System.out.println("o的个数:"+charCount.getoCount());
    System.out.println("n的个数:"+charCount.getnCount());
  }
}

运行结果:

案例四:数组操作

建立一个可以实现整型数组的操作类( Array),而后在里面可以操作的数组的大小由外部来决定,而后在Aray类里面需要提供有数组的如下处理:进行数据的增加(如果数据满了则无法增加)、可以实现数组的容量扩充、取得数组全部数据
完成之后在此基础上再派生出两个子类:

  • 数组排序类:返回的数据必须是排序后的结果
  • 数组反转类:可以实现内容的首尾交换。
import java.util.Arrays;
class Array {
  private int arr[];
  private int foot;
  
  /** 构造器 */
  public Array(int n) {
    this.arr = new int[n];
  }
  /** 空构造器 */
  public Array() {}
  /** 追加数字 */
  public boolean append(int num) {
    if (foot >= this.arr.length) {
      return false;
    } else {
      this.arr[foot++] = num;
      return true;
    }
  }
  /** 扩充数组 */
  public void expand(int len) {
    int[] a = new int[this.arr.length + len];
    for (int i = 0; i < this.arr.length; i++) {
      a[i] = this.arr[i];
    }
    this.arr = a;
  }
  /**
   * 获取数组全部数据
   *
   * @return
   */
  public int[] getData() {
    return this.arr;
  }
}
/** 数组排序类 */
class SortArray extends Array {
  /** 构造器 */
  public SortArray(int n) {
    super(n);
  }
  /** 重写getData方法 */
  @Override
  public int[] getData() {
    // 调用数组排序
    Arrays.sort(this.getData());
    return this.getData();
  }
}

/** 数组反转子类 */
class ReverseArray extends Array {
  public ReverseArray(int n) {
    super(n);
  }
  /**重写获取全部数据方法,获得已经反转之后的数组*/
  @Override
  public int[] getData() {
    int len = super.getData().length;
    for (int i = 0; i < len / 2; i++) {
      int temp = this.getData()[i];
      this.getData()[i] = this.getData()[len - 1 - i];
      this.getData()[len - 1 - i] = temp;
    }
    return this.getData();
  }
}

代码编译器:IntelliJ IDEA 2020.02

Archives QR Code Tip
QR Code for this page
Tipping QR Code