Home >Java >javaTutorial >JAVA core programming technology analysis
JAVA core programming technology analysis
Java is an object-oriented programming language that is widely used in many industries and fields. As a Java developer, it is very important to master Java core programming technology. This article will analyze Java core programming technologies through specific code examples and help readers better understand and apply these technologies.
First, let’s introduce one of the most basic programming concepts in Java: classes and objects. In Java, everything is based on classes and objects. A class is a description of a group of similar objects that defines the properties and behavior of the objects. An object is an instantiation of a class. We create an object to use the properties and behaviors defined in the class.
Example 1: Define a simple Person class, including name and age attributes, as well as a method to print information.
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public void printInfo() { System.out.println("姓名:" + name); System.out.println("年龄:" + age); } } // 测试代码 public class Test { public static void main(String[] args) { Person person = new Person("张三", 20); person.printInfo(); } }
In the above example, we first defined a Person class, which contains two private properties name and age, and a public method printInfo. In the main method, we create a Person object person and print the object's information by calling the printInfo method of the person object.
Next, let’s introduce encapsulation and inheritance in Java. Encapsulation refers to encapsulating data and behaviors in objects, hiding internal implementation details from the outside, and providing public interfaces for other objects to access and use. Inheritance refers to creating a new class that inherits the properties and behaviors of an existing class, and can add new properties and behaviors on this basis.
Example 2: Define a simple animal class Animal, including name and age attributes, as well as movement methods. Then define a dog class Dog, inherit from the Animal class, and add additional methods.
public class Animal { private String name; private int age; public Animal(String name, int age) { this.name = name; this.age = age; } public void move() { System.out.println(name + "正在移动..."); } } public class Dog extends Animal { public Dog(String name, int age) { super(name, age); } public void bark() { System.out.println("汪汪汪!"); } } // 测试代码 public class Test { public static void main(String[] args) { Dog dog = new Dog("旺财", 3); dog.move(); // 调用从Animal类继承的方法 dog.bark(); // 调用Dog类自己的方法 } }
In the above example, we first defined an Animal class, which contains two private properties name and age, and a public method move. Then, we defined a Dog class, inherited from the Animal class, and added an additional method bark. In the main method, we create a Dog object dog and call the move method inherited from the Animal class and the Dog class's own bark method.
In addition to encapsulation and inheritance, Java also provides polymorphism. Polymorphism refers to the same type of objects that will show different forms under different circumstances. By implementing interfaces and inheriting abstract classes, we can achieve polymorphism.
Example 3: Define a simple interface Shape, including methods for calculating area. Then, define a rectangle class Rectangle and a circle class Circle, implement the Shape interface, and implement methods to calculate the area respectively.
public interface Shape { double calculateArea(); } public class Rectangle implements Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double calculateArea() { return length * width; } } public class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double calculateArea() { return Math.PI * radius * radius; } } // 测试代码 public class Test { public static void main(String[] args) { Shape rectangle = new Rectangle(3, 4); Shape circle = new Circle(5); System.out.println("矩形的面积:" + rectangle.calculateArea()); System.out.println("圆形的面积:" + circle.calculateArea()); } }
In the above example, we first define a Shape interface, which contains a method calculateArea to calculate the area. Then, a Rectangle class and a Circle class are defined, which implement the Shape interface and the method of calculating the area respectively. In the main method, we created a Rectangle object and a Circle object, and calculated and printed the areas of the rectangle and circle respectively by calling the calculateArea method.
Through the above code examples, we briefly analyzed concepts such as classes and objects, encapsulation and inheritance, and polymorphism in Java core programming technology. These are very important basic knowledge in Java programming, I hope it will be helpful to readers. Through continuous practice and practice, I believe readers can understand and apply these technologies more deeply and achieve better results in Java development.
The above is the detailed content of JAVA core programming technology analysis. For more information, please follow other related articles on the PHP Chinese website!