Java抽象类的完整介绍
时间:2020-02-23 14:33:53 来源:igfitidea点击:
Java中的抽象类可帮助用户实现抽象,这是在软件设计过程中遵循的最关键的面向对象编程实践。
什么是Java中的抽象类?
Java中的抽象类充当实现方法及其功能之间的边界。
它用于在Concrete类成员和Abstract类之间交换功能。
抽象类被认为是那些向用户隐藏"方法实现"详细信息并仅显示"方法功能"的类。
它们使用关键字abstract声明。
这些方法中可以包括摘要方法和非摘要方法。
为什么我们需要Java中的抽象类?
由于以下原因,我们需要Java中的抽象类:
抽象类在运行时支持动态方法解析
它们帮助用户实现松散耦合
抽象类将方法定义与继承的子类分开
它们为所有子类提供了已定义方法的默认功能。
抽象类为将来的特定类提供模板
抽象类允许代码重用
在Java中使用抽象类的规则
要使用Java实现抽象类,我们需要遵循以下规则:
必须使用abstract关键字声明一个抽象类。
抽象类可以包括抽象和非抽象方法。
不能实例化Abstract类。
它们可以包括构造函数和静态方法。
抽象类包括最终方法。
用Java实现抽象的方法
Java中的抽象过程可以通过以下两种方法来实现,如下所述:
实现抽象类
实施接口
抽象类的语法
定义抽象类和抽象方法的语法如下:
abstract class theitroad{}
abstract class Method();
抽象类的实际例子
//抽象类
package Abstract;
public abstract class Person {
private String name;
private String gender;
public Person(String nm, String gen) {
this.name = nm;
this.gender = gen;
}
public abstract void Studying();
@Override
public String toString() {
return "Name=" + this.name + "::Gender=" + this.gender;
}
}
//学生班
package Abstract;
public class Student extends Person {
private int StudentID;
public Student(String nm, String gen, int id) {
super(nm, gen);
this.StudentID = id;
}
@Override
public void Studying() {
if (StudentID == 0) {
System.out.println("Not Studying");
}
else {
System.out.println("Pursuing a Degree in Bachelor of Engineering");
}
}
public static void main(String args[]) {
Person student = new Student("Priya", "Female", 0);
Person student1 = new Student("Karan", "Male", 201021);
Person student2 = new Student("Kumari", "Female", 101021);
Person student3 = new Student("John", "Male", 201561);
student.Studying();
student1.Studying();
student2.Studying();
student3.Studying();
System.out.println(student.toString());
System.out.println(student1.toString());
System.out.println(student2.toString());
System.out.println(student3.toString());
}
}
Not Studying Pursuing a Degree in Bachelor of Engineering Pursuing a Degree in Bachelor of Engineering Pursuing a Degree in Bachelor of Engineering Name=Priya::Gender=Female Name=Karan::Gender=Male Name=Kumari::Gender=Female Name=John::Gender=Male
接口和抽象类之间的区别
| 接口 | 抽象类 |
| 只能有抽象方法 | 可以有抽象方法和非抽象方法 |
| 它只有最终变量 | 它包括非最终变量 |
| 它仅具有静态和最终变量。它具有静态,非静态,最终,非最终变量 | |
| 将不会实现Abstract类 | 可以实现接口 |
| 使用“实施”关键字实施使用“扩展”关键字实现 | |
| 只能扩展一个接口可以扩展Java类和接口 | |
| 成员默认为公开 | 成员可以是私有的,也可以是受保护的 |
//抽象类示例
package abstactVSinterface;
abstract class Shape {
String objectName = " ";
Shape(String name) {
this.objectName = name;
}
abstract public double area();
abstract public void draw();
}
class Rectangle extends Shape {
int length, width;
Rectangle(int length, int width, String name) {
super(name);
this.length = length;
this.width = width;
}
@Override
public void draw() {
System.out.println("Rectangle is drawn ");
}
@Override
public double area() {
return (double) (length * width);
}
}
class Circle extends Shape {
double pi = 3.14;
int radius;
Circle(int radius, String name) {
super(name);
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle is drawn ");
}
@Override
public double area() {
return (double) ((pi * radius * radius)/2);
}
}
class theitroad {
public static void main(String[] args) {
Shape rect = new Rectangle(20, 30, "Rectangle");
System.out.println("Area of rectangle: " + rect.area());
Shape circle = new Circle(20, "Cicle");
System.out.println("Area of circle is: " + circle.area());
}
}
Area of rectangle: 600.0 Area of circle is: 628.0
//接口示例
package absVSint;
interface Shape {
void draw();
double area();
}
class Rectangle implements Shape {
int length, width;
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
@Override
public void draw() {
System.out.println("Rectangle has been drawn ");
}
@Override
public double area() {
return (double) (length * width);
}
}
class Circle implements Shape {
double pi = 3.14;
int radius;
Circle(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle has been drawn ");
}
@Override
public double area() {
return (double) ((pi * radius * radius)/2);
}
}
class theitroad {
public static void main(String[] args) {
Shape rect = new Rectangle(20, 30);
System.out.println("Area of rectangle: " + rect.area());
Shape circle = new Circle(20);
System.out.println("Area of circle: " + circle.area());
}
}
Area of rectangle: 600.0 Area of circle: 628.0

