2013年9月11日 星期三

【語言比較】1.class

JAVA 

class:
class a01{
    private int x;
    int y;
   
    public void setx(int x){
        this.x = x;
    }
   
    public int getx(){
        return this.x;
    }
   
    public int area(){
        return this.x * this.y;
    }
}

使用
a01 a = new a01();
a.setx(10);
a.y = 20;
system.out.print(a.area());

ObjectiveC

a01.h檔
@property int x,y;

- (void) setx:(int)x;
- (int) getx();
- (int) area();

a01.m檔
@synthesize x,y;
- (void) setx:(int)setX{
    x = setX;
}
- (int) getx(){
  return x;
}
- (int) area(){
  return x*y;
}

使用
a01 *a = [[a01 alloc] init]
[a setx:10]
a.y = 20;
NSLog(@"%i",[a area]);


PHP

class:
class a01{
private $x;
public $y;
public function setx($x){
$this->x = $x;
public function getx(){
return $this->x;
}
public function area(){
return $this->x * $this->y;
}
}

使用
$a = new a01();
$a->setx(10);
$a->y = 20;
echo $a->area();

Javascript (網頁)

class:
function a01() {  
this.x;
this.y;

    this.setx = function(x) {  
        this.x = x;
    }
this.getx = function(){
return this.x;
}
this.area = function(){
return this.x * this.y;
}
}

使用
var a = new a01();
a.setx(10);
a.y = 20;
document.write(a.area());

沒有留言:

張貼留言