Abstract classes and methods in Java

Abstract classes and methods in Java

abstract conception

The key word of this article is abstraction, so what is abstraction? Baidu Encyclopedia tells us that abstraction is a thinking process that summarizes the common aspects and essential attributes of specific affairs and abandons individual aspects and attributes. In Java, that is to extract the methods common to each specific class and put them into the base class. The base class does not need to understand how the method is implemented in the subclass. This base class is the so-called abstract class. These methods that do not need to know the specific implementation method are abstract methods.

Abstract classes and methods

Let's combine the code to analyze a wave:

package com.my.pac18;

/**
 * @auther Summerday
 */
/*抽象是很有用的重构工具,能够让共有的方法沿着继承层次向上移动。*/
public abstract class Shape {
    //抽象类中的初始化块
    {
        System.out.println("Shape.instance initializer");
    }
    //抽象类中的实例变量
    private String color;
    //抽象类中可以有静态方法及属性。
    public static String name="形状";
    public static void output(){
        System.out.println("父类的static方法");
    }
    //计算周长,但是并不知道具体细节的抽象方法
    public abstract double calPerimeter();
    //返回形状的抽象方法

    //[修饰符] abstract 返回类型 方法名();
    public abstract String getType();
    //抽象类中的构造器,用于被继承
    public Shape() {
    }

    public Shape(String color) {
        System.out.println("Shape.Shape");
        this.color = color;
    }
    //抽象类中的普通实例方法
    public String getColor() {
        return color;
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(String color,double radius) {
        super(color);
        this.radius = radius;
    }

    public void seTradius(double radius) {
        this.radius = radius;
    }

    @Override
    public double calPerimeter() {
        return 2 * Math.PI * radius;
    }

    @Override
    public String getType() {
        return getColor() + "圆形";
    }

    //如果从一个抽象类继承,并创建该新类的对象,就必须给基类的所有抽象方法提供定义。
    public static void main(String[] args) {
        Shape.output();
        //Shape是抽象类,不能创建Shape类的对象
        //但是可以让Shape类的引用变量指向其子类的对象
        Shape[] shapes = new Shape[]{new Circle("红色",5),new Rectangle("绿色",6,6)};
        for (Shape p : shapes) {
            
            System.out.println(p.getType() + "的周长为" + p.calPerimeter());
        }

    }
}

class Rectangle extends Shape {
    
    private double length;
    private double width;

    public Rectangle() {
        System.out.println("父类的构造器不是为了创建对象,而是供给子类继承的");
    }

    public Rectangle(String color,double length,double width) {
        super(color);
        this.length = length;
        this.width = width;
    }

    public boolean isSquare() {
        return length == width;
    }

    public double calPerimeter() {
        return (length + width) * 2;
    }

    public String getType() {
        if (isSquare()) return getColor() + "正方形";
        return getColor() + "长方形";
    }
}

//测试结果
父类的static方法
Shape.instance initializer
Shape.Shape
Shape.instance initializer
Shape.Shape
红色圆形的周长为31.41592653589793
绿色正方形的周长为24.0

Following the above example, let's briefly analyze what abstract classes and abstract methods are? What do you need to pay attention to?

//[修饰符] abstract class 类名
public abstract class Shape
//[修饰符] abstract 返回类型 方法名();
 public abstract double calPerimeter();
 public abstract String getType();

matters needing attention

@Override
public double calPerimeter() {
    return 2 * Math.PI * radius;
}
@Override
public String getType() {
    return getColor() + "圆形";
}
//抽象类中的实例方法,子类继承使用。
public String getColor() {
        return color;
    }
//抽象类中可以有静态方法和属性,类名.方法(属性)调用。
public static String name="形状";
public static void output(){
    System.out.println("父类的static方法");
}
//子类
public Shape() {
    }

public Shape(String color) {
    System.out.println("Shape.Shape");
    this.color = color;
}
 //抽象类中的初始化块
{
    System.out.println("Shape.instance initializer");
}

This article is based on data and personal tests. If there are errors or improper understanding, please point out in the comment area. Thank you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>