accessor

javascript property accessors

馋奶兔 提交于 2019-12-01 15:26:31
In Javascript, it seems like using property accessors is not all that common (unlike in other OO languages such as Java for example). If I have a Person object with a name, defined as function Person(name) { this.name = name; } A person's name is not going to change, but I do want to be able to access it when needed, so I could do something like: function Person(name) { var name = name; this.getName = function() { return name; } } Even in a dynamic language, I think the principles of using getters and setters apply the same way they do to statically typed OO languages (e.g. encapsulation,

common lisp: slot-value for defstruct structures

孤者浪人 提交于 2019-12-01 15:03:55
问题 In common lisp, what can I use to access structure slot using slot name/symbol? What I want is (defstruct point (x 0) (y 0)) (defmacro -> (struct slot) `(slot-value ,struct ,slot)) (setf p (make-point)) (setf (slot-value p 'x) 1) (setf (-> p 'y) 2) I'm using clozure cl, and In clozure cl this works. However, AFAIK this is non-standard behavior (equivalent to "undefined behavior" C++). I'm not planning to switch to another CL implementation, so should I keep using slot-value for structures, or

javascript property accessors

China☆狼群 提交于 2019-12-01 14:23:13
问题 In Javascript, it seems like using property accessors is not all that common (unlike in other OO languages such as Java for example). If I have a Person object with a name, defined as function Person(name) { this.name = name; } A person's name is not going to change, but I do want to be able to access it when needed, so I could do something like: function Person(name) { var name = name; this.getName = function() { return name; } } Even in a dynamic language, I think the principles of using

Writing to read-only attributes inside a Perl Moose class

China☆狼群 提交于 2019-12-01 02:58:04
问题 Using Perl and Moose , object data can be accessed in 2 ways. $self->{attribute} or $self->attribute() Here is a simple example demonstrating both: # Person.pm package Person; use strict; use warnings; use Moose; has 'name' => (is => 'rw', isa => 'Str'); has 'age' => (is => 'ro', isa => 'Int'); sub HAPPY_BIRTHDAY { my $self = shift; $self->{age}++; # Age is accessed through method 1 } sub HAPPY_BIRTHDAY2 { my $self = shift; my $age = $self->age(); $self->age($age + 1); # Age is accessed

Getters/setters in Java

霸气de小男生 提交于 2019-12-01 02:12:37
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm trying to migrate relying on stuff I know. In ActionScript 3 you can create getters and setters using the get and set keywords, meaning you create a method in the class and access data through a property of an instance of that class. I might sound complicated, but it's not. Here's an example: class Dummy{ private var _name:String; public function Dummy(name:String=null){ this._name = name; } //getter public function get name():String{ return _name; } //setter public function set name(value:String):void{ //do some

Accessor Methods in Java

会有一股神秘感。 提交于 2019-11-30 15:57:44
问题 So I have a question on "setter" and "getter" methods and how useful they are or aren't. Let's say I just write a very basic program like the following: public class Account { String name; String address; double balance; } Then, let's say I write another class that uses this "Account" class, like the following: class UseAccount { public static void main(String[] args) { Account myAccount = new Account(); Account yourAccount = new Account(); myAccount.name = "Blah blah" } } etc., etc. When I

Accessor Methods in Java

a 夏天 提交于 2019-11-30 15:24:29
So I have a question on "setter" and "getter" methods and how useful they are or aren't. Let's say I just write a very basic program like the following: public class Account { String name; String address; double balance; } Then, let's say I write another class that uses this "Account" class, like the following: class UseAccount { public static void main(String[] args) { Account myAccount = new Account(); Account yourAccount = new Account(); myAccount.name = "Blah blah" } } etc., etc. When I write myAccount.name = "Blah blah" , I am changing the value of the variable "name" in the "Account"

MATLAB Lazy Evaluation in Dependent Property

怎甘沉沦 提交于 2019-11-30 09:04:30
I have a class with a few properties that are dependent, but that I'd really like to calculate only once. I've just about concluded that using lazy evaluation on a dependent class property in MATLAB is either impossible or a bad idea. The original plan was to have a private logical flag for each (public) property that needs updating and to have the constructor set it to true. Then when the property accessor was called, it would check that flag and calculate the value and store it (in another private property) only if required. If the flag were false, it would simply return a copy of the cached

How to get FormControl instance from ControlValueAccessor

跟風遠走 提交于 2019-11-30 05:22:15
I've the following component: @Component({ selector: 'pc-radio-button', templateUrl: './radio-button.component.html', providers: [ {provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => FieldRadioButtonComponent), multi: true} ] }) export class RadioButtonComponent implements ControlValueAccessor { ... } I can assign and alter the value through these inputs: <pc-radio-button [formControl]="formControl"></pc-radio-button> <pc-radio-button [formControlName]="inputControlName"></pc-radio-button> However I need the component to have the direct access to the assigned formControl, as I need to

What are the differences amongst Python's “__get*__” and “_del*__” methods?

浪尽此生 提交于 2019-11-29 20:12:00
I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__ equivalents: __del__ __delattr__ __delete__ __delitem__ What are the differences between these? When should I use one over the other? Is there a specific reason why most of the __get*__ methods have __set*__ equivalents, but there is no __setattribute__ ? Rik Poggi The documentation for every method that you listed is easly reachable from the documentation index . Anyway this may be a little