//Java 数组类型
int[] a = { 1, 2, "three" };
//error incompatible types: String connot be converted to int
int[] b = new int[10];
b[10] = 11;
//exception in thread main java.lang.ArrayIndexOutOfBoundsException
//Javascript 稀疏数组
var a = [, , 3];
a.forEach(function(v){
console.log(v)
});
//3
如果我们初始化的时候指定了缺省值,这时数组就不是稀疏数组了。
//Javascript 指定数组元素为undefined
var a = [undefined, 1]
, b = [, 1];
console.log(0 in a); //true
console.log(0 in b); //false
Object.keys(a); //["0", "1"]
Object.keys(b); //["1"]
上述例子中in关键词可以判断对象中是否存在键,因为数组a初始化了第一个元素,所以0 in a返回为true。数组b在初始化时跳过了索引为0的值,所以0 in b返回值为false。这里a就是稠密数组,b就是稀疏数组。
//Java 通过ArrayList的add方法拓展数组元素
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.set(3, 3);
//Exception in thread "main" java.lang.IndexOutOfBoundsException
//JavaScript 对象
var language = { name: "javascript", type: "dynamic" }
console.log(language.name); //javascript
//Java 散列
Map<String, String> language = new HashMap<String, String>(){
{
put("name", "java");
put("type", "static");
}
};
System.out.println(language.get("name")); //java
注意散列和数组一样,是引用类型,在使用时要特别小心,如果不想破坏原引用最好进行深度拷贝。
#Ruby 散列属于引用类型
language = {}
a = language
a["name"] = "Ruby"
puts language["name"] #Ruby
//JavaScript 散列属于引用类型
var language = {};
var a = language;
a.name = "JavaScript";
console.log(language.name); //JavaScript
//Java 散列属于引用类型
Map<String, String> language = new HashMap<String, String>();
Map<String, String> a = new HashMap<String, String>();
a = language;
a.put("name", "Java");
System.out.println(language.get("name")); //Java
//Java 通过匿名内部类保证键值不可修改
public static void main(String args[]){
String name = "name";
Map<String, String> language = new HashMap<String, String>(){
{
put(name, "java");
put("type", "static");
}
};
name = "NAME";
//error: local variables referenced from a inner class must be final or effectively final
}
//C 枚举
enum season {
Spring = 0,
Summer = 1,
Autumn = 2,
Winter = 3;
};
//Java v1.5后引入枚举
public enum Season {
SPRING, SUMMER, AUTUMN, WINTER;
}
在分支语句中使用枚举类型能提高代码的可读性:
//Java 分支语句中使用枚举
public static void main(String args[]){
Color color = Color.RED;
switch(color){
case RED:
System.out.println("red");
break;
case GREEN:
System.out.println("green");
break;
case YELLOW:
System.out.println("yellow");
break;
}
}
枚举的另外一个主要用途是充当静态常量,与静态常量相比,枚举类型更加安全。
//Java 常量在使用时需要合法性验证
interface Color{
static final int RED = 1;
static final int GREEN = 2;
static final int YELLOW = 3;
}
public class Main{
public Boolean isRed(int i){
//为保证参数有效,需要合法性验证
if(i < 1 || i > 3) System.out.println("error code");
if(i == 1) return true;
else return false;
}
public static void main(String args[]){
Main main = new Main();
if(main.isRed(Color.RED)) System.out.println("red"); //red
}
}
//Java 枚举类型比常量更加安全
enum Color{
RED,GREEN,YELLOW;
}
public class Main{
public Boolean isRed(Color color){
//枚举类型限定了输入参数,保证了合法性输入
if(color.ordinal() == 0) return true;
else return false;
}
public static void main(String args[]){
Main main = new Main();
Color red = Color.RED;
if(main.isRed(red)) System.out.println("red"); //red
}
}
//Java 显式定义构造函数并执行赋值操作
class Demo{
public int a, b;
public Demo(int a, int b){
//构造函数
this.a = a;
this.b = b;
}
public static void main(String args[]){
Demo d = new Demo(1, 2);
System.out.println(d.a +","+ d.b); //1,2
}
//Java 类方法和实例方法
class Animal{
public static int numbers;
public int index;
public static int getNumbers(){
return numbers;
}
public int getIndex(){
return index;
}
public Animal(){
numbers++;
index++;
}
public static void main(String args[]){
Animal cat = new Animal();
Animal dog = new Animal();
System.out.println(Animal.getNumbers()); //2
System.out.println(dog.getIndex()); //1
}
}
//Java 类的私有方法
class A{
private void privateMethod(){
System.out.println("This is a private method of class A");
}
public void callPrivateMethod(){
this.privateMethod();
}
}
class B extends A{}
public class Demo{
public static void main(String args[]){
A a = new A();
B b = new B();
a.privateMethod();
//error: privateMethod() has private access in A
a.callPrivateMethod();
//This is a private method of class A
b.privateMethod();
//error: cannot find symbol b.privateMethod();
b.callPrivateMethod();
//This is a private method of class A
}
}
#Ruby 类的私有方法
class A
private
def privateMethod
puts "this is private method of class A"
end
public
def callPrivateMethod
privateMethod()
end
end
class B < A
end
a = A.new
b = B.new
a.privateMethod
#private method `privateMethod' called for #<A:0x007fde9309e038> (NoMethodError)
a.callPrivateMethod
#this is private method of class A
b.privateMethod
#private method `privateMethod' called for #<A:0x007fde9309e038> (NoMethodError)
b.callPrivateMethod
#this is private method of class A
//Java 属性构造器
class A{
private String privateVariable;
public void setVariable(String a){
this.privateVariable = a;
}
public String getVariable(){
return this.privateVariable;
}
}
public class Demo{
public static void main(String args[]){
A a = new A();
a.privateVariable = "private variable";
//error: privateVariable has private access in A
System.out.println(a.privateVariable);
//error: privateVariable has private access in A
a.setVariable("private variable");
System.out.println(a.getVariable());
//private variable
}
}
//C++ 虚函数
class A{
public:
virtual void VirtualFunction(){
cout << "This is virtual function of class A";
}
};
class B : public A{
public:
void VirtualFunction(){
cout << "Rewrite virtual function in class B";
}
};
//Java 函数重写
class A{
public void overrideFunction(){
System.out.println("function of class A");
}
}
class B extends A{
public void overrideFunction(){
System.out.println("function override by class B");
}
}
class C extends A{
public void overrideFunction(){
System.out.println("function override by class C");
}
}
public class Main{
public static void main(String args[]){
A a = new A();
B b = new B();
C c = new C();
a.overrideFunction(); //function of class A
b.overrideFunction(); //function override by class B
c.overrideFunction(); //function override by class C
}
}
//Java 匿名内部类模拟匿名函数
class Demo{
public static void main(String args[]){
new Object(){
void add(int a, int b){ System.out.println(a + b); }
}.add(1, 2);
}
}