1.Concepts of Object-Oriented Programming

Object-oriented programming centers on objects, emphasizing the roles and subjects of things. It uses objects for macro-level control, while still being process-oriented at the micro level.

Object-oriented programming has the characteristics of abstraction, encapsulation, inheritance, and polymorphism.

2.Object-Oriented Programming

2.1 Definition of a Class

A class mainly consists of variables and methods.

Variable definition format:

modifier dataType variableName = [defaultValue];

Method definition format:

modifier returnType methodName(parameterList) {}

Example: Create three empty classes

public class Class1 {
}
class Class2 {
}
class Class3 {
}

2.2 Creation and Use of Objects

Syntax format for object creation:

ClassName objectName = new ClassName();

Example:

public class Student {
    // Assume these member variables are defined in the Student class
    int age;
    String name;
    
    // Assume these methods are defined in the Student class
    public void study() {
        System.out.println(name + " is studying");
    }
    
    public void eat(String food) {
        System.out.println(name + " is eating " + food);
    }
    
    public static void main(String[] args) {
        Student student1 = new Student(); // Create the first object
        student1.age = 18;
        student1.name = "Zhang San";
        student1.study();
        
        Student student2 = new Student(); // Create the second object
        student2.age = 24;
        student2.name = "Li Si";
        student2.eat("biscuits");
    }
}

Default Values of Member Variables

Data TypeDefault ValueData TypeDefault Value
Integer types0Booleanfalse
Floating-point types0.0Reference typesnull
Character type‘\u0000’

3.Constructor Methods

3.1 What is a Constructor Method

Syntax format for calling a constructor method:

modifier ClassName(parameterTypes) {}

3.2 Use of Constructor Methods

public class Gouzao {
    public static void main(String[] args) {
        Student student1 = new Student(); // Create object using no-argument constructor
        Student student2 = new Student("Zhang San", 23); // Create object using parameterized constructor
    }    
}

// Assume the Student class has these constructors
class Student {
    String name;
    int age;
    
    // No-argument constructor
    public Student() {}
    
    // Parameterized constructor
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

4.this Keyword

4.1 Use of the this Keyword

public class Point {
    double x;
    double y;
    
    public Point(double x, double y) {
        this.x = x; // this.x refers to the member variable, x refers to the parameter
        this.y = y;
    }
    
    public double calculateDistance(Point p) {
        double xLen = this.x - p.x; // this.x refers to the current object's x
        double yLen = this.y - p.y;
        return Math.sqrt(xLen * xLen + yLen * yLen);
    }
    
    public static void main(String[] args) {
        Point point1 = new Point(0, 0);
        Point point2 = new Point(3, 4);
        System.out.println("Distance: " + point1.calculateDistance(point2));
    }
}
Avatar

By BytePilot

Because sharing makes us better. Let’s learn, build, and grow — one byte at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *