J7 多态

1.多态性

1.1多态性体现

  • 方法重载与重写
  • 对象多态性

向上转型:程序会自动完成

​ 父类 父类对象 = 子类实例

向下转型:强制类型转换

​ 子类 子类对象 = (子类)父类实例

  • 向上转型示例
package com.hanqi.duotai;

class A{
	public void tell1() {
		System.out.println("A---tell1()");
	}
	public void tell2() {
		System.out.println("A---tell2()");
	}
}
class B extends A{
	public void tell1() {
		System.out.println("B---tell1()");
	}
	public void tell3() {
		System.out.println("B---tell3()");
	}
}
public class test01 {
	
	public static void main(String[] args) {
		//向上转型
		B b = new B();
		A a = b;
		a.tell1();	//此时tell1被重写,故执行重写后的tell1方法
		a.tell2();
	}

}
//
B---tell1()
A---tell2()
  • 向下转型示例(想要向下转型就必须先向上转型)
package com.hanqi.duotai;

class A{
	public void tell1() {
		System.out.println("A---tell1()");
	}
	public void tell2() {
		System.out.println("A---tell2()");
	}
}
class B extends A{
	public void tell1() {
		System.out.println("B---tell1()");
	}
	public void tell3() {
		System.out.println("B---tell3()");
	}
}
public class test01 {
	
	public static void main(String[] args) {
		//向下转型
		A a = new B();	//注意new之后是子类
		B b = (B)a;
		b.tell1();
		b.tell2();
		b.tell3();
	}
}
//
B---tell1()
A---tell2()
B---tell3()

1.2多态性简单应用

  • 假设有一个父类A1,两个子类B1,C1均继承自父类A1.如果我们在main方法中创建方法,将类作为参数,最终调用父类中的某方法,实现结果如下:
package com.hanqi.duotai;

class A1{
	public void tell1() {
		System.out.println("A1---tell1()");
	}
}
class B1 extends A1{
	public void tell2() {
		System.out.println("B1---tell2()");
	}
}
class C1 extends A1{
	public void tell3() {
		System.out.println("C1---tell3()");
	}
}
public class test02 {

	public static void main(String[] args) {
		say(new A1());
		say(new B1());
		say(new C1());
	}
	public static void say(A1 a) {
		a.tell1();
	}
	public static void say(B1 b) {
		b.tell1();
	}
	public static void say(C1 c) {
		c.tell1();
	}
}
//
A1---tell1()
A1---tell1()
A1---tell1()
  • 上述代码乍一看人畜无害,但是假设有100多个子类继承自父类A1,那么其对应say方法也应该写100多次,明显不科学,故需要修改
package com.hanqi.duotai;

class A1{
	public void tell1() {
		System.out.println("A1---tell1()");
	}
}
class B1 extends A1{
	public void tell2() {
		System.out.println("B1---tell2()");
	}
}
class C1 extends A1{
	public void tell3() {
		System.out.println("C1---tell3()");
	}
}
public class test02 {

	public static void main(String[] args) {
		say(new A1());
		say(new B1());
		say(new C1());
	}
	public static void say(A1 a) {		//直接将父类对象作为参数即可
		a.tell1();
	}
}
//
A1---tell1()
A1---tell1()
A1---tell1()

2.instanceof关键字

2.1定义

  • 在java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例
  • 实例
package com.hanqi.duotai;

class A{
	public void tell1() {
		System.out.println("A---tell1()");
	}
	public void tell2() {
		System.out.println("A---tell2()");
	}
}
class B extends A{
	public void tell1() {
		System.out.println("B---tell1()");
	}
	public void tell3() {
		System.out.println("B---tell3()");
	}
}
public class test01 {
	
	public static void main(String[] args) {
		A a = new A();
		System.out.println(a instanceof A);
		System.out.println(a instanceof B);
		
		//向上转型
		A a1 = new B();
		System.out.println(a1 instanceof A);
		System.out.println(a1 instanceof B);
	}
}
//
true
false
true
true

3.抽象类的使用

  • 代码,即抽象类的出现就是为了继承,如果某个类已经完善,那么就没必要去继承它,继承是为了补全父类与子类之间的差异之处
package com.hanqi.duotai;

abstract class Person{
	//人共有属性:姓名,年龄
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	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 abstract void want();
}
class Student extends Person{
	//学生的特有属性:成绩
	private int score;
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public Student(String name, int age, int score) {
		super(name, age);
		this.score = score;
	}
	public void want() {
		System.out.println("姓名:" + getName() + " 年龄:" + getAge() + " 成绩:" + getScore());
	}
	
}
class Worker extends Person{
	private int salary;
	
	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	public Worker(String name, int age, int salary) {
		super(name, age);
		this.salary = salary;
	}

	public void want() {
		System.out.println("姓名:" + getName() + " 年龄:" + getAge() + " 工资:" + getSalary());
	}
	
}
public class test03 {

	public static void main(String[] args) {
		Student s = new Student("hanqi", 15, 145);
		s.want();
		Worker w = new Worker("hq", 21, 13000);
		w.want();
	}

}
//
姓名:hanqi 年龄:15 成绩:145
姓名:hq 年龄:21 工资:13000

4.接口的使用

  • 实例,接口定义了一个统一的标准,所有类均按照该标准工作
package com.hanqi.duotai;

//定义一个USB的接口
interface USB{
	void start();
	void stop();
}
class Computer{
	public static void work(USB u) {
		u.start();
		System.out.println("工作中");
		u.stop();
	}
}
class USBDisk implements USB{
	public void start() {
		System.out.println("U盘开始工作");
	}

	public void stop() {
		System.out.println("U盘停止工作");
	}
}
class Printer implements USB{
	public void start() {
		System.out.println("打印机开始工作");
	}

	public void stop() {
		System.out.println("打印机停止工作");
	}
}
public class test04 {

	public static void main(String[] args) {
		Computer.work(new USBDisk());
		Computer.work(new Printer());
	}
}
//
U盘开始工作
工作中
U盘停止工作
打印机开始工作
工作中
打印机停止工作
posted @ 2021-08-05 23:34  MHDSG  阅读(60)  评论(0)    收藏  举报