As a high-level programming language, Java language adopts Object Oriented Programming (OOP for short) as its basic programming paradigm. As an idea and method in programming, object-oriented programming has become the most important programming model in the field of computer science. In this article, we will introduce object-oriented programming design in Java language.
1. What is object-oriented programming?
Object-oriented programming is an object-based programming method. Its core idea is to decompose the program into several modules, each module has its own data and behavior. Each module is an object, and the objects collaborate to complete a certain task by calling each other's methods.
The core concepts in object-oriented programming are "classes" and "objects". A class defines the properties and behavior of an object, and an object is an instance of a class. Classes allow you to create multiple objects that all have the same properties and behavior. This programming method can effectively achieve code reuse, encapsulation and maintenance, making the program more readable and maintainable.
Object-oriented programming in the Java language is based on the Java Virtual Machine (JVM). JVM is a virtual machine that can run on different platforms. Therefore, Java programs can run on different operating systems, such as Windows, Mac, Linux, etc.
2. Object-oriented programming in Java
Object-oriented programming in Java mainly includes the following aspects:
1. Classes and objects
"Class" in Java is a template used to describe the status and behavior of a certain type of object. An "object" is an instance of a class, which has the properties and methods described by the class. In Java, classes are defined through the class keyword.
For example, a Person class is defined below:
public class Person{ private String name; // 姓名 private int age; // 年龄 // 构造方法 public Person(String name, int age){ this.name = name; this.age = age; } //获取姓名 public String getName(){ return name; } //获取年龄 public int getAge(){ return age; } //输出信息 public void printInfo(){ System.out.println("姓名:" + name + ",年龄:" + age); } }
In the above program, we define a Person class, which contains name and age attributes and methods for obtaining name, age, and output information. method. When using it, you can create an object through the following code:
Person p = new Person("张三", 20);
Among them, the new keyword is used to create an instance of the Person class, that is, a Person object.
2. Encapsulation and inheritance
Encapsulation (Encapsulation) in Java refers to the process of packaging the data and behavior of an object together and hiding the internal implementation details from the outside world. In Java, encapsulation is achieved through access control keywords private, public, protected, etc.
Inheritance means that one class inherits the properties and methods of another class. In Java, inheritance is implemented through the keyword extends. Subclasses can inherit the properties and methods of the parent class, and can also override (Override) the methods of the parent class.
The following is an example of inheritance:
public class Student extends Person{ private String school; // 学校 // 构造方法 public Student(String name, int age, String school){ super(name, age); this.school = school; } //获取学校 public String getSchool(){ return school; } //输出信息 public void printInfo(){ super.printInfo(); // 调用父类的printInfo方法 System.out.println("所在学校:" + school); } }
In the above program, we define a Student class, which inherits the attributes and methods of the Person class, and adds the school attribute and getSchool, printInfo method.
3. Polymorphism
Polymorphism in Java refers to different manifestations of the same behavior. In Java, polymorphism is achieved through inheritance and override.
The following is a polymorphic example:
public class Test{ public static void main(String[] args){ Person p = new Person("张三", 20); p.printInfo(); Student s = new Student("李四", 18, "北大"); s.printInfo(); p = s; // 多态 p.printInfo(); } }
In the above program, we define a Person class and a Student class, each of which has its own printInfo method. In the main method, we first create a Person object p and output its information; then create a Student object s and output its information; then we implement polymorphism through "p = s", and when we call p's printInfo method again, What is actually called is the printInfo method of the Student class.
4. Interface
The interface in Java is a set of abstract methods. All methods in the interface are abstract and have no specific implementation. Interfaces can be implemented by classes, and a class can implement multiple interfaces. In Java, interfaces are defined through the keyword interface.
The following is an example of an interface:
interface IShape{ public abstract double area(); } class Circle implements IShape{ private double radius; public Circle(double radius){ this.radius = radius; } public double area(){ return Math.PI * radius * radius; } } class Rectangle implements IShape{ private double width, height; public Rectangle(double width, double height){ this.width = width; this.height = height; } public double area(){ return width * height; } } public class Test{ public static void main(String[] args){ IShape c = new Circle(2.0); IShape r = new Rectangle(3.0, 4.0); System.out.println("圆面积:" + c.area()); System.out.println("矩形面积:" + r.area()); } }
In the above program, we define an IShape interface and two classes, Circle and Rectangle, both of which implement the IShape interface. In the main method, we create Circle and Rectangle objects and call their area methods respectively.
Summary
The Java language uses object-oriented programming (OOP) as its basic programming paradigm, and implements program design through mechanisms such as classes and objects, encapsulation and inheritance, polymorphism and interfaces. Programs written using object-oriented programming methods have good maintainability, scalability and better code reusability.
The above is the detailed content of Introduction to object-oriented programming design in Java language. For more information, please follow other related articles on the PHP Chinese website!