我试图在vb.net中创建一个动态对象,使用PHP会很简单,它有getter,setter和调用方法的魔术方法,但我需要在vb.net中这样做
在PHP中:
<?php
class foo {
private $vars = array()
public function __construct() {}
public function __get($name) {
if (in_array($name, $this->vars)) {
return $this->vars[$name];
}
}
public function __set($name, $value) {
$this->vars[$name] = $value;
}
public function __call($method, $arguments) {
.....
}
public function __callStatic($method, $arguments) {
.....
}
}
?>
然后我就可以使用这个对象:
<?php
// Calles foo::__construct as normal
$myFoo = new foo();
// Calls the __set method parsing the values $name as "myVar1" and $value as "foo"
$myFoo->myVar1 = "foo";
// Runs the __set method parsing the values $name as "myVar2" and $value as "bar"
$myFoo->myvar2 = "bar";
// Calls the __get method parsing the value $name as "myVar1"
// and then a seccond call to __get parsing $name as "myVar2"
echo $myFoo->myVar1 . $myFoo->myVar2
// Calls __call parsing $name as "foobar" and $values as null
$myFoo->foobar();
// Calls __callStatic parsing $name as "barfoo" and $values as null
foo::barfoo();
?>
vb.net能否应对这个基本的编程水平还是根本不可能?
答案 0 :(得分:3)
基本上,这一切都归结为:VB .Net是一种动态语言吗?
答案是否定的。
那么,动态(如PHP)和静态(如VB .Net)语言之间有什么区别,哪一种最好?Here is an answer I found here:
两者都不“更好”。他们优化不同的变量。您想要优化哪个变量取决于您要完成的任务。
所有语言都旨在将人类可读代码转换为机器指令。动态语言(Lisp,Perl,Python,Ruby)旨在优化程序员效率,因此您可以使用更少的代码实现功能。静态语言(C,C ++等)旨在优化硬件效率,以便您编写的代码尽快执行。
动态语言的关键定义功能是始终可以使用整个语言。这意味着您可以在编译时执行代码,并在执行时编译代码。这与其他功能(一流功能,内省)相结合,可实现元编程,即可自行修改的程序。与静态语言相比,这使得程序员可以用更少的工作完成相同的任务,并且在某些情况下允许您做一些在静态语言中无法完成的事情。然而,所有这些魔法都以牺牲执行速度为代价。
静态语言没有做太多(如果有的话)幕后魔术,所以开销要少得多。由于编译阶段和执行阶段完全解耦,因此编译器可以运行更长时间并生成更优化的机器代码。对于某些任务,静态代码比动态语言快几个数量级。
两种语言都有自己的位置。 Perl,Python和Ruby是大多数日常任务的优秀语言 - 但它们都是用C语言编写的。
答案 1 :(得分:0)
总之经过长时间的讨论,在VB.net中无法做到这一点