目录
Python
x = 10
y = 100
x, y = y, x
print(x)
print(y)
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
C/C++
#include <iostream>
using namespace std;
void main()
{
int a = 10;
int b = 100;
int temValue = 0;
printf("--------------before---------------------");
printf("a = %d ; b = %d\n", a, b);
temValue = a;
a = b;
b = temValue;
printf("--------------after---------------------");
printf("a = %d ; b = %d\n", a, b);
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Change
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 100;
Console.WriteLine("---------------before--------------");
Console.WriteLine(" a is {0}, b is {1} \n", a, b);
ChangValue(ref a, ref b);
Console.WriteLine("---------------after--------------");
Console.WriteLine(" a is {0}, b is {1} \n", a, b);
ChangValueNo(b:100, a:10);
Console.WriteLine(" ChangValueNo : a is {0}, b is {1} \n", a, b);
Console.Read();
}
static void ChangValue(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static void ChangValueNo( int a, int b)
{
int temp = a;
a = b;
b = temp;
}
}
}
Java
package com.company;
public class JavaLearn {
public static void main(String[] args)
{
int x = 1;
int y = 100;
changValue( x, y);
System.out.print(x+"\n");
System.out.print(y+"\n");
System.out.printf("x is %d, y is %d " + "\n", x, y);
int arrTest[] = new int[3];
for (int i = 0; i < arrTest.length; i++)
{
System.out.format("arrTest[%d] is %d\n", i, arrTest[i]);
}
//默认初始化:数组是引用类型,它的元素相当于类的成员变量,
// 因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
int arr[] = {1, 100};
changValueArr( arr);
System.out.printf("x is %d, y is %d ", arr[0], arr[1]);
}
private static void changValue(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
private static void changValueArr(int[] arr)
{
int temp = arr[0];
arr[0] = arr[1];
arr[1] = temp;
}
}