DeveloperBreeze

Class with Method

python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f'Hello, my name is {self.name} and I am {self.age} years old.'

# Create an instance of Person
person = Person('Alice', 25)

# Call the greet method and store the result in 'greeting'
greeting = person.greet()

Continue Reading

Discover more amazing content handpicked just for you

Code
php

PHP Class and Object Example

// Define a Person class
class Person {
    // Private property to store the person's name
    private $name;

    // Constructor to initialize the person's name
    public function __construct($name) {
        $this->name = $name;
    }

    // Method to greet the person
    public function greet() {
        return 'Hello, my name is ' . $this->name;
    }
}

// Create an instance of the Person class with the name 'Alice'
$person = new Person('Alice');

// Call the greet method and echo the result
echo $person->greet();

Jan 26, 2024
Read More
Code
javascript

JavaScript Class with Constructor and Method

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!