您当前的位置: 首页 >  Java

ITKEY_

暂无认证

  • 0浏览

    0关注

    732博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java温故而知新-多态性

ITKEY_ 发布时间:2021-01-07 17:55:03 ,浏览量:0

在面向对象的程序开发过程之中,有三大主要特性:封装性,继承性,多态性 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

Object 类

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

对象的比较

在这里插入图片描述

有缺陷的比较(一般人的想法)

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

示例代码

示例1:

class Person {
	private String name ;
	private String addr ;
	private char sex ;
	private int age ;
	public Person() {}
	public Person(String name, String addr) {
		this(name, addr, '男', 18) ; // 调用四参构造
	}
	public Person(String name, String addr, char sex, int age) {
		this.name = name ;
		this.addr = addr ;
		this.sex = sex ;
		this.age = age ;
	}
	public String toString() {	// 覆写Object类中的方法
		return "姓名:" + this.name + "、地址:" + this.addr + "、性别:" + this.sex + "、年龄:" + this.age ;
	}
}
class Student extends Person {	// 继承可以实现类的代码重用
	private double math ;
	private double english ;
	public Student() {}
	public Student(String name, String addr) {
		super(name, addr) ; // 调用父类中的双参构造
	}
	public Student(String name, String addr, char sex, int age, double math, double english) {
		super(name, addr, sex, age) ;	// super调用父类构造
		this.math = math ;
		this.english = english ;
	}
	public String toString() {
		return super.toString() + ",数学成绩:" + this.math + "、英语成绩:" + this.english ;
	}
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
	public static void main(String args[]) {
		Person person = new Student("小李", "北京-天安门", '男', 18, 99.8, 99.9) ;// 向上转型
		System.out.println(person) ;
	}
}

示例2:

class Employee { // 员工
	private String name ;
	private String sex ;
	private int age ;
	public Employee() {}
	public Employee(String name, String sex, int age) {
		this.name = name ;
		this.sex = sex ;
		this.age = age ;
	}
	public String toString() {
		return "姓名:" + this.name + "、性别:" + this.sex + "、年龄:" + this.age ;
	}
}
class Manager extends Employee { // 管理层
	private String job ;
	private double income ;
	public Manager() {}
	public Manager(String name, String sex, int age, String job, double income) {
		super(name, sex, age) ;
		this.job = job ;
		this.income = income ;
	}
	public String toString() {
		return super.toString() + "、职位:" + this.job + "、年薪:" + this.income ;
	}
}
class Staff extends Employee {
	private String dept ;
	private double salary ;
	public Staff() {}
	public Staff(String name, String sex, int age, String dept, double salary) {
		super(name, sex, age) ;
		this.dept = dept ;
		this.salary = salary ;
	}
	public String toString() {
		return super.toString() + "、部门:" + this.dept + "、月薪:" + this.salary ;
	}
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
	public static void main(String args[]) {
		get(new Manager("小李", "男", 20, "部门经理", 60000.0)) ;
		get(new Staff("小张", "女", 19, "后勤部", 90000.0)) ;
	}
	public static void get(Employee emp) {
		System.out.println(emp) ;
	}
}

示例3:面向过程统计字母数量

public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
	public static void main(String args[]) {
		String str = "want you to know one thing" ; // 给定一个字符串信息
		String result [] = str.split("") ; // 按照每一位进行拆分
		int oCount = 0 ; // 保存字母o的个数
		int nCount = 0 ;  // 保存字母n的个数
		for (String temp : result) {
			if ("o".equalsIgnoreCase(temp)) {
				oCount ++ ;
			}
			if ("n".equalsIgnoreCase(temp)) {
				nCount ++ ;
			}
		}
		System.out.println("字母O的个数:" + oCount + "、字母N的个数:" + nCount) ;
	}
}

示例4:面向对象的方式统计字母数量

class StringUtil { // 定义一个字符串的工具类
	private String content ; // 字符串的内容
	public StringUtil(String content) {
		this.content = content ;
	}
	public void setContent(String content) {
		this.content = content ;
	}
	public String getContent() {
		return this.content ;
	}
}
class CharCount extends StringUtil { // 实现数据统计
	private int oCount ;	// 统计字母o的个数
	private int nCount ;	// 统计字母n的个数
	public CharCount(String content) {
		super(content) ; // 调用父类中的单参构造
		this.handleContent() ; // 构造方法直接进行字符串的处理
	}
	public void handleContent() {	// 处理传入的数据信息
		String result [] = super.getContent().split("") ; // 按照每一位进行拆分
		for (String temp : result) {
			if ("o".equalsIgnoreCase(temp)) {
				this.oCount ++ ;
			}
			if ("n".equalsIgnoreCase(temp)) {
				this.nCount ++ ;
			}
		}
	}
	public String toString() {
		return "【字符统计结果】字母“O”的个数:" + this.oCount + "、字母“N”的个数:" + this.nCount ;
	}
}
public class YootkDemo { // 李兴华编程训练营:yootk.ke.qq.com
	public static void main(String args[]) {
		String str = "want you to know one thing" ; // 给定一个字符串信息
		StringUtil util = new CharCount(str) ;
		System.out.println(util) ;
	}
}

示例5:数组扩充操作

5、
class Array { // 进行数组的操作
	private int data [] ; // 提供有一个数组类型
	private int index ; // 数据增加索引号
	public Array() {
		this(5) ; // 如果使用的是无参构造,默认设置5个大小
	}
	public Array(int len) {	// 动态决定数组大小
		if (len > 0) {
			this.data = new int[len] ; // 开辟新的数组大小
		} else {
			this.data = new int[1] ; // 保证不出错
		}
	}
	public void increment(int cap) {	// 设置增长容量
		int newData [] = new int [this.data.length + cap] ; // 开辟新的数组
		System.arraycopy(this.data,0, newData,0, this.data.length) ; // 数组拷贝
		this.data = newData ; // 修改指向,产生垃圾
	}
	public boolean add(int data) {	// 实现数据增加
		if (this.index  0) {
			this.data = new int[len] ; // 开辟新的数组大小
		} else {
			this.data = new int[1] ; // 保证不出错
		}
	}
	public void increment(int cap) {	// 设置增长容量
		int newData [] = new int [this.data.length + cap] ; // 开辟新的数组
		System.arraycopy(this.data,0, newData,0, this.data.length) ; // 数组拷贝
		this.data = newData ; // 修改指向,产生垃圾
	}
	public boolean add(int data) {	// 实现数据增加
		if (this.index  0) {
			this.data = new int[len] ; // 开辟新的数组大小
		} else {
			this.data = new int[1] ; // 保证不出错
		}
	}
	public void increment(int cap) {	// 设置增长容量
		int newData [] = new int [this.data.length + cap] ; // 开辟新的数组
		System.arraycopy(this.data,0, newData,0, this.data.length) ; // 数组拷贝
		this.data = newData ; // 修改指向,产生垃圾
	}
	public boolean add(int data) {	// 实现数据增加
		if (this.index             
关注
打赏
1665243900
查看更多评论
0.0466s