在Javascript中,似乎使用属性访问器并不是那么常见(不像其他OO语言,例如Java).
如果我有一个Person名字的对象,定义为
function Person(name) {
this.name = name;
}
Run Code Online (Sandbox Code Playgroud)
一个人的名字不会改变,但我确实希望能够在需要时访问它,所以我可以这样做:
function Person(name) {
var name = name;
this.getName = function() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
即使在动态语言中,我认为使用getter和setter的原理应用与静态类型的OO语言相同(例如封装,添加验证,限制访问等)
这个问题可能会被认为是主观的,但我很好奇为什么这种行为不会更频繁出现(例如,如果一切都是公开的,Java开发人员就会疯狂).
在javascript中有一种"标准"的方法吗?我见过Object.defineProperty,但并非所有浏览器都支持.
是否可以执行以下操作:
struct test
{
this
{
get { /*do something*/ }
set { /*do something*/ }
}
}
Run Code Online (Sandbox Code Playgroud)
所以,如果有人试图这样做,
test tt = new test();
string asd = tt; // intercept this and then return something else
Run Code Online (Sandbox Code Playgroud) 我有一个具有一些依赖属性的类,但我真的只想计算一次.
我刚刚得出结论,在MATLAB中对依赖类属性使用惰性求值要么是不可能的,要么是个坏主意.最初的计划是为每个需要更新的(公共)属性创建一个私有逻辑标志,并让构造函数将其设置为true.然后,当调用属性访问器时,它将检查该标志并计算该值并仅在需要时将其存储(在另一个私有属性中).如果该标志为false,则只返回缓存值的副本.
我认为困难在于对属性访问者的限制,即他们只留下其他不相关的属性.换句话说,get.property(self)方法不能更改self对象的状态.有趣的是,这在我当前的课堂上无声地失败了.(即,更新标志和缓存计算结果都没有在get.方法中设置,因此每次都运行昂贵的计算).
我怀疑是将lazy属性从公共依赖属性更改为具有公共GetAccess但私有SetAccess的方法将起作用.但是,我不喜欢以这种方式欺骗财产惯例.我希望只有一个"懒惰"的属性可以为我做这一切.
我错过了一些明显的东西吗 是否禁止在MATLAB中依赖类属性的访问器方法来更改类实例的状态?如果是这样,那么定义具有私有副作用的访问者的数量是获得我想要的行为的最不邪恶的方式吗?
编辑:这是一个测试类......
classdef LazyTest
properties(Access = public)
% num to take factorial of
factoriand
end
properties(Access = public, Dependent)
factorial
end
properties(Access = private)
% logical flag
do_update_factorial
% old result
cached_factorial
end
methods
function self = LazyTest(factoriand)
self.factoriand = factoriand;
self.do_update_factorial = true;
end
end
methods
function result = get.factorial(self)
if self.do_update_factorial
self.cached_factorial = factorial(self.factoriand);
% pretend this is expensive
pause(0.5)
self.do_update_factorial = false
end
result = self.cached_factorial;
end
end
end
Run Code Online (Sandbox Code Playgroud)
运行它
close all; …Run Code Online (Sandbox Code Playgroud) 我面临着VB.NET和C#(.NET2)中的情况,具有静态/共享成员的可见性.在VB.NET中我觉得有点奇怪:
public class A
{
private static A instance;
public static A Instance
{
get { return instance; }
}
public string Name { get { } }
}
Run Code Online (Sandbox Code Playgroud)
usage:
A.Instance.Name// ONLY名称是"可见的"
VB.NET:
Public Class A
Private Shared _instance As A
Public Shared ReadOnly Property Instance() As A
Get
Return _instance
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return ""
End Get
End Property
End Class
Run Code Online (Sandbox Code Playgroud)
用法:
A.Instance.Instance.Instance.Instance...
Run Code Online (Sandbox Code Playgroud)
//共享成员的行为就像一个公共类,我可以重复它到无限...
这是微软的疏忽还是VB.NET的"功能"?
我有一个带有类变量的模块
module Abc
@@variable = "huhu"
def self.get_variable
@@variable
end
class Hello
def hola
puts Abc.get_variable
end
end
end
a = Abc::Hello.new
a.hola
Run Code Online (Sandbox Code Playgroud)
是否可以不使用方法进入@@variable内部?我的意思是说会很好.只是好奇.Helloget_variableAbc.variable
使用Java,有没有办法制作一个可以像数组一样使用[]访问器的自定义类?
正常数组
int[] foo = int[5];
foo[4] = 5;
print(foo[4]);
//Output: "5"
Run Code Online (Sandbox Code Playgroud)
自定义类
class Bar {
//Custom class that uses index as a ref
}
Bar foo = new Bar(5);
foo.set(4, 5);
print(foo[4]);
//Output: "5"
Run Code Online (Sandbox Code Playgroud) ECMAScript版本5规范引入了一种称为访问器属性的新类型属性.与称为数据属性的现有和已知类型的属性相比,这两个事物如何仅在规范方面相互关联?
我已经阅读了ECMAScript v5的规范,我不清楚确切的区别.有人可以用代码示例解释这两个吗?我搜索过互联网,但所有的例子看起来都很模糊.
试图在查询构建器中获取访问器但抛出错误“ Undefined property: stdClass::$shorcontent”
//controller
public function index(){
$articles = DB::table('articles')->paginate(10);
return view('articles.index', ['articles' => $articles], compact('articles'));
}
Run Code Online (Sandbox Code Playgroud)
这是带有访问器的模型文件
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'user_id', 'content', 'live', 'post_on'
];
protected $guarded = ['id'];
public function getShortContentAttribute()
{
return substr($this->content,0, random_int(60, 150));
}
}
Run Code Online (Sandbox Code Playgroud)
这是视图
//article/index.blade.php View
<td class="col-md-6">{{ $article->shortcontent }} </td>
Run Code Online (Sandbox Code Playgroud)
当我使用 eloquent 而不是查询构建器时,相同的代码工作,就像这样
public function index()
{
$articles = Article::paginate(10);
return view('articles.index', ['articles' => $articles], compact('articles'));
}
Run Code Online (Sandbox Code Playgroud) 在基类中我有这个属性:
public virtual string Text
{
get { return text; }
}
Run Code Online (Sandbox Code Playgroud)
我想覆盖它并返回一个不同的文本,但我也希望能够设置文本,所以我这样做:
public override string Text
{
get { return differentText; }
set { differentText = value; }
}
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用.我得到一个红色的波浪形,set说我无法覆盖,因为它没有设置访问器.为什么这是个问题?我该怎么办?
我需要类似于ReadToEnd或ReadAllBytes的东西来使用MappedViewAccessor读取MemoryMappedFile的所有内容,如果我不知道它的大小,我该怎么办呢?
我已经搜索过了,我已经看到了这个问题,但这不是我要找的东西:
编辑:
有一个问题,(int)stream.Length没有给我正确的长度,而是给出了内部缓冲区的大小!我需要刷新这个问题,因为它非常紧迫.
accessor ×10
c# ×4
.net ×2
javascript ×2
properties ×2
arrays ×1
class ×1
ecmascript-5 ×1
inheritance ×1
ipc ×1
java ×1
laravel ×1
matlab ×1
oop ×1
php ×1
ruby ×1
struct ×1
vb.net ×1