流行程式語言的詳細對比(6)--物件建立和解構函式
物件建立
Java
(1)使用new關鍵字,呼叫了建構函式
Employee emp1 = new Employee();
(2)使用Class類的newInstance方法,呼叫了建構函式
Employee emp2 = (Employee) Class.forName("org.programming.mitra.exercises.Employee").newInstance();
或者
Employee emp2 = Employee.class.newInstance();
(3)使用Constructor類的newInstance方法,呼叫了建構函式
和Class類的newInstance方法很像,
java.lang.reflect.Constructor類裡也有一個newInstance方法可以建立物件。
我們可以通過這個newInstance方法呼叫有引數的和私有的建構函式。
Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
(4)使用clone方法
Employee emp4 = (Employee) emp3.clone();
(5)使用反序列化
ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in .readObject();
Js
(1)原始方式
var oCar = new Object;
oCar.color = "blue";
oCar.showColor = function() {
alert(this.color);
};
(2)工廠方式
function createCar() {
var oTempCar = new Object;
oTempCar.color = "blue";
oTempCar.showColor = function() {
alert(this.color);
};
return oTempCar;
}
var oCar1 = createCar();
(3)建構函式方式
function Car(sColor,iDoors,iMpg) {
this.color = sColor;
this.showColor = function() {
alert(this.color);
};
}
var oCar1 = new Car("red",4,23);
(4)原型方式
function Car() {
}
Car.prototype.color = "blue";
Car.prototype.showColor = function() {
alert(this.color);
};
var oCar1 = new Car();
(5)混合的建構函式,原型方式推薦
function Car(sColor) {
this.color = sColor;
}
Car.prototype.showColor = function() {
alert(this.color);
};
var oCar1 = new Car("red");
(6)動態原型,推薦
function Parent(){
this.name="李小龍";
if(typeof Parent._lev=="undefined"){
Parent.prototype.lev=function(){
return this.name;
} }
};
var x =new Parent();
Python
class Employee:
'所有員工的基類'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
"建立 Employee 類的第一個物件"
emp1 = Employee("Zara", 2000)
"建立 Employee 類的第二個物件"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
Go
golang 中有兩個記憶體分配機制 :new和make,二者有明顯區別.
new:用來初始化一個物件,並且返回該物件的首地址.其自身是一個指標.可用於初始化任何型別
make:返回一個初始化的例項,返回的是一個例項,而不是指標,其只能用來初始化:slice,map和channel三種類型
package main
import (
"fmt"
)
func main() {
a := new([]int)
fmt.Println(a) //輸出&[],a本身是一個地址
b := make([]int, 1)
fmt.Println(b) //輸出[0],b本身是一個slice物件,其內容預設為0
}
通過這個例子可以看出,當對slice,map以及channel進行初始化時,使用make比new方式要好,而其他形式的則利用new進行初始化.
初始化:
使用new進行初始化時只能是預設初始化,無法賦值.很多時候,預設初始化並不是一個好主意,例如一個結構體,預設值的結構體初始化並沒有多大用處,所以面對結構體初始化我們一般適用如下方式:
type Rect struct {
x, y float64
width, height float64
}
所以我們通過在結構體前面新增取地址符號&對該結構體進行初始化:
rect3 := &Rect{0, 0, 100, 200}
rect4 := &Rect{width: 100, height: 200}
這種初始化方式在golang中初始化結構體是十分常見的.
Scala
import java.io._
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("x 的座標點: " + x);
println ("y 的座標點: " + y);
}
}
object Test {
def main(args: Array[String]) {
val pt = new Point(10, 20);
// 移到一個新的位置
pt.move(10, 10);
}
}
PHP
class Site {
/* 成員變數 */
var $url;
var $title;
/* 成員函式 */
function setUrl($par){
$this->url = $par;
}
function getUrl(){
echo $this->url . PHP_EOL;
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title . PHP_EOL;
}
//建構函式
function __construct( $par1, $par2 ) {
$this->url = $par1;
$this->title = $par2;
}
}
$runoob = new Site;
解構函式
Java
protected void finalize(){
System.out.println("in finalize");
}
在 Java 程式設計裡面,一般不需要我們去寫析構方法。
Js
Js沒有解構函式
Python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Point:
def __init__( self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "銷燬"
pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # 列印物件的id
del pt1
del pt2
del pt3
#以上例項執行結果如下:
#3083401324 3083401324 3083401324
#Point 銷燬
Go
因為型別就是簡單的結構體,所以型別並沒有所謂的建構函式和解構函式
Scala
Scala無解構函式
PHP
<?php
class MyDestructableClass {
function __construct() {
print "建構函式\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "銷燬 " . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>