多态性
instanceof关键字
抽象类的应用
接口类的应用
接口的应用
1、多态性的体现:
方法的重载和重写
对象的多态性
2、对象多态性
向上转型:程序会自动完成
父类 父类对象 = 子类实例
向下转型:强制类型转换
子类 子类对象 = (子类)父类实例
对象多态性
package Polymorphism01.PolDemo01;
class A {
public void tell1() {
System.out.println("A----telll1");
}
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();
运行结果
B----tell1
A----tell2
*/
//向下转型
A a = new B();
B b = (B) a;
b.tell1();
b.tell2();
b.tell3();
/*
运行结果:
B----tell1
A----tell2
B---tell3
*/
}
}
java面向对象多态性的应用
package Polymorphism01;
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 void tell4() {
System.out.println("D1-----------------------tell4");
}
}
public class PolDemo02 {
public static void main(String[] args) {
say(new B1());
say(new C1());
say(new D1());
}
//用到了java中的多态性 这种设计比较合理 慢慢领悟 减少多次创建
public static void say(A1 a) {
a.tell1();
}
}

instanceof关键字应用
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 instanceDemo {
public static void main(String[] args){
A a = new A();
System.out.println(a instanceof A);//true a是A类的对象
System.out.println(a instanceof B);//false 不是B类的对象
A a1 = new B(); //B是A类的子类
System.out.println(a1 instanceof A); // 向上转型 true
System.out.println(a1 instanceof B); // 是B的对象 true
}
}

抽象类应用
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 s = new Student(10,"小明",100);
s.want();
Worker w = new Worker(35,"大明",10000);
w.want();
}
}

接口的使用nterface USB {
void start();
void stop();
}
class C {
public static void work(USB u) {
u.start();
System.out.println("工作中");
u.stop();
}
}
class USBDisk implements USB {
@Override
public void start() {
System.out.println("U盘开始工作");
}
@Override
public void stop() {
System.out.println("U盘停止工作");
}
}
class Printer implements USB {
@Override
public void start() {
System.out.println("打印机开始工作");
}
@Override
public void stop() {
System.out.println("打印机停止工作");
}
}
public class lxImpl {
public static void main(String[] args) {
C.work(new USBDisk());
C.work(new Printer());
}
}

548

被折叠的 条评论
为什么被折叠?



