Java类的特点——多态、抽象

  所谓多态,顾名思义,就是多种形式多种状态。多态可以说是面向对象编程的精髓所在。因此理解多态的含义对理解面向对象编程有特别重要的意义。Java之所以引入多态的概念,原因之一是它在类的继承问题上与C++不同,C++允许多继承,这确实给它带来了非常强大的功能,但是多继承必然会引起程序错综复杂,难于阅读理解。为了规避这种风险,java采取了单继承的方法,虽然这样使继承关系简单明了,但是同时也限制了其功能的实现。为了弥补这点不足,java引入了多态性的概念。

  多态有两种理解方式,第一种是对像的不同方法可以用相同的一个方法名,即重载。另一种是同一对象根据不同的消息执行相应的行为,也就是,发送一个消息给某一个对象,让对象自行选择哪种行为。由此可以将多态分为静态多态和动态多态

  静态多态指的是程序在编译时,系统就能决定调用哪个方法,所以也成为编译时多态。在java中实现静态多态的实现方式是方法重载。

  动态多态指在运行中系统才能动态确定方法所指的对象,所以也称为运行时多态。动态多态的实现方式是重写父类中的同名成员方法。动态多态主要是通过动态绑定和重写的机制来实现的。

//定义一个shape类
public class Shape {
  public void draw(){
    System.out.println("画一个图形");
  }
}

//定义一个多边形子类,继承Shape类
public class Rectangle extends Shape{
  private int width;
  private int height;
  //with访问修改方法
  public int getWidth() {
    return width;
  }
  public void setWidth(int width) {
    if(width>=1&&width<=15){
    this.width = width;
    }
  }
  //height访问修改方法
  public int getHeight() {
    return height;
  }
  public void setHeight(int height) {
    if(height>=1&&height<=15){
    this.height = height;
    }
  }
  //重写父类draw方法
  public void draw(){
    System.out.println("******* * * *******");
  }
}

//定义一个多边形子类,继承Shape类
public class Rectangle extends Shape{
  private int width;
  private int height;
  //with访问修改方法
  public int getWidth() {
    return width;
  }
  public void setWidth(int width) {
    if(width>=1&&width<=15){
      this.width = width;
    }
  }
  //height访问修改方法
  public int getHeight() {
    return height;
  }
  public void setHeight(int height) {
    if(height>=1&&height<=15){
    this.height = height;
    }
  }
  //重写父类draw方法
  public void draw(){
    System.out.println("******* * * *******");
  }
}

//定义一个Aritst类,将Shape类型的变量传给此类
public class Artist{
  public void drawShape(Shape shape){
    shape.draw();
  }
}

//写一个main方法
public class ArtistDemo {
  public static void main(String[] args){
    //建个Shape类对象
    Shape shape=new Shape();
    //建个Rectangle类对象
    Rectangle rectangle=new Rectangle();
    //建个RightTriangle对象
    RightTriangle rt=new RightTriangle();
    //建个Artist对象
    Artist artist=new Artist();

    artist.drawShape(shape);
    artist.drawShape(rectangle);
    artist.drawShape(rt);

  }
}

上面程序输出的结果是:

画一个图形
*******
* *
*******
*
***
*****
*******

由此可见动态多态在运行中系统才能动态确定方法所指的对象,传递不同的对象,只要在子类中有重写该方法就会执行子类的方法,没有重写时才会调用父类的方法。

  此外java类还有抽象这个特点,抽象用关键字abstract修饰。经过abstract修饰过的类称为抽象类,抽象类只可以用来继承,不可以用来产生对象。进过abstract修饰的方法称为抽象方法,一个类一旦有了抽象方法,那么这个类也要变成抽象类。在继承了一个有抽象方法的抽象类时,这个子类必须重写重新类中的抽象方法。这样就要求合理设计类与类之间的继承关系并且规范了子类。

//定义一个静态类
public abstract class Mammal {
  public void breathe(){
    System.out.println("Mammal is breathing");
  }
  //定义一个静态方法
  public abstract void nurse();
}

//定义一个鲸类,继承Mammal类
public class Whale extends Mammal{
  public void breathe(){
    System.out.println("Whale is breathing");
    super.breathe();
  }
  //重写nurse静态方法
  public void nurse(){
    System.out.println("Whale is nursing");
  }
}

//定义一个Cat类
public class Cat extends Mammal{
  public void breathe(){
    System.out.println("Cat");
  }
  //这里没有重写nurse静态方法,编译就要报错
  public void nurse(int t){
    System.out.println("cat nurse");
  }
}

public class TestMain {
  //定义一个main方法
  public static void main(String[] args){
    Mammal w=new Whale();
    w.breathe();
    w.nurse();
    Mammal m=w;
    m.breathe();
    m.nurse();
    System.out.println(m instanceof Whale);
    System.out.println(w instanceof Mammal);
    System.out.println(m instanceof Cat);
  }
}

原文地址:https://www.cnblogs.com/leafde/p/3671764.html