首页  >  文章  >  Java  >  Java 中的面向对象编程

Java 中的面向对象编程

WBOY
WBOY原创
2024-08-30 16:20:181081浏览

Java 是 James Gosling 设计的面向对象编程。它是一种基于类并具有并发编程功能的通用编程语言。它也具有多线程功能。它是一种静态、安全、强类型的编程语言。它由 Oracle 公司(当时的 Sun 公司)开发和维护。其文件扩展名是.java或.class。它首次出现于 1995 年。它的目的是开发可Written Once and Run Anywhere 的应用程序。它在客户端-服务器类型的应用程序中最流行。它根据 GNU 通用公共许可证和 Java 社区进程获得许可。 Java 的最新版本是 10,于 2018 年 3 月发布。

Java 面向对象编程讲解

Java 编程语言基于面向对象的编程方法或范式,具有不同类型的概念,例如类、对象、继承、多态性、封装和抽象,可描述如下:

广告 该类别中的热门课程 编程语言 - 专业化 | 54 课程系列 | 4 次模拟测试
  • 类:这是对象的蓝图,定义了真正功能所在的字段或属性和方法。这些属性和方法称为成员,在声明成员时可以根据定义的访问修饰符来访问这些成员。
  • 对象:对象称为类的实例,可以通过调用类的构造函数来声明和实例化。对象将具有状态,状态将包含类的属性将保存的数据。
  • 继承:这是第三步过程。对数据进行检查、清洗、转换、可视化,减少无用信息,转化为重要的信息集,从现有数据中获取有价值的信息。
  • 多态性:多态性被定义为以不同可能的方式执行单个任务的过程。在Java中,多态性可以通过方法重载和方法重写两种方式来实现。方法重载也称为编译时多态性,而方法重写也称为运行时多态性。
  • 封装:这是封装,这意味着将代码隐藏或绑定或包装到Java中定义为类的单个单元或模块中。面向对象编程的封装特性可以通过Java中的类来实现。一个普通的旧 java 对象或 Java Bean 被认为是被封装的,因为类的成员是私有的(访问修饰符),这些成员只能通过使用类中的 getter 和 setters 方法来访问。
  • 抽象:面向对象的功能抽象可以定义为通过仅公开所需的接口或访问方法来调用实现类方法来隐藏功能实现的过程。在Java编程语言中可以通过使用Interface和Abstract Class来实现抽象。

Java 中面向对象编程的优点

  1. 它有助于轻松开发不同类型的应用程序及其维护,而无需额外成本。
  2. 它有助于通过对设计进行小的更改来轻松实现更改,从而使应用程序更能适应客户所需的较大更改。
  3. 代码中的模块化有助于通过轻松修复错误来轻松进行故障排除和维护。
  4. 代码复用为主。
  5. 它为频繁的功能更改提供了更大的灵活性。

 Java 中面向对象编程的应用

Java 中面向对象编程有不同的应用,以下是该概念领域的示例:

1.班级

类可以定义如下:

public class Employee {
private int employeeId;
private String employeeName;
public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}
}

在上面的类中,employeeId、employee name 和 getSalary() 方法是类的成员,而 employeeId 和 employee name 是属性或字段,getSalary() 是完成实际工作的方法。

2.对象

可以如下为上述 Employee 类创建一个对象。

Employee employeeObject = new Employee();

在上面的行中,使用 new 关键字创建了一个对象,而 Employee() 是用于创建该对象的空参数构造函数。员工反对对 Employee 类的引用。

3.多态性

这可以通过方法重写和重载来实现。

public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}

在上面的方法中,可以通过添加到括号中来将另一个参数添加到方法 getSalary() 中,如下所示:

public int getSalary(int basicPay, int da, int hra, int bonus) {
int salary = basicPay + da + hra + bonus;
return salary;
}

4. Encapsulation

This can be achieved as below:

public class Employee {
private int employeeId;
private String employeeName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}

The above class Employee has two fields (private) and four methods (getters and setters) which will be used to access the above two private attributes.

5. Abstraction

This is the process of hiding the implementation functionality.

In the above method getSalary(), the internal function of the addition of all the components of a salary is hidden inside the method, and only this can be accessed by using the method name by passing the values as method arguments. In this way, the total salary will be obtained by passing the individual salary components to the method.

Conclusion

There are different and multiples areas of applications in the field of the Web world, Standalone, and many other areas for the Object-Oriented Programming in Java concept. The average utilization or application of object-oriented programming in Java has been in the top 5 positions for most of the enterprise applications and has been in almost every enterprise as of now is the most sought-after technology. There are huge numbers of tools available, such as IDEs, to develop applications using object-oriented programming in Java. Many companies are using Java-based applications for their requirements because of the ease of development and maintenance. The standalone apps developed in Java are mostly being used by many companies for their in-house tools They are developed based on Java Swing GUI toolkit and are now called Java FX in its recent version. The recent version of Java 8 provides great functional programming features and parallel processing capabilities with its Stream API.

Recommended Articles:

This has been a guide to Object-Oriented Programming in Java. Here we have discussed the Different concepts and the applications of Object-Oriented Programming in Java. You may also look at the following article to learn more –

  1. Oops, Java Interview Questions
  2. Java Testing Interview Questions
  3. Object in Java
  4. Overloading and Overriding in C#

以上是Java 中的面向对象编程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn