13种语言入门概览

2024-08-01 22:06:58 浏览数 (2)

不多说,直接上代码!

Python

代码语言:javascript复制
# 变量声明
x = 10
y = 20

# 函数定义
def add(a, b):
    return a   b

# 条件语句
if x < y:
    print("x is less than y")
else:
    print("x is greater than or equal to y")

# 循环
for i in range(1, 11):
    print(i)

# 类定义
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

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

# 创建对象
person = Person("Alice", 30)
person.greet()

C

代码语言:javascript复制
#include <iostream>
using namespace std;

// 函数声明
int add(int a, int b) {
    return a   b;
}

// 类定义
class Person {
public:
    string name;
    int age;

    Person(string n, int a) {
        name = n;
        age = a;
    }

    void greet() {
        cout << "Hello, my name is " << name << " and I am " << age << " years old." << endl;
    }
};

int main() {
    // 变量声明
    int x = 10;
    int y = 20;

    // 条件语句
    if (x < y) {
        cout << "x is less than y" << endl;
    } else {
        cout << "x is greater than or equal to y" << endl;
    }

    // 循环
    for (int i = 1; i <= 10;   i) {
        cout << i << endl;
    }

    // 创建对象
    Person person("Alice", 30);
    person.greet();

    return 0;
}

C

代码语言:javascript复制
#include <stdio.h>

// 函数声明
int add(int a, int b) {
    return a   b;
}

// 结构体定义
struct Person {
    char name[50];
    int age;
};

// 函数定义
void greet(struct Person p) {
    printf("Hello, my name is %s and I am %d years old.n", p.name, p.age);
}

int main() {
    // 变量声明
    int x = 10;
    int y = 20;

    // 条件语句
    if (x < y) {
        printf("x is less than yn");
    } else {
        printf("x is greater than or equal to yn");
    }

    // 循环
    for (int i = 1; i <= 10;   i) {
        printf("%dn", i);
    }

    // 创建对象
    struct Person person = {"Alice", 30};
    greet(person);

    return 0;
}

Java

代码语言:javascript复制
public class Main {
    // 函数声明
    public static int add(int a, int b) {
        return a   b;
    }

    // 类定义
    public static class Person {
        String name;
        int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        void greet() {
            System.out.println("Hello, my name is "   name   " and I am "   age   " years old.");
        }
    }

    public static void main(String[] args) {
        // 变量声明
        int x = 10;
        int y = 20;

        // 条件语句
        if (x < y) {
            System.out.println("x is less than y");
        } else {
            System.out.println("x is greater than or equal to y");
        }

        // 循环
        for (int i = 1; i <= 10; i  ) {
            System.out.println(i);
        }

        // 创建对象
        Person person = new Person("Alice", 30);
        person.greet();
    }
}

C#

代码语言:javascript复制
using System;

class Program {
    // 函数声明
    static int Add(int a, int b) {
        return a   b;
    }

    // 类定义
    class Person {
        public string Name;
        public int Age;

        public Person(string name, int age) {
            Name = name;
            Age = age;
        }

        public void Greet() {
            Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
        }
    }

    static void Main() {
        // 变量声明
        int x = 10;
        int y = 20;

        // 条件语句
        if (x < y) {
            Console.WriteLine("x is less than y");
        } else {
            Console.WriteLine("x is greater than or equal to y");
        }

        // 循环
        for (int i = 1; i <= 10; i  ) {
            Console.WriteLine(i);
        }

        // 创建对象
        Person person = new Person("Alice", 30);
        person.Greet();
    }
}

JavaScript

代码语言:javascript复制
// 变量声明
let x = 10;
let y = 20;

// 函数声明
function add(a, b) {
    return a   b;
}

// 条件语句
if (x < y) {
    console.log("x is less than y");
} else {
    console.log("x is greater than or equal to y");
}

// 循环
for (let i = 1; i <= 10; i  ) {
    console.log(i);
}

// 类定义
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greet() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }
}

// 创建对象
let person = new Person("Alice", 30);
person.greet();

Go

代码语言:javascript复制
package main

import "fmt"

// 函数声明
func add(a int, b int) int {
    return a   b
}

// 结构体定义
type Person struct {
    name string
    age  int
}

// 方法定义
func (p Person) greet() {
    fmt.Printf("Hello, my name is %s and I am %d years old.n", p.name, p.age)
}

func main() {
    // 变量声明
    x := 10
    y := 20

    // 条件语句
    if x < y {
        fmt.Println("x is less than y")
    } else {
        fmt.Println("x is greater than or equal to y")
    }

    // 循环
    for i := 1; i <= 10; i   {
        fmt.Println(i)
    }

    // 创建对象
    person := Person{"Alice", 30}
    person.greet()
}

SQL (using PL/pgSQL in PostgreSQL)

代码语言:javascript复制
-- 函数声明
CREATE OR REPLACE FUNCTION add(a INTEGER, b INTEGER) RETURNS INTEGER AS $$
BEGIN
    RETURN a   b;
END;
$$ LANGUAGE plpgsql;

-- 条件语句和循环
DO $$ 
DECLARE
    x INTEGER := 10;
    y INTEGER := 20;
BEGIN
    IF x < y THEN
        RAISE NOTICE 'x is less than y';
    ELSE
        RAISE NOTICE 'x is greater than or equal to y';
    END IF;

    FOR i IN 1..10 LOOP
        RAISE NOTICE '%', i;
    END LOOP;
END $$;

Rust

代码语言:javascript复制
// 函数声明
fn add(a: i32, b: i32) -> i32 {
    a   b
}

// 结构体定义
struct Person {
    name: String,
    age: u32,
}

// 方法定义
impl Person {
    fn greet(&self) {
        println!("Hello, my name is {} and I am {} years old.", self.name, self.age);
    }
}

fn main() {
    // 变量声明
    let x = 10;
    let y = 20;

    // 条件语句
    if x < y {
        println!("x is less than y");
    } else {
        println!("x is greater than or equal to y");
    }

    // 循环
    for i in 1..=10 {
        println!("{}", i);
    }

    // 创建对象
    let person = Person {
        name: String::from("Alice"),
        age: 30,
    };
    person.greet();
}

Ruby

代码语言:javascript复制
# 变量声明
x = 10
y = 20

# 函数声明
def add(a, b)
    a   b
end

# 条件语句
if x < y
    puts "x is less than y"
else
    puts "x is greater than or equal to y"
end

# 循环
(1..10).each do |i|
    puts i
end

# 类定义
class Person
    def initialize(name, age)
        @name = name
        @age = age
    end

    def greet


        puts "Hello, my name is #{@name} and I am #{@age} years old."
    end
end

# 创建对象
person = Person.new("Alice", 30)
person.greet

PHP

代码语言:javascript复制
<?php
// 变量声明
$x = 10;
$y = 20;

// 函数声明
function add($a, $b) {
    return $a   $b;
}

// 条件语句
if ($x < $y) {
    echo "x is less than yn";
} else {
    echo "x is greater than or equal to yn";
}

// 循环
for ($i = 1; $i <= 10; $i  ) {
    echo $i . "n";
}

// 类定义
class Person {
    public $name;
    public $age;

    function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    function greet() {
        echo "Hello, my name is $this->name and I am $this->age years old.n";
    }
}

// 创建对象
$person = new Person("Alice", 30);
$person->greet();
?>

Swift

代码语言:javascript复制
// 变量声明
var x = 10
var y = 20

// 函数声明
func add(a: Int, b: Int) -> Int {
    return a   b
}

// 条件语句
if x < y {
    print("x is less than y")
} else {
    print("x is greater than or equal to y")
}

// 循环
for i in 1...10 {
    print(i)
}

// 类定义
class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func greet() {
        print("Hello, my name is (name) and I am (age) years old.")
    }
}

// 创建对象
let person = Person(name: "Alice", age: 30)
person.greet()

Kotlin

代码语言:javascript复制
// 函数声明
fun add(a: Int, b: Int): Int {
    return a   b
}

// 类定义
class Person(val name: String, val age: Int) {
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

fun main() {
    // 变量声明
    val x = 10
    val y = 20

    // 条件语句
    if (x < y) {
        println("x is less than y")
    } else {
        println("x is greater than or equal to y")
    }

    // 循环
    for (i in 1..10) {
        println(i)
    }

    // 创建对象
    val person = Person("Alice", 30)
    person.greet()
}

0 人点赞