/*Object-oriented has the characteristics of abstraction, encapsulation, inheritance and polymorphism. Abstraction is the abstraction of objects with consistent data structures (attributes) and behaviors (operations) into classes
A class is an abstraction. In addition to the basic types of data, encapsulation data in Java exists in the form of objects, which are the encapsulation of methods and data.
Inheritance is a mechanism for subclasses to automatically share data structures and methods of their parents, which is a relationship between classes. When defining and implementing a class,
It can be carried out on the basis of an existing class, taking the content defined by the existing class as its own content, and adding some new content.
Polymorphism refers to that the same operation or function or process can act on multiple types of objects and obtain different results. Different objects, receiving the same message can produce different results.
Polymorphism allows each object to respond to common messages in its own way, which enhances the flexibility and reusability of software. This chapter will introduce the features and applications of object-oriented.
*/
package JAVA_Project_01_05;//Create a package //Abstract class is a special class in Java. Abstract classes cannot create objects, but can only be created by their descendents. Abstract classes are used exclusively as the parents of other classes. // This example shows how to use abstract classes to get the area of different graphs. // Use the keyword Abstract modifier to define an abstract class. Abstract before the keyword class, the syntax format is: (public)abstractclass class name {}. // The abstract method is defined by abstract, which is placed before the return type of the method. Syntax format: Abstract return type method name (). //·The abstract class must be the parent of other classes, and the child class must implement all the abstract methods in the parent class, otherwise it must also be declared as an abstract class. abstract class Geometric {//Create abstract class String color = "block"; int weight = 2; abstract float getArea();//Abstract construction method for area abstract float getPerimeter();//Abstract construction method for perimeter } class Circle extends Geometric {//Inherit Geometric, find the area and perimeter of the circle float radius; Circle(float number) {//Construction method with parameters radius = number; } protected float getArea() {//Realizing the abstract method of parent class to find the area of circle return 3.14f * radius * radius; } protected float getPerimeter() {//Realizing the abstract method of parent class to find the circumference of circle return 2 * 3.14f * radius; } } class Rectangle extends Geometric {//Inherit Geometric to find area and perimeter of rectangle float width; float height; Rectangle(float width, float height) {//Construction method with parameters this.width = width; this.height = height; } float getArea() {//Realizing the abstract method of parent class to find the area of rectangle return width * height; } float getPerimeter() {//Realizing the abstract method of parent class to find the circumference of rectangle return 2 * (width * height); } } public class TextAbstract {//Operation abstract class to find the class of graphic area public static void main(String[] args) {//Java program main entrance System.out.println("1.Obtain the area and perimeter of a circle"); Circle circle = new Circle(4);//Create a circle object instance System.out.printf("Area of circle:%s%n", circle.getArea()); System.out.printf("Circumference of a circle:%s%n", circle.getPerimeter()); System.out.println("2.Get the area and perimeter of the rectangle"); Rectangle rectangle = new Rectangle(3, 4);//Create a rectangular object instance System.out.printf("Area of rectangle:%s%n", rectangle.getArea()); System.out.printf("Circumference of rectangle:%s%n", rectangle.getPerimeter()); } } /* (1)Create a Geometric abstract class with the keyword abstract, and declare two abstract construction methods. The default abstract method has protected access, which is decorated with protected access modifier by default. Simply put, only the inner class and subclass can access the member. (2)Circle And Rectangle class inherit Geometric abstract class. All abstract methods must be implemented. Otherwise, you need to add abstract before keyword class to become abstract class.*/