Статические поля и методы

Последнее обновление: 28.03.2024

C++

C#

Dart

JavaScript

Kotlin

Python

C++

#include <iostream>
 
class Person
{
public:
    static inline std::string className {"Person"};

    Person(std::string p_name, unsigned p_age): name{p_name}, age{p_age} { }
    void print()
    { 
        std::cout << "Name: " << name << "  Age:" << age << std::endl;
    }
    static void print_info()
    {
        std::cout << "Person class has two fields: name and age and a print method" << std::endl;
    }
private:
    std::string name;
    unsigned age{};
};
 
int main()
{
    std::cout << Person::className << std::endl;
    Person::print_info(); 
}

C#

Console.WriteLine(Person.className);
Person.PrintInfo();

class Person
{
    public static string className = "Person";
    string name;
    int age;
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
    public void Print()
    { 
        Console.WriteLine($"Имя: {this.name}  Возраст: {this.age}");
    }
    public static void PrintInfo()
    { 
        Console.WriteLine("Person class has two fields: name and age and a Print method");
    }
}

Dart

class Person{
 
    String name;
    int age; 
    static String className = "Person";
 
    Person(this.name, this.age){ }
    
    void display(){
        print("Name: $name \tAge: $age");
    }
    static void printInfo(){ 
        print("Person class has two attributes: name and age and a display method");
    }
}
void main (){
    print(Person.className);
    Person.printInfo();
}

JavaScript

class Person{
    static className = "Person";
    constructor(name, age){
        this.name = name;
        this.age = age;
    }
    print(){ 
        console.log(`Name: ${this.name}  Age: ${this.age}`);
    }
    static printInfo(){ console.log("Person class has two fields: name and age and a print method");}
}
console.log(Person.className);
Person.printInfo();

Kotlin

class Person(val name: String, val age: Int){
  
    fun display(){
        print("Name: `$name` \tAge: `$age`")
    }
    companion object {
        val className = "Person"
        fun printInfo(){ 
            println("Person class has two attributes: name and age and a display method")
        }
    }
}
fun main (){
    println(Person.className)
    Person.printInfo()
}

Python

class Person:
    className = "Person"
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def display(self):
        print(f"Name: {self.name}  Age: {self.age}")
    @staticmethod
    def print_info():
        print("Person class has two attributes: name and age and a display method")
 
print(Person.className)
Person.print_info()

Rust

fn main(){
     
    println!("{}", Person::class_name);
    Person::print_info();
}
struct Person
{
    name: String,
    age: u8 
}
impl Person{
    const class_name : &'static str = "Person";
    fn display(&self){
        println!("Name: {}  Age: {}", &self.name, &self.age);
    }
    fn print_info(){
        println!("Person class has two attributes: name and age and a display method");
    }
}
Помощь сайту
Юмани:
410011174743222
Перевод на карту
Номер карты:
4048415020898850