概要:
- 多态性
- instanceof关键字
- 抽象类的应用
- 接口的应用
多态性
- 多态性的体现:
- 方法的重载和重写
- 对象的多态性
- 对象的多态性
- 向上转型:程序会自动完成
父类 父类对象 = 子类实例
- 向下转型:强制类型转换
子类 子类对象 = (子类)父类实例
- 向上转型:程序会自动完成
package com.jikexueyuan.pol;
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 PolDemo01 {
public static void main(String[] args) {
//向上转型
B b = new B();
A a = b;
a.tell1();//tell1重写的
a.tell2();
//向下转型,需要先完成向上转型。
A aa = new B();
B bb = (B)aa;
bb.tell1();
bb.tell2();
bb.tell3();
}
}
对象多态性的使用
package com.jikexueyuan.pol;
//为了实现调用每一个子类都会执行父类的tell1方法。
class A1{
public void tell1() {
System.out.println("A--tell1");
}
}
class B1 extends A1{
public void tell2() {
System.out.println("B--tell2");
}
}
class C1 extends A1{
public void tell3() {
System.out.println("C--tell3");
}
}
class D1 extends A1{
}
public class PolDemo2 {
public static void main(String[] args) {
say(new B1());
say(new C1());
say(new D1());
}
//这个方法是不管传递的是B1还是C1都要调用到A1中的tell方法的执行。
public static void say(A1 a) {
a.tell1();
}
}
instanceof关键字
在Java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例。返回的是一个布尔类型。
a instanceof A
抽象类的应用
无论什么时候不要去继承一个已经完成好的类
package com.jikexueyuan.pol;
//无论什么时候不要去继承一个已经完成好的类
abstract class Person{
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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(int age,String name,int score) {
super(age, name);
this.score = score;
}
@Override
public void want() {
System.out.println("姓名:"+getName()+"年龄"+getAge()+"成绩"+getScore());
}
}
class Worker extends Person{
private int money;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Worker(int age,String name,int money) {
super(age,name);
this.money = money;
}
@Override
public void want() {
System.out.println("姓名:"+getName()+"年龄"+getAge()+"工资"+getMoney());
}
}
public class AbsDemo01 {
public static void main(String[] args) {
Student student = new Student(10, "xiaoming", 100);
student.want();
Worker worker = new Worker(30, "daming", 1000);
worker.want();
}
}
接口的应用
usb是一个标准,U盘和打印机的执行都得按照USB的标准进行工作。
package com.jikexueyuan.pol;
//usb是一个标准,U盘和打印机的执行都得按照USB的标准进行工作。
interface USB{
void start();
void stop();
}
class Comquter{
public static void work(USB usb) {
usb.start();
System.out.println("工作中");
usb.stop();
}
}
class USBDisk implements USB{
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("u盘开始工作");
}
@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("u盘停止工作");
}
}
class Printer implements USB{
@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("打印机开始工作");
}
@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("打印机停止工作");
}
}
public class InterDemo01 {
public static void main(String[] args) {
Comquter.work(new USBDisk());
Comquter.work(new Printer());
}
}